Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * teib.h: next-hop resolution |
| 3 | * |
| 4 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #include <vnet/teib/teib.h> |
| 20 | #include <vnet/fib/fib_table.h> |
| 21 | #include <vnet/adj/adj_midchain.h> |
| 22 | |
| 23 | typedef struct teib_key_t_ |
| 24 | { |
| 25 | ip46_address_t tk_peer; |
| 26 | u32 tk_sw_if_index; |
| 27 | } teib_key_t; |
| 28 | |
| 29 | struct teib_entry_t_ |
| 30 | { |
| 31 | teib_key_t *te_key; |
| 32 | fib_prefix_t te_nh; |
| 33 | u32 te_fib_index; |
| 34 | }; |
| 35 | |
| 36 | static uword *teib_db; |
| 37 | static teib_entry_t *teib_pool; |
| 38 | static teib_vft_t *teib_vfts; |
| 39 | |
| 40 | #define TEIB_NOTIFY(_te, _fn) { \ |
| 41 | teib_vft_t *_vft; \ |
| 42 | vec_foreach(_vft, teib_vfts) { \ |
| 43 | if (_vft->_fn) { \ |
| 44 | _vft->_fn(_te); \ |
| 45 | } \ |
| 46 | } \ |
| 47 | } |
| 48 | |
| 49 | u32 |
| 50 | teib_entry_get_sw_if_index (const teib_entry_t * te) |
| 51 | { |
| 52 | return (te->te_key->tk_sw_if_index); |
| 53 | } |
| 54 | |
| 55 | u32 |
| 56 | teib_entry_get_fib_index (const teib_entry_t * te) |
| 57 | { |
| 58 | return (te->te_fib_index); |
| 59 | } |
| 60 | |
| 61 | const ip46_address_t * |
| 62 | teib_entry_get_peer (const teib_entry_t * te) |
| 63 | { |
| 64 | return (&te->te_key->tk_peer); |
| 65 | } |
| 66 | |
| 67 | const fib_prefix_t * |
| 68 | teib_entry_get_nh (const teib_entry_t * te) |
| 69 | { |
| 70 | return (&te->te_nh); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | teib_entry_adj_stack (const teib_entry_t * te, adj_index_t ai) |
| 75 | { |
| 76 | adj_midchain_delegate_stack (ai, te->te_fib_index, &te->te_nh); |
| 77 | } |
| 78 | |
| 79 | teib_entry_t * |
| 80 | teib_entry_get (index_t tei) |
| 81 | { |
| 82 | return pool_elt_at_index (teib_pool, tei); |
| 83 | } |
| 84 | |
| 85 | teib_entry_t * |
| 86 | teib_entry_find (u32 sw_if_index, const ip46_address_t * peer) |
| 87 | { |
| 88 | teib_key_t nk = { |
| 89 | .tk_peer = *peer, |
| 90 | .tk_sw_if_index = sw_if_index, |
| 91 | }; |
| 92 | uword *p; |
| 93 | |
| 94 | p = hash_get_mem (teib_db, &nk); |
| 95 | |
| 96 | if (NULL != p) |
| 97 | return teib_entry_get (p[0]); |
| 98 | |
| 99 | return (NULL); |
| 100 | } |
| 101 | |
| 102 | int |
| 103 | teib_entry_add (u32 sw_if_index, |
| 104 | const ip46_address_t * peer, |
| 105 | u32 nh_table_id, const ip46_address_t * nh) |
| 106 | { |
| 107 | fib_protocol_t fproto; |
| 108 | teib_entry_t *te; |
| 109 | u32 fib_index; |
| 110 | index_t tei; |
| 111 | |
| 112 | fproto = (ip46_address_is_ip4 (nh) ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6); |
| 113 | |
| 114 | fib_index = fib_table_find (fproto, nh_table_id); |
| 115 | |
| 116 | if (~0 == fib_index) |
| 117 | { |
| 118 | return (VNET_API_ERROR_NO_SUCH_FIB); |
| 119 | } |
| 120 | |
| 121 | te = teib_entry_find (sw_if_index, peer); |
| 122 | |
| 123 | if (NULL == te) |
| 124 | { |
| 125 | teib_key_t nk = { |
| 126 | .tk_peer = *peer, |
| 127 | .tk_sw_if_index = sw_if_index, |
| 128 | }; |
| 129 | teib_entry_t *te; |
| 130 | |
| 131 | pool_get_zero (teib_pool, te); |
| 132 | |
| 133 | tei = te - teib_pool; |
| 134 | te->te_key = clib_mem_alloc (sizeof (*te->te_key)); |
| 135 | clib_memcpy (te->te_key, &nk, sizeof (*te->te_key)); |
| 136 | |
| 137 | ip46_address_copy (&te->te_nh.fp_addr, nh); |
| 138 | te->te_nh.fp_proto = fproto; |
| 139 | te->te_nh.fp_len = (te->te_nh.fp_proto == FIB_PROTOCOL_IP4 ? 32 : 128); |
| 140 | te->te_fib_index = fib_index; |
| 141 | |
| 142 | hash_set_mem (teib_db, te->te_key, tei); |
| 143 | |
| 144 | TEIB_NOTIFY (te, nv_added); |
| 145 | } |
| 146 | else |
| 147 | return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | int |
| 153 | teib_entry_del (u32 sw_if_index, const ip46_address_t * peer) |
| 154 | { |
| 155 | teib_entry_t *te; |
| 156 | |
| 157 | te = teib_entry_find (sw_if_index, peer); |
| 158 | |
| 159 | if (te != NULL) |
| 160 | { |
| 161 | hash_unset_mem (teib_db, te->te_key); |
| 162 | |
| 163 | TEIB_NOTIFY (te, nv_deleted); |
| 164 | |
| 165 | clib_mem_free (te->te_key); |
| 166 | pool_put (teib_pool, te); |
| 167 | } |
| 168 | else |
| 169 | return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | u8 * |
| 175 | format_teib_entry (u8 * s, va_list * args) |
| 176 | { |
| 177 | index_t tei = va_arg (*args, index_t); |
| 178 | vnet_main_t *vnm = vnet_get_main (); |
| 179 | teib_entry_t *te; |
| 180 | |
| 181 | te = teib_entry_get (tei); |
| 182 | |
| 183 | s = format (s, "[%d] ", tei); |
| 184 | s = format (s, "%U:", format_vnet_sw_if_index_name, |
| 185 | vnm, te->te_key->tk_sw_if_index); |
| 186 | s = format (s, " %U", format_ip46_address, |
| 187 | &te->te_key->tk_peer, IP46_TYPE_ANY); |
| 188 | s = format (s, " via [%d]:%U", |
| 189 | fib_table_get_table_id (te->te_fib_index, te->te_nh.fp_proto), |
| 190 | format_fib_prefix, &te->te_nh); |
| 191 | |
| 192 | return (s); |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | teib_walk (teib_walk_cb_t fn, void *ctx) |
| 197 | { |
| 198 | index_t tei; |
| 199 | |
| 200 | /* *INDENT-OFF* */ |
| 201 | pool_foreach_index(tei, teib_pool, |
| 202 | ({ |
| 203 | fn(tei, ctx); |
| 204 | })); |
| 205 | /* *INDENT-ON* */ |
| 206 | } |
| 207 | |
| 208 | void |
| 209 | teib_walk_itf (u32 sw_if_index, teib_walk_cb_t fn, void *ctx) |
| 210 | { |
| 211 | index_t tei; |
| 212 | |
| 213 | /* *INDENT-OFF* */ |
| 214 | pool_foreach_index(tei, teib_pool, |
| 215 | ({ |
| 216 | if (sw_if_index == teib_entry_get_sw_if_index(teib_entry_get(tei))) |
| 217 | fn(tei, ctx); |
| 218 | })); |
| 219 | /* *INDENT-ON* */ |
| 220 | } |
| 221 | |
| 222 | void |
| 223 | teib_register (const teib_vft_t * vft) |
| 224 | { |
| 225 | vec_add1 (teib_vfts, *vft); |
| 226 | } |
| 227 | |
| 228 | static clib_error_t * |
| 229 | teib_init (vlib_main_t * vm) |
| 230 | { |
| 231 | teib_db = hash_create_mem (0, sizeof (teib_key_t), sizeof (u32)); |
| 232 | |
| 233 | return (NULL); |
| 234 | } |
| 235 | |
| 236 | VLIB_INIT_FUNCTION (teib_init); |
| 237 | |
| 238 | /* |
| 239 | * fd.io coding-style-patch-verification: ON |
| 240 | * |
| 241 | * Local Variables: |
| 242 | * eval: (c-set-style "gnu") |
| 243 | * End: |
| 244 | */ |