Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | * nhrp.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/nhrp/nhrp.h> |
| 20 | #include <vnet/fib/fib_table.h> |
| 21 | #include <vnet/adj/adj_midchain.h> |
| 22 | |
Neale Ranns | 4c16d80 | 2019-12-17 20:15:03 +0000 | [diff] [blame] | 23 | typedef struct nhrp_key_t_ |
| 24 | { |
| 25 | ip46_address_t nk_peer; |
| 26 | u32 nk_sw_if_index; |
| 27 | } nhrp_key_t; |
| 28 | |
| 29 | struct nhrp_entry_t_ |
| 30 | { |
| 31 | nhrp_key_t *ne_key; |
| 32 | fib_prefix_t ne_nh; |
| 33 | u32 ne_fib_index; |
| 34 | }; |
| 35 | |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 36 | static uword *nhrp_db; |
| 37 | static nhrp_entry_t *nhrp_pool; |
Neale Ranns | 4c16d80 | 2019-12-17 20:15:03 +0000 | [diff] [blame] | 38 | static nhrp_vft_t *nhrp_vfts; |
| 39 | |
| 40 | #define NHRP_NOTIFY(_ne, _fn) { \ |
| 41 | nhrp_vft_t *_vft; \ |
| 42 | vec_foreach(_vft, nhrp_vfts) { \ |
| 43 | if (_vft->_fn) { \ |
| 44 | _vft->_fn(_ne); \ |
| 45 | } \ |
| 46 | } \ |
| 47 | } |
| 48 | |
| 49 | u32 |
| 50 | nhrp_entry_get_sw_if_index (const nhrp_entry_t * ne) |
| 51 | { |
| 52 | return (ne->ne_key->nk_sw_if_index); |
| 53 | } |
| 54 | |
| 55 | u32 |
| 56 | nhrp_entry_get_fib_index (const nhrp_entry_t * ne) |
| 57 | { |
| 58 | return (ne->ne_fib_index); |
| 59 | } |
| 60 | |
| 61 | const ip46_address_t * |
| 62 | nhrp_entry_get_peer (const nhrp_entry_t * ne) |
| 63 | { |
| 64 | return (&ne->ne_key->nk_peer); |
| 65 | } |
| 66 | |
| 67 | const fib_prefix_t * |
| 68 | nhrp_entry_get_nh (const nhrp_entry_t * ne) |
| 69 | { |
| 70 | return (&ne->ne_nh); |
| 71 | } |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 72 | |
| 73 | void |
| 74 | nhrp_entry_adj_stack (const nhrp_entry_t * ne, adj_index_t ai) |
| 75 | { |
| 76 | adj_midchain_delegate_stack (ai, ne->ne_fib_index, &ne->ne_nh); |
| 77 | } |
| 78 | |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 79 | nhrp_entry_t * |
| 80 | nhrp_entry_get (index_t nei) |
| 81 | { |
| 82 | return pool_elt_at_index (nhrp_pool, nei); |
| 83 | } |
| 84 | |
| 85 | nhrp_entry_t * |
| 86 | nhrp_entry_find (u32 sw_if_index, const ip46_address_t * peer) |
| 87 | { |
| 88 | nhrp_key_t nk = { |
| 89 | .nk_peer = *peer, |
| 90 | .nk_sw_if_index = sw_if_index, |
| 91 | }; |
| 92 | uword *p; |
| 93 | |
| 94 | p = hash_get_mem (nhrp_db, &nk); |
| 95 | |
| 96 | if (NULL != p) |
| 97 | return nhrp_entry_get (p[0]); |
| 98 | |
| 99 | return (NULL); |
| 100 | } |
| 101 | |
| 102 | int |
| 103 | nhrp_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 | nhrp_entry_t *ne; |
| 109 | u32 fib_index; |
| 110 | index_t nei; |
| 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 | ne = nhrp_entry_find (sw_if_index, peer); |
| 122 | |
| 123 | if (NULL == ne) |
| 124 | { |
| 125 | nhrp_key_t nk = { |
| 126 | .nk_peer = *peer, |
| 127 | .nk_sw_if_index = sw_if_index, |
| 128 | }; |
| 129 | nhrp_entry_t *ne; |
| 130 | |
| 131 | pool_get_zero (nhrp_pool, ne); |
| 132 | |
| 133 | nei = ne - nhrp_pool; |
| 134 | ne->ne_key = clib_mem_alloc (sizeof (*ne->ne_key)); |
| 135 | clib_memcpy (ne->ne_key, &nk, sizeof (*ne->ne_key)); |
| 136 | |
| 137 | ip46_address_copy (&ne->ne_nh.fp_addr, nh); |
| 138 | ne->ne_nh.fp_proto = fproto; |
| 139 | ne->ne_nh.fp_len = (ne->ne_nh.fp_proto == FIB_PROTOCOL_IP4 ? 32 : 128); |
| 140 | ne->ne_fib_index = fib_index; |
| 141 | |
| 142 | hash_set_mem (nhrp_db, ne->ne_key, nei); |
| 143 | |
Neale Ranns | 4c16d80 | 2019-12-17 20:15:03 +0000 | [diff] [blame] | 144 | NHRP_NOTIFY (ne, nv_added); |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 145 | } |
| 146 | else |
| 147 | return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | int |
| 153 | nhrp_entry_del (u32 sw_if_index, const ip46_address_t * peer) |
| 154 | { |
| 155 | nhrp_entry_t *ne; |
| 156 | |
| 157 | ne = nhrp_entry_find (sw_if_index, peer); |
| 158 | |
| 159 | if (ne != NULL) |
| 160 | { |
| 161 | hash_unset_mem (nhrp_db, ne->ne_key); |
| 162 | |
Neale Ranns | 4c16d80 | 2019-12-17 20:15:03 +0000 | [diff] [blame] | 163 | NHRP_NOTIFY (ne, nv_deleted); |
| 164 | |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 165 | clib_mem_free (ne->ne_key); |
| 166 | pool_put (nhrp_pool, ne); |
| 167 | } |
| 168 | else |
| 169 | return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | u8 * |
| 175 | format_nhrp_entry (u8 * s, va_list * args) |
| 176 | { |
| 177 | index_t nei = va_arg (*args, index_t); |
| 178 | vnet_main_t *vnm = vnet_get_main (); |
| 179 | nhrp_entry_t *ne; |
| 180 | |
| 181 | ne = nhrp_entry_get (nei); |
| 182 | |
| 183 | s = format (s, "[%d] ", nei); |
Neale Ranns | e11dce2 | 2019-12-17 00:14:26 +0000 | [diff] [blame] | 184 | s = format (s, "%U:", format_vnet_sw_if_index_name, |
| 185 | vnm, ne->ne_key->nk_sw_if_index); |
| 186 | s = format (s, " %U", format_ip46_address, |
| 187 | &ne->ne_key->nk_peer, IP46_TYPE_ANY); |
| 188 | s = format (s, " via [%d]:%U", |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 189 | fib_table_get_table_id (ne->ne_fib_index, ne->ne_nh.fp_proto), |
| 190 | format_fib_prefix, &ne->ne_nh); |
| 191 | |
| 192 | return (s); |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | nhrp_walk (nhrp_walk_cb_t fn, void *ctx) |
| 197 | { |
| 198 | index_t nei; |
| 199 | |
| 200 | /* *INDENT-OFF* */ |
| 201 | pool_foreach_index(nei, nhrp_pool, |
| 202 | ({ |
| 203 | fn(nei, ctx); |
| 204 | })); |
| 205 | /* *INDENT-ON* */ |
| 206 | } |
| 207 | |
Neale Ranns | 4c16d80 | 2019-12-17 20:15:03 +0000 | [diff] [blame] | 208 | void |
| 209 | nhrp_walk_itf (u32 sw_if_index, nhrp_walk_cb_t fn, void *ctx) |
| 210 | { |
| 211 | index_t nei; |
| 212 | |
| 213 | /* *INDENT-OFF* */ |
| 214 | pool_foreach_index(nei, nhrp_pool, |
| 215 | ({ |
| 216 | if (sw_if_index == nhrp_entry_get_sw_if_index(nhrp_entry_get(nei))) |
| 217 | fn(nei, ctx); |
| 218 | })); |
| 219 | /* *INDENT-ON* */ |
| 220 | } |
| 221 | |
| 222 | void |
| 223 | nhrp_register (const nhrp_vft_t * vft) |
| 224 | { |
| 225 | vec_add1 (nhrp_vfts, *vft); |
| 226 | } |
| 227 | |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 228 | static clib_error_t * |
| 229 | nhrp_init (vlib_main_t * vm) |
| 230 | { |
| 231 | nhrp_db = hash_create_mem (0, sizeof (nhrp_key_t), sizeof (u32)); |
| 232 | |
| 233 | return (NULL); |
| 234 | } |
| 235 | |
| 236 | VLIB_INIT_FUNCTION (nhrp_init); |
| 237 | |
| 238 | /* |
| 239 | * fd.io coding-style-patch-verification: ON |
| 240 | * |
| 241 | * Local Variables: |
| 242 | * eval: (c-set-style "gnu") |
| 243 | * End: |
| 244 | */ |