Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * mpls_api.c - mpls 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> |
| 21 | #include <vlibmemory/api.h> |
| 22 | |
| 23 | #include <vnet/interface.h> |
| 24 | #include <vnet/api_errno.h> |
| 25 | #include <vnet/mpls/mpls.h> |
| 26 | #include <vnet/mpls/mpls_tunnel.h> |
| 27 | #include <vnet/fib/fib_table.h> |
| 28 | #include <vnet/fib/fib_api.h> |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 29 | #include <vnet/fib/mpls_fib.h> |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 30 | #include <vnet/fib/fib_path_list.h> |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 31 | #include <vnet/ip/ip_types_api.h> |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 32 | |
| 33 | #include <vnet/vnet_msg_enum.h> |
| 34 | |
| 35 | #define vl_typedefs /* define message structures */ |
| 36 | #include <vnet/vnet_all_api_h.h> |
| 37 | #undef vl_typedefs |
| 38 | |
| 39 | #define vl_endianfun /* define message structures */ |
| 40 | #include <vnet/vnet_all_api_h.h> |
| 41 | #undef vl_endianfun |
| 42 | |
| 43 | /* instantiate all the print functions we know about */ |
| 44 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 45 | #define vl_printfun |
| 46 | #include <vnet/vnet_all_api_h.h> |
| 47 | #undef vl_printfun |
| 48 | |
| 49 | #include <vlibapi/api_helper_macros.h> |
| 50 | |
| 51 | #define foreach_vpe_api_msg \ |
| 52 | _(MPLS_IP_BIND_UNBIND, mpls_ip_bind_unbind) \ |
| 53 | _(MPLS_ROUTE_ADD_DEL, mpls_route_add_del) \ |
Neale Ranns | 28ab9cc | 2017-08-14 07:18:42 -0700 | [diff] [blame] | 54 | _(MPLS_TABLE_ADD_DEL, mpls_table_add_del) \ |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 55 | _(MPLS_TUNNEL_ADD_DEL, mpls_tunnel_add_del) \ |
| 56 | _(MPLS_TUNNEL_DUMP, mpls_tunnel_dump) \ |
Neale Ranns | b8d4481 | 2017-11-10 06:53:54 -0800 | [diff] [blame] | 57 | _(SW_INTERFACE_SET_MPLS_ENABLE, sw_interface_set_mpls_enable) \ |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 58 | _(MPLS_TABLE_DUMP, mpls_table_dump) \ |
| 59 | _(MPLS_ROUTE_DUMP, mpls_route_dump) |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 60 | |
Neale Ranns | 28ab9cc | 2017-08-14 07:18:42 -0700 | [diff] [blame] | 61 | void |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 62 | mpls_table_delete (u32 table_id, u8 is_api) |
| 63 | { |
| 64 | u32 fib_index; |
| 65 | |
| 66 | /* |
| 67 | * The MPLS defult table must also be explicitly created via the API. |
| 68 | * So in contrast to IP, it gets no special treatment here. |
| 69 | * |
| 70 | * The API holds only one lock on the table. |
| 71 | * i.e. it can be added many times via the API but needs to be |
| 72 | * deleted only once. |
| 73 | */ |
| 74 | fib_index = fib_table_find (FIB_PROTOCOL_MPLS, table_id); |
| 75 | |
| 76 | if (~0 != fib_index) |
| 77 | { |
| 78 | fib_table_unlock (fib_index, |
| 79 | FIB_PROTOCOL_MPLS, |
| 80 | (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI)); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void |
Neale Ranns | 28ab9cc | 2017-08-14 07:18:42 -0700 | [diff] [blame] | 85 | vl_api_mpls_table_add_del_t_handler (vl_api_mpls_table_add_del_t * mp) |
| 86 | { |
| 87 | vl_api_mpls_table_add_del_reply_t *rmp; |
| 88 | vnet_main_t *vnm; |
| 89 | int rv = 0; |
| 90 | |
| 91 | vnm = vnet_get_main (); |
| 92 | vnm->api_errno = 0; |
| 93 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 94 | if (mp->mt_is_add) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 95 | mpls_table_create (ntohl (mp->mt_table.mt_table_id), |
| 96 | 1, mp->mt_table.mt_name); |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 97 | else |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 98 | mpls_table_delete (ntohl (mp->mt_table.mt_table_id), 1); |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 99 | |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 100 | // NB: Nothing sets rv; none of the above returns an error |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 101 | |
Neale Ranns | 28ab9cc | 2017-08-14 07:18:42 -0700 | [diff] [blame] | 102 | REPLY_MACRO (VL_API_MPLS_TABLE_ADD_DEL_REPLY); |
| 103 | } |
| 104 | |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 105 | static int |
| 106 | mpls_ip_bind_unbind_handler (vnet_main_t * vnm, |
| 107 | vl_api_mpls_ip_bind_unbind_t * mp) |
| 108 | { |
| 109 | u32 mpls_fib_index, ip_fib_index; |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 110 | fib_prefix_t pfx; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 111 | |
| 112 | mpls_fib_index = |
| 113 | fib_table_find (FIB_PROTOCOL_MPLS, ntohl (mp->mb_mpls_table_id)); |
| 114 | |
| 115 | if (~0 == mpls_fib_index) |
| 116 | { |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 117 | return VNET_API_ERROR_NO_SUCH_FIB; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 120 | ip_prefix_decode (&mp->mb_prefix, &pfx); |
| 121 | |
| 122 | ip_fib_index = fib_table_find (pfx.fp_proto, ntohl (mp->mb_ip_table_id)); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 123 | if (~0 == ip_fib_index) |
| 124 | return VNET_API_ERROR_NO_SUCH_FIB; |
| 125 | |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 126 | if (mp->mb_is_bind) |
| 127 | fib_table_entry_local_label_add (ip_fib_index, &pfx, |
| 128 | ntohl (mp->mb_label)); |
| 129 | else |
| 130 | fib_table_entry_local_label_remove (ip_fib_index, &pfx, |
| 131 | ntohl (mp->mb_label)); |
| 132 | |
| 133 | return (0); |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | vl_api_mpls_ip_bind_unbind_t_handler (vl_api_mpls_ip_bind_unbind_t * mp) |
| 138 | { |
| 139 | vl_api_mpls_ip_bind_unbind_reply_t *rmp; |
| 140 | vnet_main_t *vnm; |
| 141 | int rv; |
| 142 | |
| 143 | vnm = vnet_get_main (); |
| 144 | vnm->api_errno = 0; |
| 145 | |
| 146 | rv = mpls_ip_bind_unbind_handler (vnm, mp); |
| 147 | rv = (rv == 0) ? vnm->api_errno : rv; |
| 148 | |
| 149 | REPLY_MACRO (VL_API_MPLS_IP_BIND_UNBIND_REPLY); |
| 150 | } |
| 151 | |
| 152 | static int |
| 153 | mpls_route_add_del_t_handler (vnet_main_t * vnm, |
Neale Ranns | 008dbe1 | 2018-09-07 09:32:36 -0700 | [diff] [blame] | 154 | vl_api_mpls_route_add_del_t * mp, |
| 155 | u32 * stats_index) |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 156 | { |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 157 | fib_route_path_t *rpaths = NULL, *rpath; |
| 158 | vl_api_fib_path_t *apath; |
| 159 | u32 fib_index; |
| 160 | int rv, ii; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 161 | |
| 162 | fib_prefix_t pfx = { |
| 163 | .fp_len = 21, |
| 164 | .fp_proto = FIB_PROTOCOL_MPLS, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 165 | .fp_eos = mp->mr_route.mr_eos, |
| 166 | .fp_label = ntohl (mp->mr_route.mr_label), |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 167 | }; |
| 168 | if (pfx.fp_eos) |
| 169 | { |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 170 | pfx.fp_payload_proto = mp->mr_route.mr_eos_proto; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 171 | } |
| 172 | else |
| 173 | { |
| 174 | pfx.fp_payload_proto = DPO_PROTO_MPLS; |
| 175 | } |
| 176 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 177 | rv = fib_api_table_id_decode (FIB_PROTOCOL_MPLS, |
| 178 | ntohl (mp->mr_route.mr_table_id), &fib_index); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 179 | if (0 != rv) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 180 | goto out; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 181 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 182 | vec_validate (rpaths, mp->mr_route.mr_n_paths - 1); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 183 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 184 | for (ii = 0; ii < mp->mr_route.mr_n_paths; ii++) |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 185 | { |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 186 | apath = &mp->mr_route.mr_paths[ii]; |
| 187 | rpath = &rpaths[ii]; |
| 188 | |
| 189 | rv = fib_api_path_decode (apath, rpath); |
| 190 | |
| 191 | if (0 != rv) |
| 192 | goto out; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Neale Ranns | c2ac235 | 2019-07-02 14:33:29 +0000 | [diff] [blame] | 195 | rv = fib_api_route_add_del (mp->mr_is_add, |
| 196 | mp->mr_is_multipath, |
| 197 | fib_index, |
| 198 | &pfx, |
| 199 | (mp->mr_route.mr_is_multicast ? |
| 200 | FIB_ENTRY_FLAG_MULTICAST : |
| 201 | FIB_ENTRY_FLAG_NONE), rpaths); |
Neale Ranns | 008dbe1 | 2018-09-07 09:32:36 -0700 | [diff] [blame] | 202 | |
| 203 | if (mp->mr_is_add && 0 == rv) |
| 204 | *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx); |
| 205 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 206 | out: |
| 207 | vec_free (rpaths); |
| 208 | |
Neale Ranns | 008dbe1 | 2018-09-07 09:32:36 -0700 | [diff] [blame] | 209 | return (rv); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void |
| 213 | vl_api_mpls_route_add_del_t_handler (vl_api_mpls_route_add_del_t * mp) |
| 214 | { |
| 215 | vl_api_mpls_route_add_del_reply_t *rmp; |
| 216 | vnet_main_t *vnm; |
Neale Ranns | 008dbe1 | 2018-09-07 09:32:36 -0700 | [diff] [blame] | 217 | u32 stats_index; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 218 | int rv; |
| 219 | |
| 220 | vnm = vnet_get_main (); |
Neale Ranns | 008dbe1 | 2018-09-07 09:32:36 -0700 | [diff] [blame] | 221 | stats_index = ~0; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 222 | |
Neale Ranns | 008dbe1 | 2018-09-07 09:32:36 -0700 | [diff] [blame] | 223 | rv = mpls_route_add_del_t_handler (vnm, mp, &stats_index); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 224 | |
Neale Ranns | 008dbe1 | 2018-09-07 09:32:36 -0700 | [diff] [blame] | 225 | /* *INDENT-OFF* */ |
| 226 | REPLY_MACRO2 (VL_API_MPLS_ROUTE_ADD_DEL_REPLY, |
| 227 | ({ |
| 228 | rmp->stats_index = htonl (stats_index); |
| 229 | })); |
| 230 | /* *INDENT-ON* */ |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 233 | void |
Neale Ranns | 2297af0 | 2017-09-12 09:45:04 -0700 | [diff] [blame] | 234 | mpls_table_create (u32 table_id, u8 is_api, const u8 * name) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 235 | { |
| 236 | u32 fib_index; |
| 237 | |
| 238 | /* |
| 239 | * The MPLS defult table must also be explicitly created via the API. |
| 240 | * So in contrast to IP, it gets no special treatment here. |
| 241 | */ |
| 242 | |
| 243 | /* |
| 244 | * The API holds only one lock on the table. |
| 245 | * i.e. it can be added many times via the API but needs to be |
| 246 | * deleted only once. |
| 247 | */ |
| 248 | fib_index = fib_table_find (FIB_PROTOCOL_MPLS, table_id); |
| 249 | |
| 250 | if (~0 == fib_index) |
| 251 | { |
Neale Ranns | 2297af0 | 2017-09-12 09:45:04 -0700 | [diff] [blame] | 252 | fib_table_find_or_create_and_lock_w_name (FIB_PROTOCOL_MPLS, |
| 253 | table_id, |
| 254 | (is_api ? |
| 255 | FIB_SOURCE_API : |
| 256 | FIB_SOURCE_CLI), name); |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 260 | static void |
| 261 | vl_api_mpls_tunnel_add_del_t_handler (vl_api_mpls_tunnel_add_del_t * mp) |
| 262 | { |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 263 | u32 tunnel_sw_if_index = ~0, tunnel_index = ~0; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 264 | vl_api_mpls_tunnel_add_del_reply_t *rmp; |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 265 | fib_route_path_t *rpath, *rpaths = NULL; |
Neale Ranns | f5fa5ae | 2018-09-26 05:07:25 -0700 | [diff] [blame] | 266 | int ii, rv = 0; |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 267 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 268 | vec_validate (rpaths, mp->mt_tunnel.mt_n_paths - 1); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 269 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 270 | for (ii = 0; ii < mp->mt_tunnel.mt_n_paths; ii++) |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 271 | { |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 272 | rpath = &rpaths[ii]; |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 273 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 274 | rv = fib_api_path_decode (&mp->mt_tunnel.mt_paths[ii], rpath); |
| 275 | |
| 276 | if (0 != rv) |
| 277 | goto out; |
Neale Ranns | 7c922dc | 2018-08-30 06:12:27 -0700 | [diff] [blame] | 278 | } |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 279 | tunnel_sw_if_index = ntohl (mp->mt_tunnel.mt_sw_if_index); |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 280 | |
| 281 | if (mp->mt_is_add) |
| 282 | { |
| 283 | if (~0 == tunnel_sw_if_index) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 284 | tunnel_sw_if_index = |
| 285 | vnet_mpls_tunnel_create (mp->mt_tunnel.mt_l2_only, |
IJsbrand Wijnands | 39ae0a0 | 2020-03-05 10:56:26 -0800 | [diff] [blame] | 286 | mp->mt_tunnel.mt_is_multicast, |
| 287 | mp->mt_tunnel.mt_tag); |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 288 | vnet_mpls_tunnel_path_add (tunnel_sw_if_index, rpaths); |
Neale Ranns | f5fa5ae | 2018-09-26 05:07:25 -0700 | [diff] [blame] | 289 | |
| 290 | tunnel_index = vnet_mpls_tunnel_get_index (tunnel_sw_if_index); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 291 | } |
| 292 | else |
| 293 | { |
Neale Ranns | f5fa5ae | 2018-09-26 05:07:25 -0700 | [diff] [blame] | 294 | tunnel_index = vnet_mpls_tunnel_get_index (tunnel_sw_if_index); |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 295 | tunnel_sw_if_index = ntohl (mp->mt_tunnel.mt_sw_if_index); |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 296 | if (!vnet_mpls_tunnel_path_remove (tunnel_sw_if_index, rpaths)) |
| 297 | vnet_mpls_tunnel_del (tunnel_sw_if_index); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 298 | } |
| 299 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 300 | vec_free (rpaths); |
| 301 | |
John Lo | 06fda9c | 2018-10-03 16:32:44 -0400 | [diff] [blame] | 302 | out: |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 303 | /* *INDENT-OFF* */ |
| 304 | REPLY_MACRO2(VL_API_MPLS_TUNNEL_ADD_DEL_REPLY, |
| 305 | ({ |
| 306 | rmp->sw_if_index = ntohl(tunnel_sw_if_index); |
Neale Ranns | f5fa5ae | 2018-09-26 05:07:25 -0700 | [diff] [blame] | 307 | rmp->tunnel_index = ntohl(tunnel_index); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 308 | })); |
| 309 | /* *INDENT-ON* */ |
| 310 | } |
| 311 | |
Neale Ranns | b8d4481 | 2017-11-10 06:53:54 -0800 | [diff] [blame] | 312 | static void |
| 313 | vl_api_sw_interface_set_mpls_enable_t_handler |
| 314 | (vl_api_sw_interface_set_mpls_enable_t * mp) |
| 315 | { |
| 316 | vl_api_sw_interface_set_mpls_enable_reply_t *rmp; |
| 317 | int rv = 0; |
| 318 | |
| 319 | VALIDATE_SW_IF_INDEX (mp); |
| 320 | |
| 321 | rv = mpls_sw_interface_enable_disable (&mpls_main, |
| 322 | ntohl (mp->sw_if_index), |
| 323 | mp->enable, 1); |
| 324 | |
| 325 | BAD_SW_IF_INDEX_LABEL; |
| 326 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_MPLS_ENABLE_REPLY); |
| 327 | } |
| 328 | |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 329 | typedef struct mpls_tunnel_send_walk_ctx_t_ |
| 330 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 331 | vl_api_registration_t *reg; |
Neale Ranns | f5fa5ae | 2018-09-26 05:07:25 -0700 | [diff] [blame] | 332 | u32 sw_if_index; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 333 | u32 context; |
| 334 | } mpls_tunnel_send_walk_ctx_t; |
| 335 | |
| 336 | static void |
| 337 | send_mpls_tunnel_entry (u32 mti, void *arg) |
| 338 | { |
| 339 | mpls_tunnel_send_walk_ctx_t *ctx; |
| 340 | vl_api_mpls_tunnel_details_t *mp; |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 341 | fib_path_encode_ctx_t path_ctx = { |
| 342 | .rpaths = NULL, |
| 343 | }; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 344 | const mpls_tunnel_t *mt; |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 345 | fib_route_path_t *rpath; |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 346 | vl_api_fib_path_t *fp; |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 347 | u32 n; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 348 | |
| 349 | ctx = arg; |
| 350 | |
Neale Ranns | f5fa5ae | 2018-09-26 05:07:25 -0700 | [diff] [blame] | 351 | mt = mpls_tunnel_get (mti); |
| 352 | |
| 353 | if (~0 != ctx->sw_if_index && mt->mt_sw_if_index != ctx->sw_if_index) |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 354 | return; |
| 355 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 356 | n = fib_path_list_get_n_paths (mt->mt_path_list); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 357 | |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 358 | mp = vl_msg_api_alloc (sizeof (*mp) + n * sizeof (vl_api_fib_path_t)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 359 | clib_memset (mp, 0, sizeof (*mp) + n * sizeof (vl_api_fib_path_t)); |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 360 | |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 361 | mp->_vl_msg_id = ntohs (VL_API_MPLS_TUNNEL_DETAILS); |
| 362 | mp->context = ctx->context; |
| 363 | |
IJsbrand Wijnands | bf103d9 | 2019-10-28 17:19:27 +0100 | [diff] [blame] | 364 | mp->mt_tunnel.mt_n_paths = n; |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 365 | mp->mt_tunnel.mt_sw_if_index = ntohl (mt->mt_sw_if_index); |
| 366 | mp->mt_tunnel.mt_tunnel_index = ntohl (mti); |
| 367 | mp->mt_tunnel.mt_l2_only = ! !(MPLS_TUNNEL_FLAG_L2 & mt->mt_flags); |
| 368 | mp->mt_tunnel.mt_is_multicast = ! !(MPLS_TUNNEL_FLAG_MCAST & mt->mt_flags); |
IJsbrand Wijnands | 39ae0a0 | 2020-03-05 10:56:26 -0800 | [diff] [blame] | 369 | memcpy (mp->mt_tunnel.mt_tag, mt->mt_tag, sizeof (mp->mt_tunnel.mt_tag)); |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 370 | |
Neale Ranns | 775f73c | 2018-12-20 03:01:49 -0800 | [diff] [blame] | 371 | fib_path_list_walk_w_ext (mt->mt_path_list, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 372 | &mt->mt_path_exts, fib_path_encode, &path_ctx); |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 373 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 374 | fp = mp->mt_tunnel.mt_paths; |
| 375 | vec_foreach (rpath, path_ctx.rpaths) |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 376 | { |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 377 | fib_api_path_encode (rpath, fp); |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 378 | fp++; |
| 379 | } |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 380 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 381 | vl_api_send_msg (ctx->reg, (u8 *) mp); |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 382 | |
| 383 | vec_free (path_ctx.rpaths); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | static void |
| 387 | vl_api_mpls_tunnel_dump_t_handler (vl_api_mpls_tunnel_dump_t * mp) |
| 388 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 389 | vl_api_registration_t *reg; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 390 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 391 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 392 | if (!reg) |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 393 | return; |
| 394 | |
| 395 | mpls_tunnel_send_walk_ctx_t ctx = { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 396 | .reg = reg, |
Neale Ranns | f5fa5ae | 2018-09-26 05:07:25 -0700 | [diff] [blame] | 397 | .sw_if_index = ntohl (mp->sw_if_index), |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 398 | .context = mp->context, |
| 399 | }; |
| 400 | mpls_tunnel_walk (send_mpls_tunnel_entry, &ctx); |
| 401 | } |
| 402 | |
| 403 | static void |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 404 | send_mpls_table_details (vpe_api_main_t * am, |
| 405 | vl_api_registration_t * reg, |
| 406 | u32 context, const fib_table_t * table) |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 407 | { |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 408 | vl_api_mpls_table_details_t *mp; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 409 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 410 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 411 | memset (mp, 0, sizeof (*mp)); |
| 412 | mp->_vl_msg_id = ntohs (VL_API_MPLS_TABLE_DETAILS); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 413 | mp->context = context; |
| 414 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 415 | mp->mt_table.mt_table_id = htonl (table->ft_table_id); |
| 416 | memcpy (mp->mt_table.mt_name, |
| 417 | table->ft_desc, |
| 418 | clib_min (vec_len (table->ft_desc), sizeof (mp->mt_table.mt_name))); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 419 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 420 | vl_api_send_msg (reg, (u8 *) mp); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 421 | } |
| 422 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 423 | static void |
| 424 | vl_api_mpls_table_dump_t_handler (vl_api_mpls_table_dump_t * mp) |
| 425 | { |
| 426 | vpe_api_main_t *am = &vpe_api_main; |
| 427 | vl_api_registration_t *reg; |
| 428 | mpls_main_t *mm = &mpls_main; |
| 429 | fib_table_t *fib_table; |
| 430 | |
| 431 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 432 | if (!reg) |
| 433 | return; |
| 434 | |
| 435 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 436 | pool_foreach (fib_table, mm->fibs) |
| 437 | { |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 438 | send_mpls_table_details(am, reg, mp->context, fib_table); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 439 | } |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 440 | /* *INDENT-ON* */ |
| 441 | } |
| 442 | |
| 443 | static void |
| 444 | send_mpls_route_details (vpe_api_main_t * am, |
| 445 | vl_api_registration_t * reg, |
| 446 | u32 context, fib_node_index_t fib_entry_index) |
| 447 | { |
| 448 | fib_route_path_t *rpaths, *rpath; |
| 449 | vl_api_mpls_route_details_t *mp; |
| 450 | const fib_prefix_t *pfx; |
| 451 | vl_api_fib_path_t *fp; |
| 452 | int path_count; |
| 453 | |
| 454 | rpaths = fib_entry_encode (fib_entry_index); |
| 455 | pfx = fib_entry_get_prefix (fib_entry_index); |
| 456 | |
| 457 | path_count = vec_len (rpaths); |
| 458 | mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp)); |
| 459 | if (!mp) |
| 460 | return; |
| 461 | clib_memset (mp, 0, sizeof (*mp)); |
| 462 | mp->_vl_msg_id = ntohs (VL_API_MPLS_ROUTE_DETAILS); |
| 463 | mp->context = context; |
| 464 | |
| 465 | mp->mr_route.mr_table_id = |
| 466 | htonl (fib_table_get_table_id |
| 467 | (fib_entry_get_fib_index (fib_entry_index), pfx->fp_proto)); |
| 468 | mp->mr_route.mr_eos = pfx->fp_eos; |
| 469 | mp->mr_route.mr_eos_proto = pfx->fp_payload_proto; |
| 470 | mp->mr_route.mr_label = htonl (pfx->fp_label); |
| 471 | |
| 472 | mp->mr_route.mr_n_paths = path_count; |
| 473 | fp = mp->mr_route.mr_paths; |
| 474 | vec_foreach (rpath, rpaths) |
| 475 | { |
| 476 | fib_api_path_encode (rpath, fp); |
| 477 | fp++; |
| 478 | } |
| 479 | |
| 480 | vec_free (rpaths); |
| 481 | vl_api_send_msg (reg, (u8 *) mp); |
| 482 | } |
| 483 | |
| 484 | typedef struct vl_api_mpls_route_dump_table_walk_ctx_t_ |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 485 | { |
| 486 | fib_node_index_t *lfeis; |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 487 | } vl_api_mpls_route_dump_table_walk_ctx_t; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 488 | |
Neale Ranns | 8954199 | 2017-04-06 04:41:02 -0700 | [diff] [blame] | 489 | static fib_table_walk_rc_t |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 490 | vl_api_mpls_route_dump_table_walk (fib_node_index_t fei, void *arg) |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 491 | { |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 492 | vl_api_mpls_route_dump_table_walk_ctx_t *ctx = arg; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 493 | |
| 494 | vec_add1 (ctx->lfeis, fei); |
| 495 | |
Neale Ranns | 8954199 | 2017-04-06 04:41:02 -0700 | [diff] [blame] | 496 | return (FIB_TABLE_WALK_CONTINUE); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 499 | static void |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 500 | vl_api_mpls_route_dump_t_handler (vl_api_mpls_route_dump_t * mp) |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 501 | { |
| 502 | vpe_api_main_t *am = &vpe_api_main; |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 503 | vl_api_registration_t *reg; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 504 | fib_node_index_t *lfeip = NULL; |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 505 | vl_api_mpls_route_dump_table_walk_ctx_t ctx = { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 506 | .lfeis = NULL, |
| 507 | }; |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 508 | u32 fib_index; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 509 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 510 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 511 | if (!reg) |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 512 | return; |
| 513 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 514 | fib_index = fib_table_find (FIB_PROTOCOL_MPLS, |
| 515 | ntohl (mp->table.mt_table_id)); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 516 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 517 | if (INDEX_INVALID != fib_index) |
| 518 | { |
| 519 | fib_table_walk (fib_index, |
| 520 | FIB_PROTOCOL_MPLS, |
| 521 | vl_api_mpls_route_dump_table_walk, &ctx); |
| 522 | vec_sort_with_function (ctx.lfeis, fib_entry_cmp_for_sort); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 523 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 524 | vec_foreach (lfeip, ctx.lfeis) |
| 525 | { |
| 526 | send_mpls_route_details (am, reg, mp->context, *lfeip); |
| 527 | } |
| 528 | |
| 529 | vec_free (ctx.lfeis); |
| 530 | } |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* |
| 534 | * mpls_api_hookup |
| 535 | * Add vpe's API message handlers to the table. |
Jim Thompson | f324dec | 2019-04-08 03:22:21 -0500 | [diff] [blame] | 536 | * vlib has already mapped shared memory and |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 537 | * added the client registration handlers. |
| 538 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 539 | */ |
| 540 | #define vl_msg_name_crc_list |
| 541 | #include <vnet/vnet_all_api_h.h> |
| 542 | #undef vl_msg_name_crc_list |
| 543 | |
| 544 | static void |
| 545 | setup_message_id_table (api_main_t * am) |
| 546 | { |
| 547 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 548 | foreach_vl_msg_name_crc_mpls; |
| 549 | #undef _ |
| 550 | } |
| 551 | |
| 552 | static clib_error_t * |
| 553 | mpls_api_hookup (vlib_main_t * vm) |
| 554 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 555 | api_main_t *am = vlibapi_get_main (); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 556 | |
| 557 | #define _(N,n) \ |
| 558 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 559 | vl_api_##n##_t_handler, \ |
| 560 | vl_noop_handler, \ |
| 561 | vl_api_##n##_t_endian, \ |
| 562 | vl_api_##n##_t_print, \ |
| 563 | sizeof(vl_api_##n##_t), 1); |
| 564 | foreach_vpe_api_msg; |
| 565 | #undef _ |
| 566 | |
| 567 | /* |
| 568 | * Trace space for 8 MPLS encap labels |
| 569 | */ |
| 570 | am->api_trace_cfg[VL_API_MPLS_TUNNEL_ADD_DEL].size += 8 * sizeof (u32); |
| 571 | |
| 572 | /* |
| 573 | * Set up the (msg_name, crc, message-id) table |
| 574 | */ |
| 575 | setup_message_id_table (am); |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | VLIB_API_INIT_FUNCTION (mpls_api_hookup); |
| 581 | |
| 582 | /* |
| 583 | * fd.io coding-style-patch-verification: ON |
| 584 | * |
| 585 | * Local Variables: |
| 586 | * eval: (c-set-style "gnu") |
| 587 | * End: |
| 588 | */ |