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