Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * bier_api.c - vnet BIER 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/api_errno.h> |
| 24 | #include <vnet/bier/bier_table.h> |
| 25 | #include <vnet/bier/bier_imp.h> |
| 26 | #include <vnet/bier/bier_disp_table.h> |
| 27 | #include <vnet/bier/bier_disp_entry.h> |
| 28 | #include <vnet/bier/bier_bit_string.h> |
| 29 | #include <vnet/bier/bier_hdr_inlines.h> |
| 30 | #include <vnet/fib/fib_path_list.h> |
| 31 | #include <vnet/fib/fib_api.h> |
| 32 | #include <vnet/fib/fib_table.h> |
| 33 | #include <vnet/mfib/mfib_table.h> |
| 34 | |
| 35 | #include <vnet/vnet_msg_enum.h> |
| 36 | |
| 37 | #define vl_typedefs /* define message structures */ |
| 38 | #include <vnet/vnet_all_api_h.h> |
| 39 | #undef vl_typedefs |
| 40 | |
| 41 | #define vl_endianfun /* define message structures */ |
| 42 | #include <vnet/vnet_all_api_h.h> |
| 43 | #undef vl_endianfun |
| 44 | |
| 45 | /* instantiate all the print functions we know about */ |
| 46 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 47 | #define vl_printfun |
| 48 | #include <vnet/vnet_all_api_h.h> |
| 49 | #undef vl_printfun |
| 50 | |
| 51 | #include <vlibapi/api_helper_macros.h> |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 52 | #include <vnet/fib/fib_api.h> |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 53 | |
| 54 | #define foreach_bier_api_msg \ |
| 55 | _(BIER_TABLE_ADD_DEL, bier_table_add_del) \ |
| 56 | _(BIER_TABLE_DUMP, bier_table_dump) \ |
| 57 | _(BIER_ROUTE_ADD_DEL, bier_route_add_del) \ |
| 58 | _(BIER_ROUTE_DUMP, bier_route_dump) \ |
| 59 | _(BIER_IMP_ADD, bier_imp_add) \ |
| 60 | _(BIER_IMP_DEL, bier_imp_del) \ |
| 61 | _(BIER_IMP_DUMP, bier_imp_dump) \ |
| 62 | _(BIER_DISP_TABLE_ADD_DEL, bier_disp_table_add_del) \ |
| 63 | _(BIER_DISP_TABLE_DUMP, bier_disp_table_dump) \ |
| 64 | _(BIER_DISP_ENTRY_ADD_DEL, bier_disp_entry_add_del) \ |
| 65 | _(BIER_DISP_ENTRY_DUMP, bier_disp_entry_dump) |
| 66 | |
| 67 | static void |
| 68 | vl_api_bier_table_add_del_t_handler (vl_api_bier_table_add_del_t * mp) |
| 69 | { |
| 70 | vl_api_bier_table_add_del_reply_t *rmp; |
| 71 | vnet_main_t *vnm; |
| 72 | int rv; |
| 73 | |
| 74 | vnm = vnet_get_main (); |
| 75 | vnm->api_errno = 0; |
| 76 | |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 77 | if (mp->bt_tbl_id.bt_hdr_len_id >= BIER_HDR_LEN_2048) |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 78 | { |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 79 | rv = VNET_API_ERROR_BIER_BSL_UNSUP; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 80 | } |
| 81 | else |
| 82 | { |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 83 | bier_table_id_t bti = { |
| 84 | .bti_set = mp->bt_tbl_id.bt_set, |
| 85 | .bti_sub_domain = mp->bt_tbl_id.bt_sub_domain, |
| 86 | .bti_hdr_len = mp->bt_tbl_id.bt_hdr_len_id, |
| 87 | .bti_type = BIER_TABLE_MPLS_SPF, |
| 88 | .bti_ecmp = BIER_ECMP_TABLE_ID_MAIN, |
| 89 | }; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 90 | |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 91 | if (mp->bt_is_add) |
| 92 | { |
| 93 | mpls_label_t label = ntohl(mp->bt_label); |
| 94 | |
| 95 | /* |
| 96 | * convert acceptable 'don't want a label' values from |
| 97 | * the API to the correct internal INVLID value |
| 98 | */ |
| 99 | if ((0 == label) || (~0 == label)) |
| 100 | { |
| 101 | label = MPLS_LABEL_INVALID; |
| 102 | } |
| 103 | bier_table_add_or_lock(&bti, label); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | bier_table_unlock(&bti); |
| 108 | } |
| 109 | |
| 110 | rv = vnm->api_errno; |
| 111 | } |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 112 | |
| 113 | REPLY_MACRO (VL_API_BIER_TABLE_ADD_DEL_REPLY); |
| 114 | } |
| 115 | |
| 116 | static void |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 117 | send_bier_table_details (vl_api_registration_t * reg, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 118 | u32 context, |
| 119 | const bier_table_t *bt) |
| 120 | { |
| 121 | vl_api_bier_table_details_t *mp; |
| 122 | |
| 123 | mp = vl_msg_api_alloc(sizeof(*mp)); |
| 124 | if (!mp) |
| 125 | return; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 126 | clib_memset(mp, 0, sizeof(*mp)); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 127 | mp->_vl_msg_id = ntohs(VL_API_BIER_TABLE_DETAILS); |
| 128 | mp->context = context; |
| 129 | |
| 130 | mp->bt_label = bt->bt_ll; |
| 131 | mp->bt_tbl_id.bt_set = bt->bt_id.bti_set; |
| 132 | mp->bt_tbl_id.bt_sub_domain = bt->bt_id.bti_sub_domain; |
| 133 | mp->bt_tbl_id.bt_hdr_len_id = bt->bt_id.bti_hdr_len; |
| 134 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 135 | vl_api_send_msg (reg, (u8 *) mp); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static void |
| 139 | vl_api_bier_table_dump_t_handler (vl_api_bier_table_dump_t * mp) |
| 140 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 141 | vl_api_registration_t *reg; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 142 | bier_table_t *bt; |
| 143 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 144 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 145 | if (!reg) |
| 146 | return; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 147 | |
| 148 | pool_foreach(bt, bier_table_pool, |
| 149 | ({ |
| 150 | /* |
| 151 | * skip the ecmp tables. |
| 152 | */ |
| 153 | if (bier_table_is_main(bt)) |
| 154 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 155 | send_bier_table_details(reg, mp->context, bt); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 156 | } |
| 157 | })); |
| 158 | } |
| 159 | |
| 160 | static void |
| 161 | vl_api_bier_route_add_del_t_handler (vl_api_bier_route_add_del_t * mp) |
| 162 | { |
| 163 | vl_api_bier_route_add_del_reply_t *rmp; |
| 164 | fib_route_path_t *brpaths, *brpath; |
| 165 | vnet_main_t *vnm; |
| 166 | bier_bp_t bp; |
| 167 | int rv = 0; |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 168 | u8 ii; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 169 | |
| 170 | vnm = vnet_get_main (); |
| 171 | vnm->api_errno = 0; |
Neale Ranns | be302d7 | 2017-12-19 01:34:30 -0800 | [diff] [blame] | 172 | bp = ntohl(mp->br_bp); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 173 | brpaths = NULL; |
| 174 | |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 175 | if (mp->br_tbl_id.bt_hdr_len_id >= BIER_HDR_LEN_2048) |
| 176 | { |
| 177 | rv = VNET_API_ERROR_BIER_BSL_UNSUP; |
| 178 | goto done; |
| 179 | } |
Neale Ranns | be302d7 | 2017-12-19 01:34:30 -0800 | [diff] [blame] | 180 | if (0 == bp || bp > BIER_BP_MAX) |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 181 | { |
| 182 | rv = -1; |
| 183 | goto done; |
| 184 | } |
| 185 | |
| 186 | bier_table_id_t bti = { |
| 187 | .bti_set = mp->br_tbl_id.bt_set, |
| 188 | .bti_sub_domain = mp->br_tbl_id.bt_sub_domain, |
| 189 | .bti_hdr_len = mp->br_tbl_id.bt_hdr_len_id, |
| 190 | .bti_type = BIER_TABLE_MPLS_SPF, |
| 191 | .bti_ecmp = BIER_ECMP_TABLE_ID_MAIN, |
| 192 | }; |
| 193 | |
| 194 | vec_validate(brpaths, mp->br_n_paths - 1); |
| 195 | |
| 196 | vec_foreach_index(ii, brpaths) |
| 197 | { |
| 198 | brpath = &brpaths[ii]; |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 199 | rv = fib_path_api_parse(&mp->br_paths[ii], brpath); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 200 | |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 201 | if (0 != rv) |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 202 | { |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 203 | goto done; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Neale Ranns | ef90ed0 | 2018-09-13 08:45:12 -0700 | [diff] [blame] | 207 | if (mp->br_is_replace) |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 208 | { |
Neale Ranns | ef90ed0 | 2018-09-13 08:45:12 -0700 | [diff] [blame] | 209 | if (0 == vec_len(brpaths)) |
| 210 | { |
| 211 | bier_table_route_delete(&bti, bp); |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | bier_table_route_path_update(&bti, bp, brpaths); |
| 216 | } |
| 217 | } |
| 218 | else if (mp->br_is_add) |
| 219 | { |
| 220 | bier_table_route_path_add(&bti, bp, brpaths); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 221 | } |
| 222 | else |
| 223 | { |
Neale Ranns | ef90ed0 | 2018-09-13 08:45:12 -0700 | [diff] [blame] | 224 | bier_table_route_path_remove(&bti, bp, brpaths); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 225 | } |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 226 | vec_free(brpaths); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 227 | |
| 228 | done: |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 229 | rv = (rv == 0) ? vnm->api_errno : rv; |
| 230 | |
| 231 | REPLY_MACRO (VL_API_BIER_ROUTE_ADD_DEL_REPLY); |
| 232 | } |
| 233 | |
| 234 | typedef struct bier_route_details_walk_t_ |
| 235 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 236 | vl_api_registration_t * reg; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 237 | u32 context; |
| 238 | } bier_route_details_walk_t; |
| 239 | |
| 240 | static void |
| 241 | send_bier_route_details (const bier_table_t *bt, |
| 242 | const bier_entry_t *be, |
| 243 | void *args) |
| 244 | { |
| 245 | fib_route_path_encode_t *api_rpaths = NULL, *api_rpath; |
| 246 | bier_route_details_walk_t *ctx = args; |
| 247 | vl_api_bier_route_details_t *mp; |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 248 | vl_api_fib_path_t *fp; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 249 | u32 n_paths, m_size; |
| 250 | |
| 251 | n_paths = fib_path_list_get_n_paths(be->be_path_list); |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 252 | m_size = sizeof(*mp) + (n_paths * sizeof(vl_api_fib_path_t)); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 253 | mp = vl_msg_api_alloc(m_size); |
| 254 | if (!mp) |
| 255 | return; |
| 256 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 257 | clib_memset(mp, 0, m_size); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 258 | mp->_vl_msg_id = ntohs(VL_API_BIER_ROUTE_DETAILS); |
| 259 | mp->context = ctx->context; |
| 260 | |
| 261 | mp->br_tbl_id.bt_set = bt->bt_id.bti_set; |
| 262 | mp->br_tbl_id.bt_sub_domain = bt->bt_id.bti_sub_domain; |
| 263 | mp->br_tbl_id.bt_hdr_len_id = bt->bt_id.bti_hdr_len; |
| 264 | mp->br_bp = htons(be->be_bp); |
| 265 | mp->br_n_paths = htonl(n_paths); |
| 266 | |
Neale Ranns | 775f73c | 2018-12-20 03:01:49 -0800 | [diff] [blame] | 267 | fib_path_list_walk_w_ext(be->be_path_list, |
| 268 | NULL, |
| 269 | fib_path_encode, |
| 270 | &api_rpaths); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 271 | |
| 272 | fp = mp->br_paths; |
| 273 | vec_foreach (api_rpath, api_rpaths) |
| 274 | { |
Neale Ranns | 8145842 | 2018-03-12 06:59:36 -0700 | [diff] [blame] | 275 | fib_api_path_encode(api_rpath, fp); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 276 | fp++; |
| 277 | } |
| 278 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 279 | vl_api_send_msg (ctx->reg, (u8 *) mp); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | static void |
| 283 | vl_api_bier_route_dump_t_handler (vl_api_bier_route_dump_t * mp) |
| 284 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 285 | vl_api_registration_t *reg; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 286 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 287 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 288 | if (!reg) |
| 289 | return; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 290 | |
| 291 | bier_table_id_t bti = { |
| 292 | .bti_set = mp->br_tbl_id.bt_set, |
| 293 | .bti_sub_domain = mp->br_tbl_id.bt_sub_domain, |
| 294 | .bti_hdr_len = mp->br_tbl_id.bt_hdr_len_id, |
| 295 | .bti_type = BIER_TABLE_MPLS_SPF, |
| 296 | .bti_ecmp = BIER_ECMP_TABLE_ID_MAIN, |
| 297 | }; |
| 298 | bier_route_details_walk_t ctx = { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 299 | .reg = reg, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 300 | .context = mp->context, |
| 301 | }; |
| 302 | bier_table_walk(&bti, send_bier_route_details, &ctx); |
| 303 | } |
| 304 | |
| 305 | static void |
| 306 | vl_api_bier_imp_add_t_handler (vl_api_bier_imp_add_t * mp) |
| 307 | { |
| 308 | vl_api_bier_imp_add_reply_t *rmp; |
| 309 | vnet_main_t *vnm; |
Neale Ranns | 18075a0 | 2018-04-05 02:24:52 -0700 | [diff] [blame] | 310 | index_t bii = ~0; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 311 | int rv = 0; |
| 312 | |
| 313 | vnm = vnet_get_main (); |
| 314 | vnm->api_errno = 0; |
| 315 | |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 316 | /* |
| 317 | * The BSL support by VPP is limited to the size of the |
| 318 | * available space in the vlib_buffer_t |
| 319 | */ |
| 320 | if (mp->bi_tbl_id.bt_hdr_len_id >= BIER_HDR_LEN_2048) |
| 321 | { |
| 322 | rv = VNET_API_ERROR_BIER_BSL_UNSUP; |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | bier_table_id_t bti = { |
| 327 | .bti_set = mp->bi_tbl_id.bt_set, |
| 328 | .bti_sub_domain = mp->bi_tbl_id.bt_sub_domain, |
| 329 | .bti_hdr_len = mp->bi_tbl_id.bt_hdr_len_id, |
| 330 | .bti_type = BIER_TABLE_MPLS_SPF, |
| 331 | .bti_ecmp = BIER_ECMP_TABLE_ID_MAIN, |
| 332 | }; |
| 333 | bier_bit_string_t bs = { |
| 334 | .bbs_len = mp->bi_n_bytes, |
| 335 | .bbs_buckets = mp->bi_bytes, |
| 336 | }; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 337 | |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 338 | bii = bier_imp_add_or_lock(&bti, ntohs(mp->bi_src), &bs); |
| 339 | } |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 340 | |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 341 | REPLY_MACRO2 (VL_API_BIER_IMP_ADD_REPLY, |
| 342 | ({ |
Neale Ranns | eea537a | 2018-01-09 04:11:28 -0800 | [diff] [blame] | 343 | rmp->bi_index = ntohl (bii); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 344 | })); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static void |
| 348 | vl_api_bier_imp_del_t_handler (vl_api_bier_imp_del_t * mp) |
| 349 | { |
| 350 | vl_api_bier_imp_del_reply_t *rmp; |
| 351 | vnet_main_t *vnm; |
| 352 | int rv = 0; |
| 353 | |
| 354 | vnm = vnet_get_main (); |
| 355 | vnm->api_errno = 0; |
| 356 | |
| 357 | bier_imp_unlock(ntohl(mp->bi_index)); |
| 358 | |
| 359 | REPLY_MACRO(VL_API_BIER_IMP_DEL_REPLY); |
| 360 | } |
| 361 | |
| 362 | static void |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 363 | send_bier_imp_details (vl_api_registration_t * reg, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 364 | u32 context, |
| 365 | const bier_imp_t *bi) |
| 366 | { |
| 367 | vl_api_bier_imp_details_t *mp; |
| 368 | bier_hdr_t copy; |
| 369 | u8 n_bytes; |
| 370 | |
| 371 | copy = bi->bi_hdr; |
| 372 | bier_hdr_ntoh(©); |
| 373 | |
| 374 | n_bytes = bier_hdr_len_id_to_num_bytes( |
| 375 | bier_hdr_get_len_id(©)); |
| 376 | mp = vl_msg_api_alloc(sizeof(*mp) + n_bytes); |
| 377 | if (!mp) |
| 378 | return; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 379 | clib_memset(mp, 0, sizeof(*mp)+n_bytes); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 380 | mp->_vl_msg_id = ntohs(VL_API_BIER_IMP_DETAILS); |
| 381 | mp->context = context; |
| 382 | |
| 383 | mp->bi_tbl_id.bt_set = bi->bi_tbl.bti_set; |
| 384 | mp->bi_tbl_id.bt_sub_domain = bi->bi_tbl.bti_sub_domain; |
| 385 | mp->bi_tbl_id.bt_hdr_len_id = bi->bi_tbl.bti_hdr_len; |
| 386 | |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 387 | mp->bi_src = htons(bier_hdr_get_src_id(©)); |
| 388 | mp->bi_n_bytes = n_bytes; |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 389 | memcpy(mp->bi_bytes, bi->bi_bits, n_bytes); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 390 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 391 | vl_api_send_msg (reg, (u8 *) mp); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static void |
| 395 | vl_api_bier_imp_dump_t_handler (vl_api_bier_imp_dump_t * mp) |
| 396 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 397 | vl_api_registration_t *reg; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 398 | bier_imp_t *bi; |
| 399 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 400 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 401 | if (!reg) |
| 402 | return; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 403 | |
| 404 | pool_foreach(bi, bier_imp_pool, |
| 405 | ({ |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 406 | send_bier_imp_details(reg, mp->context, bi); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 407 | })); |
| 408 | } |
| 409 | |
| 410 | static void |
| 411 | vl_api_bier_disp_table_add_del_t_handler (vl_api_bier_disp_table_add_del_t * mp) |
| 412 | { |
| 413 | vl_api_bier_disp_table_add_del_reply_t *rmp; |
| 414 | vnet_main_t *vnm; |
| 415 | u32 table_id; |
| 416 | int rv; |
| 417 | |
| 418 | vnm = vnet_get_main (); |
| 419 | vnm->api_errno = 0; |
| 420 | table_id = ntohl(mp->bdt_tbl_id); |
| 421 | |
| 422 | if (mp->bdt_is_add) |
| 423 | { |
| 424 | bier_disp_table_add_or_lock(table_id); |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | bier_disp_table_unlock_w_table_id(table_id); |
| 429 | } |
| 430 | |
| 431 | rv = vnm->api_errno; |
| 432 | |
| 433 | REPLY_MACRO (VL_API_BIER_DISP_TABLE_ADD_DEL_REPLY); |
| 434 | } |
| 435 | |
| 436 | static void |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 437 | send_bier_disp_table_details (vl_api_registration_t * reg, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 438 | u32 context, |
| 439 | const bier_disp_table_t *bdt) |
| 440 | { |
| 441 | vl_api_bier_disp_table_details_t *mp; |
| 442 | |
| 443 | mp = vl_msg_api_alloc(sizeof(*mp)); |
| 444 | if (!mp) |
| 445 | return; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 446 | clib_memset(mp, 0, sizeof(*mp)); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 447 | mp->_vl_msg_id = ntohs(VL_API_BIER_DISP_TABLE_DETAILS); |
| 448 | mp->context = context; |
| 449 | |
| 450 | mp->bdt_tbl_id = htonl(bdt->bdt_table_id); |
| 451 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 452 | vl_api_send_msg (reg, (u8 *) mp); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | static void |
| 456 | vl_api_bier_disp_table_dump_t_handler (vl_api_bier_disp_table_dump_t * mp) |
| 457 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 458 | vl_api_registration_t *reg; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 459 | bier_disp_table_t *bdt; |
| 460 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 461 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 462 | if (!reg) |
| 463 | return; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 464 | |
| 465 | pool_foreach(bdt, bier_disp_table_pool, |
| 466 | ({ |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 467 | send_bier_disp_table_details(reg, mp->context, bdt); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 468 | })); |
| 469 | } |
| 470 | |
| 471 | static void |
| 472 | vl_api_bier_disp_entry_add_del_t_handler (vl_api_bier_disp_entry_add_del_t * mp) |
| 473 | { |
| 474 | vl_api_bier_disp_entry_add_del_reply_t *rmp; |
| 475 | fib_route_path_t *brps = NULL, *brp; |
| 476 | vnet_main_t *vnm; |
| 477 | bier_bp_t bp; |
| 478 | u32 table_id; |
| 479 | int rv = 0; |
| 480 | u32 ii; |
| 481 | |
| 482 | vnm = vnet_get_main (); |
| 483 | vnm->api_errno = 0; |
| 484 | table_id = ntohl(mp->bde_tbl_id); |
| 485 | bp = ntohs(mp->bde_bp); |
| 486 | |
Neale Ranns | ceb4d05 | 2017-12-13 09:13:41 -0800 | [diff] [blame] | 487 | /* |
| 488 | * BP=0 is the default route |
| 489 | */ |
| 490 | if (bp > 0xffff) |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 491 | { |
| 492 | rv = -1; |
| 493 | goto done; |
| 494 | } |
| 495 | |
| 496 | vec_validate(brps, mp->bde_n_paths - 1); |
| 497 | vec_foreach_index(ii, brps) |
| 498 | { |
| 499 | brp = &brps[ii]; |
| 500 | brp->frp_fib_index = ntohl(mp->bde_paths[ii].table_id); |
| 501 | brp->frp_sw_if_index = ntohl(mp->bde_paths[ii].sw_if_index); |
| 502 | |
| 503 | if (~0 != ntohl(mp->bde_paths[ii].rpf_id)) |
| 504 | { |
| 505 | brp->frp_flags = FIB_ROUTE_PATH_RPF_ID; |
| 506 | brp->frp_rpf_id = ntohl(mp->bde_paths[ii].rpf_id); |
| 507 | } |
| 508 | |
| 509 | if (0 == mp->bde_paths[ii].afi) |
| 510 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 511 | clib_memcpy_fast (&brp->frp_addr.ip4, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 512 | mp->bde_paths[ii].next_hop, |
| 513 | sizeof (brp->frp_addr.ip4)); |
| 514 | } |
| 515 | else |
| 516 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 517 | clib_memcpy_fast (&brp->frp_addr.ip6, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 518 | mp->bde_paths[ii].next_hop, |
| 519 | sizeof (brp->frp_addr.ip6)); |
| 520 | } |
| 521 | if (ip46_address_is_zero(&brp->frp_addr)) |
| 522 | { |
| 523 | index_t fti; |
| 524 | |
| 525 | switch (mp->bde_payload_proto) |
| 526 | { |
| 527 | case BIER_HDR_PROTO_INVALID: |
| 528 | case BIER_HDR_PROTO_MPLS_DOWN_STREAM: |
| 529 | case BIER_HDR_PROTO_MPLS_UP_STREAM: |
| 530 | case BIER_HDR_PROTO_ETHERNET: |
| 531 | case BIER_HDR_PROTO_VXLAN: |
| 532 | case BIER_HDR_PROTO_CTRL: |
| 533 | case BIER_HDR_PROTO_OAM: |
| 534 | rv = VNET_API_ERROR_UNSUPPORTED; |
| 535 | goto done; |
| 536 | break; |
| 537 | case BIER_HDR_PROTO_IPV4: |
| 538 | case BIER_HDR_PROTO_IPV6: |
| 539 | { |
| 540 | fib_protocol_t fproto; |
| 541 | |
| 542 | fproto = (mp->bde_payload_proto == BIER_HDR_PROTO_IPV4 ? |
| 543 | FIB_PROTOCOL_IP4 : |
| 544 | FIB_PROTOCOL_IP6); |
| 545 | |
| 546 | if (brp->frp_flags & FIB_ROUTE_PATH_RPF_ID) |
| 547 | { |
| 548 | fti = mfib_table_find (fproto, |
| 549 | ntohl (mp->bde_paths[ii].table_id)); |
| 550 | } |
| 551 | else |
| 552 | { |
| 553 | fti = fib_table_find (fproto, |
| 554 | ntohl (mp->bde_paths[ii].table_id)); |
| 555 | } |
| 556 | |
| 557 | if (INDEX_INVALID != fti) |
| 558 | { |
| 559 | brp->frp_fib_index = fti; |
| 560 | } |
| 561 | else |
| 562 | { |
| 563 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 564 | goto done; |
| 565 | } |
| 566 | break; |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | if (mp->bde_is_add) |
| 573 | { |
| 574 | bier_disp_table_entry_path_add(table_id, bp, |
| 575 | mp->bde_payload_proto, |
| 576 | brps); |
| 577 | } |
| 578 | else |
| 579 | { |
| 580 | bier_disp_table_entry_path_remove(table_id, bp, |
| 581 | mp->bde_payload_proto, |
| 582 | brps); |
| 583 | } |
| 584 | |
| 585 | done: |
| 586 | vec_free(brps); |
| 587 | rv = (rv == 0) ? vnm->api_errno : rv; |
| 588 | |
| 589 | REPLY_MACRO (VL_API_BIER_DISP_ENTRY_ADD_DEL_REPLY); |
| 590 | } |
| 591 | |
| 592 | typedef struct bier_disp_entry_details_walk_t_ |
| 593 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 594 | vl_api_registration_t * reg; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 595 | u32 context; |
| 596 | } bier_disp_entry_details_walk_t; |
| 597 | |
| 598 | static void |
| 599 | send_bier_disp_entry_details (const bier_disp_table_t *bdt, |
| 600 | const bier_disp_entry_t *bde, |
| 601 | u16 bp, |
| 602 | void *args) |
| 603 | { |
| 604 | fib_route_path_encode_t *api_rpaths = NULL, *api_rpath; |
| 605 | bier_disp_entry_details_walk_t *ctx = args; |
| 606 | vl_api_bier_disp_entry_details_t *mp; |
| 607 | bier_hdr_proto_id_t pproto; |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 608 | vl_api_fib_path_t *fp; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 609 | u32 n_paths, m_size; |
| 610 | |
| 611 | FOR_EACH_BIER_HDR_PROTO(pproto) |
| 612 | { |
| 613 | fib_node_index_t pl = bde->bde_pl[pproto]; |
| 614 | if (INDEX_INVALID != pl) |
| 615 | { |
| 616 | n_paths = fib_path_list_get_n_paths(pl); |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 617 | m_size = sizeof(*mp) + (n_paths * sizeof(vl_api_fib_path_t)); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 618 | mp = vl_msg_api_alloc(m_size); |
| 619 | if (!mp) |
| 620 | return; |
| 621 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 622 | clib_memset(mp, 0, m_size); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 623 | mp->_vl_msg_id = ntohs(VL_API_BIER_DISP_ENTRY_DETAILS); |
| 624 | mp->context = ctx->context; |
| 625 | |
| 626 | mp->bde_tbl_id = htonl(bdt->bdt_table_id); |
| 627 | mp->bde_n_paths = htonl(n_paths); |
| 628 | mp->bde_payload_proto = pproto; |
| 629 | mp->bde_bp = htons(bp); |
| 630 | |
Neale Ranns | 775f73c | 2018-12-20 03:01:49 -0800 | [diff] [blame] | 631 | fib_path_list_walk_w_ext(pl, |
| 632 | NULL, |
| 633 | fib_path_encode, |
| 634 | &api_rpaths); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 635 | |
| 636 | fp = mp->bde_paths; |
| 637 | vec_foreach (api_rpath, api_rpaths) |
| 638 | { |
Neale Ranns | 8145842 | 2018-03-12 06:59:36 -0700 | [diff] [blame] | 639 | fib_api_path_encode(api_rpath, fp); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 640 | fp++; |
| 641 | } |
| 642 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 643 | vl_api_send_msg (ctx->reg, (u8 *) mp); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | static void |
| 649 | vl_api_bier_disp_entry_dump_t_handler (vl_api_bier_disp_entry_dump_t * mp) |
| 650 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 651 | vl_api_registration_t *reg; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 652 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 653 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 654 | if (!reg) |
| 655 | return; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 656 | |
| 657 | bier_disp_entry_details_walk_t ctx = { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 658 | .reg = reg, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 659 | .context = mp->context, |
| 660 | }; |
| 661 | bier_disp_table_walk(ntohl(mp->bde_tbl_id), |
| 662 | send_bier_disp_entry_details, |
| 663 | &ctx); |
| 664 | } |
| 665 | |
| 666 | #define vl_msg_name_crc_list |
| 667 | #include <vnet/bier/bier.api.h> |
| 668 | #undef vl_msg_name_crc_list |
| 669 | |
| 670 | static void |
| 671 | setup_message_id_table (api_main_t * am) |
| 672 | { |
| 673 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 674 | foreach_vl_msg_name_crc_bier; |
| 675 | #undef _ |
| 676 | } |
| 677 | |
| 678 | static clib_error_t * |
| 679 | bier_api_hookup (vlib_main_t * vm) |
| 680 | { |
| 681 | api_main_t *am = &api_main; |
| 682 | |
| 683 | #define _(N,n) \ |
| 684 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 685 | vl_api_##n##_t_handler, \ |
| 686 | vl_noop_handler, \ |
| 687 | vl_api_##n##_t_endian, \ |
| 688 | vl_api_##n##_t_print, \ |
| 689 | sizeof(vl_api_##n##_t), 1); |
| 690 | foreach_bier_api_msg; |
| 691 | #undef _ |
| 692 | |
| 693 | /* |
| 694 | * Set up the (msg_name, crc, message-id) table |
| 695 | */ |
| 696 | setup_message_id_table (am); |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | VLIB_API_INIT_FUNCTION (bier_api_hookup); |