Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 1 | /* |
| 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 Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 21 | #include <vnet/srv6/sr.h> |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 22 | #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 Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 47 | _(SR_LOCALSID_ADD_DEL, sr_localsid_add_del) \ |
| 48 | _(SR_POLICY_DEL, sr_policy_del) \ |
Pablo Camarillo | 1a5e301 | 2017-11-16 16:02:50 +0100 | [diff] [blame] | 49 | _(SR_STEERING_ADD_DEL, sr_steering_add_del) \ |
| 50 | _(SR_SET_ENCAP_SOURCE, sr_set_encap_source) \ |
| 51 | _(SR_LOCALSIDS_DUMP, sr_localsids_dump) |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 52 | //_(SR_LOCALSID_BEHAVIORS, sr_localsid_behaviors_dump) |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 53 | |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 54 | static void vl_api_sr_localsid_add_del_t_handler |
| 55 | (vl_api_sr_localsid_add_del_t * mp) |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 56 | { |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 57 | vl_api_sr_localsid_add_del_reply_t *rmp; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 58 | int rv = 0; |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 59 | /* |
| 60 | * int sr_cli_localsid (char is_del, ip6_address_t *localsid_addr, |
| 61 | * char end_psp, u8 behavior, u32 sw_if_index, u32 vlan_index, u32 fib_table, |
| 62 | * ip46_address_t *nh_addr, void *ls_plugin_mem) |
| 63 | */ |
Chris Luke | 879ace3 | 2017-09-26 13:15:16 -0400 | [diff] [blame] | 64 | |
| 65 | VALIDATE_SW_IF_INDEX (mp); |
| 66 | |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 67 | rv = sr_cli_localsid (mp->is_del, |
| 68 | (ip6_address_t *) & mp->localsid_addr, |
| 69 | mp->end_psp, |
| 70 | mp->behavior, |
| 71 | ntohl (mp->sw_if_index), |
| 72 | ntohl (mp->vlan_index), |
| 73 | ntohl (mp->fib_table), |
| 74 | (ip46_address_t *) & mp->nh_addr, NULL); |
| 75 | |
Chris Luke | 879ace3 | 2017-09-26 13:15:16 -0400 | [diff] [blame] | 76 | BAD_SW_IF_INDEX_LABEL; |
| 77 | |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 78 | REPLY_MACRO (VL_API_SR_LOCALSID_ADD_DEL_REPLY); |
| 79 | } |
| 80 | |
| 81 | static void |
| 82 | vl_api_sr_policy_add_t_handler (vl_api_sr_policy_add_t * mp) |
| 83 | { |
| 84 | vl_api_sr_policy_add_reply_t *rmp; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 85 | ip6_address_t *segments = 0, *seg; |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 86 | ip6_address_t *this_address = (ip6_address_t *) mp->segments; |
| 87 | |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 88 | int i; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 89 | for (i = 0; i < mp->n_segments; i++) |
| 90 | { |
| 91 | vec_add2 (segments, seg, 1); |
| 92 | clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address)); |
| 93 | this_address++; |
| 94 | } |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * sr_policy_add (ip6_address_t *bsid, ip6_address_t *segments, |
| 98 | * u32 weight, u8 behavior, u32 fib_table, u8 is_encap) |
| 99 | */ |
| 100 | int rv = 0; |
| 101 | rv = sr_policy_add ((ip6_address_t *) & mp->bsid_addr, |
| 102 | segments, |
| 103 | ntohl (mp->weight), |
| 104 | mp->type, ntohl (mp->fib_table), mp->is_encap); |
| 105 | |
| 106 | REPLY_MACRO (VL_API_SR_POLICY_ADD_REPLY); |
| 107 | } |
| 108 | |
| 109 | static void |
| 110 | vl_api_sr_policy_mod_t_handler (vl_api_sr_policy_mod_t * mp) |
| 111 | { |
| 112 | vl_api_sr_policy_mod_reply_t *rmp; |
| 113 | |
| 114 | ip6_address_t *segments = 0, *seg; |
| 115 | ip6_address_t *this_address = (ip6_address_t *) mp->segments; |
| 116 | |
| 117 | int i; |
| 118 | for (i = 0; i < mp->n_segments; i++) |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 119 | { |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 120 | vec_add2 (segments, seg, 1); |
| 121 | clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address)); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 122 | this_address++; |
| 123 | } |
| 124 | |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 125 | int rv = 0; |
| 126 | /* |
| 127 | * int |
| 128 | * sr_policy_mod(ip6_address_t *bsid, u32 index, u32 fib_table, |
| 129 | * u8 operation, ip6_address_t *segments, u32 sl_index, |
| 130 | * u32 weight, u8 is_encap) |
| 131 | */ |
| 132 | rv = sr_policy_mod ((ip6_address_t *) & mp->bsid_addr, |
| 133 | ntohl (mp->sr_policy_index), |
| 134 | ntohl (mp->fib_table), |
| 135 | mp->operation, |
| 136 | segments, ntohl (mp->sl_index), ntohl (mp->weight)); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 137 | |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 138 | REPLY_MACRO (VL_API_SR_POLICY_MOD_REPLY); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 141 | static void |
| 142 | vl_api_sr_policy_del_t_handler (vl_api_sr_policy_del_t * mp) |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 143 | { |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 144 | vl_api_sr_policy_del_reply_t *rmp; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 145 | int rv = 0; |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 146 | /* |
| 147 | * int |
| 148 | * sr_policy_del (ip6_address_t *bsid, u32 index) |
| 149 | */ |
| 150 | rv = sr_policy_del ((ip6_address_t *) & mp->bsid_addr, |
| 151 | ntohl (mp->sr_policy_index)); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 152 | |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 153 | REPLY_MACRO (VL_API_SR_POLICY_DEL_REPLY); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Pablo Camarillo | 1a5e301 | 2017-11-16 16:02:50 +0100 | [diff] [blame] | 156 | static void |
| 157 | vl_api_sr_set_encap_source_t_handler (vl_api_sr_set_encap_source_t * mp) |
| 158 | { |
| 159 | vl_api_sr_set_encap_source_reply_t *rmp; |
| 160 | int rv = 0; |
| 161 | sr_set_source ((ip6_address_t *) & mp->encaps_source); |
| 162 | |
| 163 | REPLY_MACRO (VL_API_SR_POLICY_DEL_REPLY); |
| 164 | } |
| 165 | |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 166 | static void vl_api_sr_steering_add_del_t_handler |
| 167 | (vl_api_sr_steering_add_del_t * mp) |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 168 | { |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 169 | vl_api_sr_steering_add_del_reply_t *rmp; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 170 | int rv = 0; |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 171 | /* |
| 172 | * int |
| 173 | * sr_steering_policy(int is_del, ip6_address_t *bsid, u32 sr_policy_index, |
| 174 | * u32 table_id, ip46_address_t *prefix, u32 mask_width, u32 sw_if_index, |
| 175 | * u8 traffic_type) |
| 176 | */ |
Chris Luke | 879ace3 | 2017-09-26 13:15:16 -0400 | [diff] [blame] | 177 | |
| 178 | VALIDATE_SW_IF_INDEX (mp); |
| 179 | |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 180 | rv = sr_steering_policy (mp->is_del, |
| 181 | (ip6_address_t *) & mp->bsid_addr, |
| 182 | ntohl (mp->sr_policy_index), |
| 183 | ntohl (mp->table_id), |
| 184 | (ip46_address_t *) & mp->prefix_addr, |
| 185 | ntohl (mp->mask_width), |
| 186 | ntohl (mp->sw_if_index), mp->traffic_type); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 187 | |
Chris Luke | 879ace3 | 2017-09-26 13:15:16 -0400 | [diff] [blame] | 188 | BAD_SW_IF_INDEX_LABEL; |
| 189 | |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 190 | REPLY_MACRO (VL_API_SR_STEERING_ADD_DEL_REPLY); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Pablo Camarillo | 1a5e301 | 2017-11-16 16:02:50 +0100 | [diff] [blame] | 193 | static void send_sr_localsid_details |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 194 | (ip6_sr_localsid_t * t, vl_api_registration_t * reg, u32 context) |
Pablo Camarillo | 1a5e301 | 2017-11-16 16:02:50 +0100 | [diff] [blame] | 195 | { |
| 196 | vl_api_sr_localsids_details_t *rmp; |
| 197 | |
| 198 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 199 | memset (rmp, 0, sizeof (*rmp)); |
| 200 | rmp->_vl_msg_id = ntohs (VL_API_SR_LOCALSIDS_DETAILS); |
| 201 | memcpy (rmp->address, &t->localsid, sizeof (ip6_address_t)); |
| 202 | rmp->end_psp = t->end_psp; |
| 203 | rmp->behavior = htons (t->behavior); |
| 204 | rmp->fib_table = htonl (t->fib_table); |
| 205 | memcpy (rmp->xconnect_next_hop, &t->next_hop, sizeof (ip6_address_t)); |
| 206 | rmp->xconnect_iface_or_vrf_table = htonl (t->sw_if_index); |
| 207 | rmp->context = context; |
| 208 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 209 | vl_api_send_msg (reg, (u8 *) rmp); |
Pablo Camarillo | 1a5e301 | 2017-11-16 16:02:50 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static void vl_api_sr_localsids_dump_t_handler |
| 213 | (vl_api_sr_localsids_dump_t * mp) |
| 214 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 215 | vl_api_registration_t *reg; |
Pablo Camarillo | 1a5e301 | 2017-11-16 16:02:50 +0100 | [diff] [blame] | 216 | ip6_sr_main_t *sm = &sr_main; |
| 217 | ip6_sr_localsid_t *t; |
| 218 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 219 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 220 | if (!reg) |
| 221 | return; |
Pablo Camarillo | 1a5e301 | 2017-11-16 16:02:50 +0100 | [diff] [blame] | 222 | |
| 223 | /* *INDENT-OFF* */ |
| 224 | pool_foreach (t, sm->localsids, |
| 225 | ({ |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 226 | send_sr_localsid_details(t, reg, mp->context); |
Pablo Camarillo | 1a5e301 | 2017-11-16 16:02:50 +0100 | [diff] [blame] | 227 | })); |
| 228 | /* *INDENT-ON* */ |
| 229 | } |
| 230 | |
| 231 | |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 232 | /* |
| 233 | * sr_api_hookup |
| 234 | * Add vpe's API message handlers to the table. |
| 235 | * vlib has alread mapped shared memory and |
| 236 | * added the client registration handlers. |
| 237 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 238 | */ |
| 239 | #define vl_msg_name_crc_list |
| 240 | #include <vnet/vnet_all_api_h.h> |
| 241 | #undef vl_msg_name_crc_list |
| 242 | |
| 243 | static void |
| 244 | setup_message_id_table (api_main_t * am) |
| 245 | { |
| 246 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 247 | foreach_vl_msg_name_crc_sr; |
| 248 | #undef _ |
| 249 | } |
| 250 | |
| 251 | static clib_error_t * |
| 252 | sr_api_hookup (vlib_main_t * vm) |
| 253 | { |
| 254 | api_main_t *am = &api_main; |
| 255 | |
| 256 | #define _(N,n) \ |
| 257 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 258 | vl_api_##n##_t_handler, \ |
| 259 | vl_noop_handler, \ |
| 260 | vl_api_##n##_t_endian, \ |
| 261 | vl_api_##n##_t_print, \ |
| 262 | sizeof(vl_api_##n##_t), 1); |
| 263 | foreach_vpe_api_msg; |
| 264 | #undef _ |
| 265 | |
| 266 | /* |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 267 | * Manually register the sr policy add msg, so we trace |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 268 | * enough bytes to capture a typical segment list |
| 269 | */ |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 270 | vl_msg_api_set_handlers (VL_API_SR_POLICY_ADD, |
| 271 | "sr_policy_add", |
| 272 | vl_api_sr_policy_add_t_handler, |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 273 | vl_noop_handler, |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 274 | vl_api_sr_policy_add_t_endian, |
| 275 | vl_api_sr_policy_add_t_print, 256, 1); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 276 | |
| 277 | /* |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 278 | * Manually register the sr policy mod msg, so we trace |
| 279 | * enough bytes to capture a typical segment list |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 280 | */ |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 281 | vl_msg_api_set_handlers (VL_API_SR_POLICY_MOD, |
| 282 | "sr_policy_mod", |
| 283 | vl_api_sr_policy_mod_t_handler, |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 284 | vl_noop_handler, |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 285 | vl_api_sr_policy_mod_t_endian, |
| 286 | vl_api_sr_policy_mod_t_print, 256, 1); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 287 | |
| 288 | /* |
| 289 | * Set up the (msg_name, crc, message-id) table |
| 290 | */ |
| 291 | setup_message_id_table (am); |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | VLIB_API_INIT_FUNCTION (sr_api_hookup); |
| 297 | |
| 298 | /* |
| 299 | * fd.io coding-style-patch-verification: ON |
| 300 | * |
| 301 | * Local Variables: |
| 302 | * eval: (c-set-style "gnu") |
| 303 | * End: |
| 304 | */ |