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 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 32 | |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame] | 33 | #define vl_api_version(n, v) static u32 api_version = v; |
| 34 | #include <vnet/srmpls/sr_mpls.api.h> |
| 35 | #undef vl_api_version |
| 36 | |
| 37 | #define vl_endianfun |
| 38 | #include <vnet/srmpls/sr_mpls.api.h> |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 39 | #undef vl_endianfun |
| 40 | |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 41 | #define vl_calcsizefun |
| 42 | #include <vnet/srmpls/sr_mpls.api.h> |
| 43 | #undef vl_calcsizefun |
| 44 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 45 | #define vl_printfun |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame] | 46 | #include <vnet/srmpls/sr_mpls.api.h> |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 47 | #undef vl_printfun |
| 48 | |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame] | 49 | #define vl_msg_name_crc_list |
| 50 | #include <vnet/srmpls/sr_mpls.api.h> |
| 51 | #undef vl_msg_name_crc_list |
| 52 | |
| 53 | #define REPLY_MSG_ID_BASE msg_id_base |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 54 | #include <vlibapi/api_helper_macros.h> |
| 55 | |
| 56 | #define foreach_vpe_api_msg \ |
| 57 | _(SR_MPLS_POLICY_DEL, sr_mpls_policy_del) \ |
| 58 | _(SR_MPLS_STEERING_ADD_DEL, sr_mpls_steering_add_del) \ |
| 59 | _(SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR, sr_mpls_policy_assign_endpoint_color) |
| 60 | |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame] | 61 | static u16 msg_id_base; |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 62 | |
| 63 | static void |
| 64 | vl_api_sr_mpls_policy_add_t_handler (vl_api_sr_mpls_policy_add_t * mp) |
| 65 | { |
| 66 | vl_api_sr_mpls_policy_add_reply_t *rmp; |
| 67 | |
| 68 | mpls_label_t *segments = 0, *seg; |
| 69 | mpls_label_t this_address = 0; |
| 70 | |
| 71 | int i; |
| 72 | for (i = 0; i < mp->n_segments; i++) |
| 73 | { |
| 74 | vec_add2 (segments, seg, 1); |
| 75 | this_address = ntohl (mp->segments[i]); |
| 76 | clib_memcpy (seg, &this_address, sizeof (this_address)); |
| 77 | } |
| 78 | |
| 79 | int rv = 0; |
| 80 | rv = sr_mpls_policy_add (ntohl (mp->bsid), |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 81 | segments, mp->is_spray, ntohl (mp->weight)); |
John Lo | d23d39c | 2018-09-13 15:08:08 -0400 | [diff] [blame] | 82 | vec_free (segments); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 83 | |
| 84 | REPLY_MACRO (VL_API_SR_MPLS_POLICY_ADD_REPLY); |
| 85 | } |
| 86 | |
| 87 | static void |
| 88 | vl_api_sr_mpls_policy_mod_t_handler (vl_api_sr_mpls_policy_mod_t * mp) |
| 89 | { |
| 90 | vl_api_sr_mpls_policy_mod_reply_t *rmp; |
| 91 | |
| 92 | mpls_label_t *segments = 0, *seg; |
| 93 | mpls_label_t this_address = 0; |
| 94 | |
| 95 | int i; |
| 96 | for (i = 0; i < mp->n_segments; i++) |
| 97 | { |
| 98 | vec_add2 (segments, seg, 1); |
| 99 | this_address = ntohl (mp->segments[i]); |
| 100 | clib_memcpy (seg, &this_address, sizeof (this_address)); |
| 101 | } |
| 102 | |
| 103 | int rv = 0; |
| 104 | rv = sr_mpls_policy_mod (ntohl (mp->bsid), |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 105 | ntohl (mp->operation), segments, |
| 106 | ntohl (mp->sl_index), ntohl (mp->weight)); |
John Lo | d23d39c | 2018-09-13 15:08:08 -0400 | [diff] [blame] | 107 | vec_free (segments); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 108 | |
| 109 | REPLY_MACRO (VL_API_SR_MPLS_POLICY_MOD_REPLY); |
| 110 | } |
| 111 | |
| 112 | static void |
| 113 | vl_api_sr_mpls_policy_del_t_handler (vl_api_sr_mpls_policy_del_t * mp) |
| 114 | { |
| 115 | vl_api_sr_mpls_policy_del_reply_t *rmp; |
| 116 | int rv = 0; |
| 117 | rv = sr_mpls_policy_del (ntohl (mp->bsid)); |
| 118 | |
| 119 | REPLY_MACRO (VL_API_SR_MPLS_POLICY_DEL_REPLY); |
| 120 | } |
| 121 | |
| 122 | static void vl_api_sr_mpls_steering_add_del_t_handler |
| 123 | (vl_api_sr_mpls_steering_add_del_t * mp) |
| 124 | { |
| 125 | vl_api_sr_mpls_steering_add_del_reply_t *rmp; |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 126 | fib_prefix_t prefix; |
| 127 | ip46_address_t next_hop; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 128 | clib_memset (&prefix, 0, sizeof (ip46_address_t)); |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 129 | |
| 130 | ip_prefix_decode (&mp->prefix, &prefix); |
| 131 | ip_address_decode (&mp->next_hop, &next_hop); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 132 | |
| 133 | int rv = 0; |
| 134 | if (mp->is_del) |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 135 | rv = sr_mpls_steering_policy_del (&prefix.fp_addr, |
| 136 | prefix.fp_len, |
| 137 | ip46_address_is_ip4 (&prefix.fp_addr) ? |
| 138 | SR_STEER_IPV4 : SR_STEER_IPV6, |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 139 | ntohl (mp->table_id), |
| 140 | ntohl (mp->color)); |
| 141 | else |
| 142 | rv = sr_mpls_steering_policy_add (ntohl (mp->bsid), |
| 143 | ntohl (mp->table_id), |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 144 | &prefix.fp_addr, |
| 145 | prefix.fp_len, |
| 146 | ip46_address_is_ip4 (&prefix.fp_addr) ? |
| 147 | SR_STEER_IPV4 : SR_STEER_IPV6, |
| 148 | &next_hop, |
| 149 | ip46_address_is_ip4 (&next_hop) ? |
| 150 | SR_STEER_IPV4 : SR_STEER_IPV6, |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 151 | ntohl (mp->color), mp->co_bits, |
| 152 | ntohl (mp->vpn_label)); |
| 153 | |
| 154 | REPLY_MACRO (VL_API_SR_MPLS_STEERING_ADD_DEL_REPLY); |
| 155 | } |
| 156 | |
| 157 | static void vl_api_sr_mpls_policy_assign_endpoint_color_t_handler |
| 158 | (vl_api_sr_mpls_policy_assign_endpoint_color_t * mp) |
| 159 | { |
| 160 | vl_api_sr_mpls_policy_assign_endpoint_color_reply_t *rmp; |
| 161 | int rv = 0; |
| 162 | |
| 163 | ip46_address_t endpoint; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 164 | clib_memset (&endpoint, 0, sizeof (ip46_address_t)); |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 165 | ip_address_decode (&mp->endpoint, &endpoint); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 166 | |
| 167 | rv = sr_mpls_policy_assign_endpoint_color (ntohl (mp->bsid), |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 168 | &endpoint, |
| 169 | ip46_address_is_ip4 (&endpoint) ? |
| 170 | SR_STEER_IPV4 : SR_STEER_IPV6, |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 171 | ntohl (mp->color)); |
| 172 | |
| 173 | REPLY_MACRO (VL_API_SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR_REPLY); |
| 174 | } |
| 175 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 176 | static void |
| 177 | setup_message_id_table (api_main_t * am) |
| 178 | { |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame] | 179 | #define _(id, n, crc) \ |
| 180 | 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] | 181 | foreach_vl_msg_name_crc_sr_mpls; |
| 182 | #undef _ |
| 183 | } |
| 184 | |
| 185 | static clib_error_t * |
| 186 | sr_mpls_api_hookup (vlib_main_t * vm) |
| 187 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 188 | api_main_t *am = vlibapi_get_main (); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 189 | |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame] | 190 | u8 *name = format (0, "sr_mpls_%08x%c", api_version, 0); |
| 191 | REPLY_MSG_ID_BASE = |
| 192 | vl_msg_api_get_msg_ids ((char *) name, VL_MSG_SR_MPLS_LAST); |
| 193 | vec_free (name); |
| 194 | |
| 195 | #define _(N, n) \ |
Damjan Marion | a2eb507 | 2022-05-20 20:06:01 +0200 | [diff] [blame] | 196 | vl_msg_api_config (&(vl_msg_api_msg_config_t){ \ |
| 197 | .id = REPLY_MSG_ID_BASE + VL_API_##N, \ |
| 198 | .name = #n, \ |
| 199 | .handler = vl_api_##n##_t_handler, \ |
| 200 | .endian = vl_api_##n##_t_endian, \ |
| 201 | .format_fn = vl_api_##n##_t_format, \ |
| 202 | .size = sizeof (vl_api_##n##_t), \ |
| 203 | .traced = 1, \ |
| 204 | .tojson = vl_api_##n##_t_tojson, \ |
| 205 | .fromjson = vl_api_##n##_t_fromjson, \ |
| 206 | .calc_size = vl_api_##n##_t_calc_size, \ |
| 207 | }); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 208 | foreach_vpe_api_msg; |
| 209 | #undef _ |
| 210 | |
| 211 | /* |
| 212 | * Manually register the sr policy add msg, so we trace enough bytes |
| 213 | * to capture a typical segment list |
| 214 | */ |
Damjan Marion | a2eb507 | 2022-05-20 20:06:01 +0200 | [diff] [blame] | 215 | vl_msg_api_config (&(vl_msg_api_msg_config_t){ |
| 216 | .id = REPLY_MSG_ID_BASE + VL_API_SR_MPLS_POLICY_ADD, |
| 217 | .name = "sr_mpls_policy_add", |
| 218 | .handler = vl_api_sr_mpls_policy_add_t_handler, |
| 219 | .endian = vl_api_sr_mpls_policy_add_t_endian, |
| 220 | .format_fn = vl_api_sr_mpls_policy_add_t_format, |
| 221 | .size = 256, |
| 222 | .traced = 1, |
| 223 | .tojson = vl_api_sr_mpls_policy_add_t_tojson, |
| 224 | .fromjson = vl_api_sr_mpls_policy_add_t_fromjson, |
| 225 | .calc_size = vl_api_sr_mpls_policy_add_t_calc_size, |
| 226 | }); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 227 | /* |
| 228 | * Manually register the sr policy mod msg, so we trace enough bytes |
| 229 | * to capture a typical segment list |
| 230 | */ |
Damjan Marion | a2eb507 | 2022-05-20 20:06:01 +0200 | [diff] [blame] | 231 | vl_msg_api_config (&(vl_msg_api_msg_config_t){ |
| 232 | .id = REPLY_MSG_ID_BASE + VL_API_SR_MPLS_POLICY_MOD, |
| 233 | .name = "sr_mpls_policy_mod", |
| 234 | .handler = vl_api_sr_mpls_policy_mod_t_handler, |
| 235 | .endian = vl_api_sr_mpls_policy_mod_t_endian, |
| 236 | .format_fn = vl_api_sr_mpls_policy_mod_t_format, |
| 237 | .size = 256, |
| 238 | .traced = 1, |
| 239 | .tojson = vl_api_sr_mpls_policy_mod_t_tojson, |
| 240 | .fromjson = vl_api_sr_mpls_policy_mod_t_fromjson, |
| 241 | .calc_size = vl_api_sr_mpls_policy_mod_t_calc_size, |
| 242 | }); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 243 | |
| 244 | /* |
| 245 | * Set up the (msg_name, crc, message-id) table |
| 246 | */ |
| 247 | setup_message_id_table (am); |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | VLIB_API_INIT_FUNCTION (sr_mpls_api_hookup); |
| 253 | |
| 254 | /* |
| 255 | * fd.io coding-style-patch-verification: ON |
| 256 | * |
| 257 | * Local Variables: eval: (c-set-style "gnu") End: |
| 258 | */ |