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