Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 16 | #include <vlibmemory/api.h> |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 17 | #include <vnet/lisp-cp/control.h> |
| 18 | #include <vnet/lisp-cp/packets.h> |
| 19 | #include <vnet/lisp-cp/lisp_msg_serdes.h> |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 20 | #include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h> |
| 21 | #include <vnet/lisp-gpe/lisp_gpe_tenant.h> |
Filip Tehlar | 4868ff6 | 2017-03-09 16:48:39 +0100 | [diff] [blame] | 22 | #include <vnet/lisp-gpe/lisp_gpe_tunnel.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 23 | #include <vnet/fib/fib_entry.h> |
| 24 | #include <vnet/fib/fib_table.h> |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 25 | #include <vnet/ethernet/arp_packet.h> |
| 26 | #include <vnet/ethernet/packet.h> |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 27 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 28 | #include <openssl/evp.h> |
| 29 | #include <openssl/hmac.h> |
| 30 | |
Filip Tehlar | ef2a5bf | 2017-05-30 07:14:46 +0200 | [diff] [blame] | 31 | #define MAX_VALUE_U24 0xffffff |
| 32 | |
Florin Coras | f3dc11a | 2017-01-24 03:13:43 -0800 | [diff] [blame] | 33 | lisp_cp_main_t lisp_control_main; |
| 34 | |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 35 | u8 *format_lisp_cp_input_trace (u8 * s, va_list * args); |
| 36 | |
| 37 | typedef enum |
| 38 | { |
| 39 | LISP_CP_INPUT_NEXT_DROP, |
| 40 | LISP_CP_INPUT_N_NEXT, |
| 41 | } lisp_cp_input_next_t; |
| 42 | |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 43 | typedef struct |
| 44 | { |
| 45 | u8 is_resend; |
| 46 | gid_address_t seid; |
| 47 | gid_address_t deid; |
| 48 | u8 smr_invoked; |
| 49 | } map_request_args_t; |
| 50 | |
Florin Coras | dca8804 | 2016-09-14 16:01:38 +0200 | [diff] [blame] | 51 | u8 |
| 52 | vnet_lisp_get_map_request_mode (void) |
| 53 | { |
| 54 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 55 | return lcm->map_request_mode; |
| 56 | } |
| 57 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 58 | static u16 |
| 59 | auth_data_len_by_key_id (lisp_key_type_t key_id) |
| 60 | { |
| 61 | switch (key_id) |
| 62 | { |
| 63 | case HMAC_SHA_1_96: |
| 64 | return SHA1_AUTH_DATA_LEN; |
| 65 | case HMAC_SHA_256_128: |
| 66 | return SHA256_AUTH_DATA_LEN; |
| 67 | default: |
| 68 | clib_warning ("unsupported key type: %d!", key_id); |
| 69 | return (u16) ~ 0; |
| 70 | } |
| 71 | return (u16) ~ 0; |
| 72 | } |
| 73 | |
| 74 | static const EVP_MD * |
| 75 | get_encrypt_fcn (lisp_key_type_t key_id) |
| 76 | { |
| 77 | switch (key_id) |
| 78 | { |
| 79 | case HMAC_SHA_1_96: |
| 80 | return EVP_sha1 (); |
| 81 | case HMAC_SHA_256_128: |
| 82 | return EVP_sha256 (); |
| 83 | default: |
| 84 | clib_warning ("unsupported encryption key type: %d!", key_id); |
| 85 | break; |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 90 | static int |
| 91 | queue_map_request (gid_address_t * seid, gid_address_t * deid, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 92 | u8 smr_invoked, u8 is_resend); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 93 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 94 | ip_interface_address_t * |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 95 | ip_interface_get_first_interface_address (ip_lookup_main_t * lm, |
| 96 | u32 sw_if_index, u8 loop) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 97 | { |
| 98 | vnet_main_t *vnm = vnet_get_main (); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 99 | vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 100 | if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) |
| 101 | sw_if_index = swif->unnumbered_sw_if_index; |
| 102 | u32 ia = |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 103 | (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ? |
| 104 | vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) : |
| 105 | (u32) ~ 0; |
| 106 | return pool_elt_at_index ((lm)->if_address_pool, ia); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 107 | } |
Filip Tehlar | 215104e | 2016-05-10 16:58:29 +0200 | [diff] [blame] | 108 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 109 | void * |
| 110 | ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 111 | u8 version) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 112 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 113 | ip_interface_address_t *ia; |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 114 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 115 | ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1); |
| 116 | if (!ia) |
| 117 | return 0; |
| 118 | return ip_interface_address_get_address (lm, ia); |
| 119 | } |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 120 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 121 | int |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 122 | ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 123 | u8 version, ip_address_t * result) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 124 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 125 | ip_lookup_main_t *lm; |
| 126 | void *addr; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 127 | |
| 128 | lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main; |
| 129 | addr = ip_interface_get_first_address (lm, sw_if_index, version); |
| 130 | if (!addr) |
| 131 | return 0; |
| 132 | |
| 133 | ip_address_set (result, addr, version); |
| 134 | return 1; |
| 135 | } |
| 136 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 137 | /** |
| 138 | * convert from a LISP address to a FIB prefix |
| 139 | */ |
| 140 | void |
| 141 | ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 142 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 143 | if (addr->version == IP4) |
| 144 | { |
| 145 | prefix->fp_len = 32; |
| 146 | prefix->fp_proto = FIB_PROTOCOL_IP4; |
| 147 | memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad)); |
| 148 | memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4)); |
| 149 | } |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 150 | else |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 151 | { |
| 152 | prefix->fp_len = 128; |
| 153 | prefix->fp_proto = FIB_PROTOCOL_IP6; |
| 154 | memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6)); |
| 155 | } |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 158 | /** |
| 159 | * convert from a LISP to a FIB prefix |
| 160 | */ |
| 161 | void |
| 162 | ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix, |
| 163 | fib_prefix_t * fib_prefix) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 164 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 165 | ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix); |
| 166 | fib_prefix->fp_len = ip_prefix->len; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Find the sw_if_index of the interface that would be used to egress towards |
| 171 | * dst. |
| 172 | */ |
| 173 | u32 |
| 174 | ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst) |
| 175 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 176 | fib_node_index_t fei; |
| 177 | fib_prefix_t prefix; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 178 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 179 | ip_address_to_fib_prefix (dst, &prefix); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 180 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 181 | fei = fib_table_lookup (0, &prefix); |
| 182 | |
| 183 | return (fib_entry_get_resolving_interface (fei)); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Find first IP of the interface that would be used to egress towards dst. |
| 188 | * Returns 1 if the address is found 0 otherwise. |
| 189 | */ |
| 190 | int |
| 191 | ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 192 | ip_address_t * result) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 193 | { |
| 194 | u32 si; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 195 | ip_lookup_main_t *lm; |
| 196 | void *addr = 0; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 197 | u8 ipver; |
| 198 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 199 | ASSERT (result != 0); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 200 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 201 | ipver = ip_addr_version (dst); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 202 | |
| 203 | lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 204 | si = ip_fib_get_egress_iface_for_dst (lcm, dst); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 205 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 206 | if ((u32) ~ 0 == si) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 207 | return 0; |
| 208 | |
| 209 | /* find the first ip address */ |
| 210 | addr = ip_interface_get_first_address (lm, si, ipver); |
| 211 | if (0 == addr) |
| 212 | return 0; |
| 213 | |
| 214 | ip_address_set (result, addr, ipver); |
| 215 | return 1; |
| 216 | } |
| 217 | |
| 218 | static int |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 219 | dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 220 | { |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 221 | uword *dp_table; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 222 | |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 223 | if (!is_l2) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 224 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 225 | dp_table = hash_get (lcm->table_id_by_vni, vni); |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 226 | |
| 227 | if (!dp_table) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 228 | { |
| 229 | clib_warning ("vni %d not associated to a vrf!", vni); |
| 230 | return VNET_API_ERROR_INVALID_VALUE; |
| 231 | } |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 232 | } |
| 233 | else |
| 234 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 235 | dp_table = hash_get (lcm->bd_id_by_vni, vni); |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 236 | if (!dp_table) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 237 | { |
| 238 | clib_warning ("vni %d not associated to a bridge domain!", vni); |
| 239 | return VNET_API_ERROR_INVALID_VALUE; |
| 240 | } |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 243 | /* enable/disable data-plane interface */ |
| 244 | if (is_add) |
| 245 | { |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 246 | if (is_l2) |
| 247 | lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]); |
| 248 | else |
| 249 | lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0]); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 250 | } |
| 251 | else |
| 252 | { |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 253 | if (is_l2) |
| 254 | lisp_gpe_tenant_l2_iface_unlock (vni); |
| 255 | else |
| 256 | lisp_gpe_tenant_l3_iface_unlock (vni); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static void |
| 263 | dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index) |
| 264 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 265 | vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a; |
| 266 | fwd_entry_t *fe = 0; |
| 267 | uword *feip = 0; |
| 268 | memset (a, 0, sizeof (*a)); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 269 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 270 | feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 271 | if (!feip) |
| 272 | return; |
| 273 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 274 | fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 275 | |
| 276 | /* delete dp fwd entry */ |
| 277 | u32 sw_if_index; |
| 278 | a->is_add = 0; |
Florin Coras | bb5c22f | 2016-08-02 02:31:03 +0200 | [diff] [blame] | 279 | a->locator_pairs = fe->locator_pairs; |
Filip Tehlar | 2fdaece | 2016-09-28 14:27:59 +0200 | [diff] [blame] | 280 | a->vni = gid_address_vni (&fe->reid); |
| 281 | gid_address_copy (&a->rmt_eid, &fe->reid); |
Filip Tehlar | d5fcc46 | 2016-10-17 16:20:18 +0200 | [diff] [blame] | 282 | if (fe->is_src_dst) |
| 283 | gid_address_copy (&a->lcl_eid, &fe->leid); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 284 | |
Filip Tehlar | 2151191 | 2017-04-07 10:41:42 +0200 | [diff] [blame] | 285 | vnet_lisp_gpe_del_fwd_counters (a, feip[0]); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 286 | vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index); |
| 287 | |
| 288 | /* delete entry in fwd table */ |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 289 | hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index); |
| 290 | vec_free (fe->locator_pairs); |
| 291 | pool_put (lcm->fwd_entry_pool, fe); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Finds first remote locator with best (lowest) priority that has a local |
| 296 | * peer locator with an underlying route to it. |
| 297 | * |
| 298 | */ |
| 299 | static u32 |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 300 | get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map, |
| 301 | mapping_t * rmt_map, locator_pair_t ** locator_pairs) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 302 | { |
Florin Coras | 3590ac5 | 2016-08-08 16:04:26 +0200 | [diff] [blame] | 303 | u32 i, limitp = 0, li, found = 0, esi; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 304 | locator_set_t *rmt_ls, *lcl_ls; |
| 305 | ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr; |
| 306 | locator_t *lp, *rmt = 0; |
| 307 | uword *checked = 0; |
Florin Coras | bb5c22f | 2016-08-02 02:31:03 +0200 | [diff] [blame] | 308 | locator_pair_t pair; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 309 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 310 | rmt_ls = |
| 311 | pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index); |
| 312 | lcl_ls = |
| 313 | pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 314 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 315 | if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 316 | return 0; |
| 317 | |
Florin Coras | 3590ac5 | 2016-08-08 16:04:26 +0200 | [diff] [blame] | 318 | while (1) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 319 | { |
| 320 | rmt = 0; |
| 321 | |
| 322 | /* find unvisited remote locator with best priority */ |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 323 | for (i = 0; i < vec_len (rmt_ls->locator_indices); i++) |
| 324 | { |
| 325 | if (0 != hash_get (checked, i)) |
| 326 | continue; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 327 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 328 | li = vec_elt (rmt_ls->locator_indices, i); |
| 329 | lp = pool_elt_at_index (lcm->locator_pool, li); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 330 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 331 | /* we don't support non-IP locators for now */ |
| 332 | if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX) |
| 333 | continue; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 334 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 335 | if ((found && lp->priority == limitp) |
| 336 | || (!found && lp->priority >= limitp)) |
| 337 | { |
| 338 | rmt = lp; |
Florin Coras | 3590ac5 | 2016-08-08 16:04:26 +0200 | [diff] [blame] | 339 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 340 | /* don't search for locators with lower priority and don't |
| 341 | * check this locator again*/ |
| 342 | limitp = lp->priority; |
| 343 | hash_set (checked, i, 1); |
| 344 | break; |
| 345 | } |
| 346 | } |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 347 | /* check if a local locator with a route to remote locator exists */ |
| 348 | if (rmt != 0) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 349 | { |
| 350 | /* find egress sw_if_index for rmt locator */ |
| 351 | esi = |
| 352 | ip_fib_get_egress_iface_for_dst (lcm, |
| 353 | &gid_address_ip (&rmt->address)); |
| 354 | if ((u32) ~ 0 == esi) |
| 355 | continue; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 356 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 357 | for (i = 0; i < vec_len (lcl_ls->locator_indices); i++) |
| 358 | { |
| 359 | li = vec_elt (lcl_ls->locator_indices, i); |
| 360 | locator_t *sl = pool_elt_at_index (lcm->locator_pool, li); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 361 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 362 | /* found local locator with the needed sw_if_index */ |
| 363 | if (sl->sw_if_index == esi) |
| 364 | { |
| 365 | /* and it has an address */ |
| 366 | if (0 == ip_interface_get_first_ip_address (lcm, |
| 367 | sl->sw_if_index, |
| 368 | gid_address_ip_version |
| 369 | (&rmt->address), |
| 370 | lcl_addr)) |
| 371 | continue; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 372 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 373 | memset (&pair, 0, sizeof (pair)); |
| 374 | ip_address_copy (&pair.rmt_loc, |
| 375 | &gid_address_ip (&rmt->address)); |
| 376 | ip_address_copy (&pair.lcl_loc, lcl_addr); |
| 377 | pair.weight = rmt->weight; |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 378 | pair.priority = rmt->priority; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 379 | vec_add1 (locator_pairs[0], pair); |
| 380 | found = 1; |
| 381 | } |
| 382 | } |
| 383 | } |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 384 | else |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 385 | break; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 386 | } |
Florin Coras | 3590ac5 | 2016-08-08 16:04:26 +0200 | [diff] [blame] | 387 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 388 | hash_free (checked); |
Florin Coras | 3590ac5 | 2016-08-08 16:04:26 +0200 | [diff] [blame] | 389 | return found; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static void |
Florin Coras | dca8804 | 2016-09-14 16:01:38 +0200 | [diff] [blame] | 393 | gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src, |
| 394 | fid_address_t * fid) |
| 395 | { |
| 396 | ASSERT (GID_ADDR_SRC_DST == gid_address_type (src)); |
| 397 | |
| 398 | dst[0] = src[0]; |
| 399 | |
| 400 | switch (fid_addr_type (fid)) |
| 401 | { |
| 402 | case FID_ADDR_IP_PREF: |
| 403 | gid_address_type (dst) = GID_ADDR_IP_PREFIX; |
| 404 | gid_address_ippref (dst) = fid_addr_ippref (fid); |
| 405 | break; |
| 406 | case FID_ADDR_MAC: |
| 407 | gid_address_type (dst) = GID_ADDR_MAC; |
| 408 | mac_copy (gid_address_mac (dst), fid_addr_mac (fid)); |
| 409 | break; |
| 410 | default: |
| 411 | clib_warning ("Unsupported fid type %d!", fid_addr_type (fid)); |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 416 | u8 |
| 417 | vnet_lisp_map_register_state_get (void) |
| 418 | { |
| 419 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 420 | return lcm->map_registering; |
| 421 | } |
| 422 | |
| 423 | u8 |
| 424 | vnet_lisp_rloc_probe_state_get (void) |
| 425 | { |
| 426 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 427 | return lcm->rloc_probing; |
| 428 | } |
| 429 | |
Florin Coras | dca8804 | 2016-09-14 16:01:38 +0200 | [diff] [blame] | 430 | static void |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 431 | dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 432 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 433 | vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a; |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 434 | gid_address_t *rmt_eid, *lcl_eid; |
| 435 | mapping_t *lcl_map, *rmt_map; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 436 | u32 sw_if_index; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 437 | uword *feip = 0, *dpid; |
| 438 | fwd_entry_t *fe; |
Filip Tehlar | 69a9b76 | 2016-09-23 10:00:52 +0200 | [diff] [blame] | 439 | u8 type, is_src_dst = 0; |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 440 | int rv; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 441 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 442 | memset (a, 0, sizeof (*a)); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 443 | |
| 444 | /* remove entry if it already exists */ |
| 445 | feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index); |
| 446 | if (feip) |
| 447 | dp_del_fwd_entry (lcm, src_map_index, dst_map_index); |
| 448 | |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 449 | /* |
| 450 | * Determine local mapping and eid |
| 451 | */ |
Filip Tehlar | f3e3fd3 | 2016-09-30 12:47:59 +0200 | [diff] [blame] | 452 | if (lcm->lisp_pitr) |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 453 | lcl_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index); |
Filip Tehlar | f3e3fd3 | 2016-09-30 12:47:59 +0200 | [diff] [blame] | 454 | else |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 455 | lcl_map = pool_elt_at_index (lcm->mapping_pool, src_map_index); |
| 456 | lcl_eid = &lcl_map->eid; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 457 | |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 458 | /* |
| 459 | * Determine remote mapping and eid |
| 460 | */ |
| 461 | rmt_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index); |
| 462 | rmt_eid = &rmt_map->eid; |
| 463 | |
| 464 | /* |
| 465 | * Build and insert data plane forwarding entry |
| 466 | */ |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 467 | a->is_add = 1; |
| 468 | |
Filip Tehlar | d5fcc46 | 2016-10-17 16:20:18 +0200 | [diff] [blame] | 469 | if (MR_MODE_SRC_DST == lcm->map_request_mode) |
Florin Coras | dca8804 | 2016-09-14 16:01:38 +0200 | [diff] [blame] | 470 | { |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 471 | if (GID_ADDR_SRC_DST == gid_address_type (rmt_eid)) |
Filip Tehlar | d5fcc46 | 2016-10-17 16:20:18 +0200 | [diff] [blame] | 472 | { |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 473 | gid_address_sd_to_flat (&a->rmt_eid, rmt_eid, |
| 474 | &gid_address_sd_dst (rmt_eid)); |
| 475 | gid_address_sd_to_flat (&a->lcl_eid, rmt_eid, |
| 476 | &gid_address_sd_src (rmt_eid)); |
Filip Tehlar | d5fcc46 | 2016-10-17 16:20:18 +0200 | [diff] [blame] | 477 | } |
| 478 | else |
| 479 | { |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 480 | gid_address_copy (&a->rmt_eid, rmt_eid); |
| 481 | gid_address_copy (&a->lcl_eid, lcl_eid); |
Filip Tehlar | d5fcc46 | 2016-10-17 16:20:18 +0200 | [diff] [blame] | 482 | } |
Filip Tehlar | 69a9b76 | 2016-09-23 10:00:52 +0200 | [diff] [blame] | 483 | is_src_dst = 1; |
Florin Coras | dca8804 | 2016-09-14 16:01:38 +0200 | [diff] [blame] | 484 | } |
| 485 | else |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 486 | gid_address_copy (&a->rmt_eid, rmt_eid); |
Florin Coras | dca8804 | 2016-09-14 16:01:38 +0200 | [diff] [blame] | 487 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 488 | a->vni = gid_address_vni (&a->rmt_eid); |
Filip Tehlar | ed6b52b | 2017-03-22 09:02:33 +0100 | [diff] [blame] | 489 | a->is_src_dst = is_src_dst; |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 490 | |
| 491 | /* get vrf or bd_index associated to vni */ |
Florin Coras | dca8804 | 2016-09-14 16:01:38 +0200 | [diff] [blame] | 492 | type = gid_address_type (&a->rmt_eid); |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 493 | if (GID_ADDR_IP_PREFIX == type) |
| 494 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 495 | dpid = hash_get (lcm->table_id_by_vni, a->vni); |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 496 | if (!dpid) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 497 | { |
| 498 | clib_warning ("vni %d not associated to a vrf!", a->vni); |
| 499 | return; |
| 500 | } |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 501 | a->table_id = dpid[0]; |
| 502 | } |
| 503 | else if (GID_ADDR_MAC == type) |
| 504 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 505 | dpid = hash_get (lcm->bd_id_by_vni, a->vni); |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 506 | if (!dpid) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 507 | { |
| 508 | clib_warning ("vni %d not associated to a bridge domain !", a->vni); |
| 509 | return; |
| 510 | } |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 511 | a->bd_id = dpid[0]; |
| 512 | } |
| 513 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 514 | /* find best locator pair that 1) verifies LISP policy 2) are connected */ |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 515 | rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs); |
| 516 | |
| 517 | /* Either rmt mapping is negative or we can't find underlay path. |
| 518 | * Try again with petr if configured */ |
| 519 | if (rv == 0 && (lcm->flags & LISP_FLAG_USE_PETR)) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 520 | { |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 521 | rmt_map = lisp_get_petr_mapping (lcm); |
| 522 | rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 523 | } |
| 524 | |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 525 | /* negative entry */ |
| 526 | if (rv == 0) |
| 527 | { |
| 528 | a->is_negative = 1; |
| 529 | a->action = rmt_map->action; |
| 530 | } |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 531 | |
Filip Tehlar | 2151191 | 2017-04-07 10:41:42 +0200 | [diff] [blame] | 532 | rv = vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index); |
| 533 | if (rv) |
| 534 | { |
| 535 | if (a->locator_pairs) |
| 536 | vec_free (a->locator_pairs); |
| 537 | return; |
| 538 | } |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 539 | |
Filip Tehlar | 2151191 | 2017-04-07 10:41:42 +0200 | [diff] [blame] | 540 | /* add tunnel to fwd entry table */ |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 541 | pool_get (lcm->fwd_entry_pool, fe); |
Filip Tehlar | 2151191 | 2017-04-07 10:41:42 +0200 | [diff] [blame] | 542 | vnet_lisp_gpe_add_fwd_counters (a, fe - lcm->fwd_entry_pool); |
| 543 | |
Florin Coras | bb5c22f | 2016-08-02 02:31:03 +0200 | [diff] [blame] | 544 | fe->locator_pairs = a->locator_pairs; |
Filip Tehlar | 2fdaece | 2016-09-28 14:27:59 +0200 | [diff] [blame] | 545 | gid_address_copy (&fe->reid, &a->rmt_eid); |
Filip Tehlar | b601f22 | 2017-01-02 10:22:56 +0100 | [diff] [blame] | 546 | |
| 547 | if (is_src_dst) |
| 548 | gid_address_copy (&fe->leid, &a->lcl_eid); |
| 549 | else |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 550 | gid_address_copy (&fe->leid, lcl_eid); |
Filip Tehlar | b601f22 | 2017-01-02 10:22:56 +0100 | [diff] [blame] | 551 | |
Filip Tehlar | 69a9b76 | 2016-09-23 10:00:52 +0200 | [diff] [blame] | 552 | fe->is_src_dst = is_src_dst; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 553 | hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 554 | fe - lcm->fwd_entry_pool); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 555 | } |
| 556 | |
Filip Tehlar | ce1aae4 | 2017-01-04 10:42:25 +0100 | [diff] [blame] | 557 | typedef struct |
| 558 | { |
| 559 | u32 si; |
| 560 | u32 di; |
| 561 | } fwd_entry_mt_arg_t; |
| 562 | |
| 563 | static void * |
| 564 | dp_add_fwd_entry_thread_fn (void *arg) |
| 565 | { |
| 566 | fwd_entry_mt_arg_t *a = arg; |
| 567 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 568 | dp_add_fwd_entry (lcm, a->si, a->di); |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | static int |
| 573 | dp_add_fwd_entry_from_mt (u32 si, u32 di) |
| 574 | { |
| 575 | fwd_entry_mt_arg_t a; |
| 576 | |
| 577 | memset (&a, 0, sizeof (a)); |
| 578 | a.si = si; |
| 579 | a.di = di; |
| 580 | |
| 581 | vl_api_rpc_call_main_thread (dp_add_fwd_entry_thread_fn, |
| 582 | (u8 *) & a, sizeof (a)); |
| 583 | return 0; |
| 584 | } |
| 585 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 586 | /** |
Filip Tehlar | 69a9b76 | 2016-09-23 10:00:52 +0200 | [diff] [blame] | 587 | * Returns vector of adjacencies. |
| 588 | * |
| 589 | * The caller must free the vector returned by this function. |
| 590 | * |
| 591 | * @param vni virtual network identifier |
| 592 | * @return vector of adjacencies |
| 593 | */ |
| 594 | lisp_adjacency_t * |
| 595 | vnet_lisp_adjacencies_get_by_vni (u32 vni) |
| 596 | { |
| 597 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 598 | fwd_entry_t *fwd; |
| 599 | lisp_adjacency_t *adjs = 0, adj; |
| 600 | |
| 601 | /* *INDENT-OFF* */ |
| 602 | pool_foreach(fwd, lcm->fwd_entry_pool, |
| 603 | ({ |
| 604 | if (gid_address_vni (&fwd->reid) != vni) |
| 605 | continue; |
| 606 | |
| 607 | gid_address_copy (&adj.reid, &fwd->reid); |
| 608 | gid_address_copy (&adj.leid, &fwd->leid); |
| 609 | vec_add1 (adjs, adj); |
| 610 | })); |
| 611 | /* *INDENT-ON* */ |
| 612 | |
| 613 | return adjs; |
| 614 | } |
| 615 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 616 | static lisp_msmr_t * |
| 617 | get_map_server (ip_address_t * a) |
| 618 | { |
| 619 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 620 | lisp_msmr_t *m; |
| 621 | |
| 622 | vec_foreach (m, lcm->map_servers) |
| 623 | { |
| 624 | if (!ip_address_cmp (&m->address, a)) |
| 625 | { |
| 626 | return m; |
| 627 | } |
| 628 | } |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | static lisp_msmr_t * |
| 633 | get_map_resolver (ip_address_t * a) |
| 634 | { |
| 635 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 636 | lisp_msmr_t *m; |
| 637 | |
| 638 | vec_foreach (m, lcm->map_resolvers) |
| 639 | { |
| 640 | if (!ip_address_cmp (&m->address, a)) |
| 641 | { |
| 642 | return m; |
| 643 | } |
| 644 | } |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | int |
| 649 | vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add) |
| 650 | { |
| 651 | u32 i; |
| 652 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 653 | lisp_msmr_t _ms, *ms = &_ms; |
| 654 | |
| 655 | if (vnet_lisp_enable_disable_status () == 0) |
| 656 | { |
| 657 | clib_warning ("LISP is disabled!"); |
| 658 | return VNET_API_ERROR_LISP_DISABLED; |
| 659 | } |
| 660 | |
| 661 | if (is_add) |
| 662 | { |
| 663 | if (get_map_server (addr)) |
| 664 | { |
| 665 | clib_warning ("map-server %U already exists!", format_ip_address, |
| 666 | addr); |
| 667 | return -1; |
| 668 | } |
| 669 | |
| 670 | memset (ms, 0, sizeof (*ms)); |
| 671 | ip_address_copy (&ms->address, addr); |
| 672 | vec_add1 (lcm->map_servers, ms[0]); |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 673 | |
| 674 | if (vec_len (lcm->map_servers) == 1) |
| 675 | lcm->do_map_server_election = 1; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 676 | } |
| 677 | else |
| 678 | { |
| 679 | for (i = 0; i < vec_len (lcm->map_servers); i++) |
| 680 | { |
| 681 | ms = vec_elt_at_index (lcm->map_servers, i); |
| 682 | if (!ip_address_cmp (&ms->address, addr)) |
| 683 | { |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 684 | if (!ip_address_cmp (&ms->address, &lcm->active_map_server)) |
| 685 | lcm->do_map_server_election = 1; |
| 686 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 687 | vec_del1 (lcm->map_servers, i); |
| 688 | break; |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
Filip Tehlar | 69a9b76 | 2016-09-23 10:00:52 +0200 | [diff] [blame] | 696 | /** |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 697 | * Add/remove mapping to/from map-cache. Overwriting not allowed. |
| 698 | */ |
| 699 | int |
| 700 | vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 701 | u32 * map_index_result) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 702 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 703 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 704 | u32 mi, *map_indexp, map_index, i; |
| 705 | mapping_t *m, *old_map; |
| 706 | u32 **eid_indexes; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 707 | |
Filip Tehlar | ef2a5bf | 2017-05-30 07:14:46 +0200 | [diff] [blame] | 708 | if (gid_address_type (&a->eid) == GID_ADDR_NSH) |
| 709 | { |
| 710 | if (gid_address_vni (&a->eid) != 0) |
| 711 | { |
| 712 | clib_warning ("Supported only default VNI for NSH!"); |
| 713 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 714 | } |
| 715 | if (gid_address_nsh_spi (&a->eid) > MAX_VALUE_U24) |
| 716 | { |
| 717 | clib_warning ("SPI is greater than 24bit!"); |
| 718 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 719 | } |
| 720 | } |
| 721 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 722 | mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid); |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 723 | old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 724 | if (a->is_add) |
| 725 | { |
| 726 | /* TODO check if overwriting and take appropriate actions */ |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 727 | if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid)) |
| 728 | { |
| 729 | clib_warning ("eid %U found in the eid-table", format_gid_address, |
| 730 | &a->eid); |
| 731 | return VNET_API_ERROR_VALUE_EXIST; |
| 732 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 733 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 734 | pool_get (lcm->mapping_pool, m); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 735 | gid_address_copy (&m->eid, &a->eid); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 736 | m->locator_set_index = a->locator_set_index; |
| 737 | m->ttl = a->ttl; |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 738 | m->action = a->action; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 739 | m->local = a->local; |
Filip Tehlar | 3cd9e73 | 2016-08-23 10:52:44 +0200 | [diff] [blame] | 740 | m->is_static = a->is_static; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 741 | m->key = vec_dup (a->key); |
| 742 | m->key_id = a->key_id; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 743 | |
| 744 | map_index = m - lcm->mapping_pool; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 745 | gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 746 | 1); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 747 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 748 | if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index)) |
| 749 | { |
| 750 | clib_warning ("Locator set with index %d doesn't exist", |
| 751 | a->locator_set_index); |
| 752 | return VNET_API_ERROR_INVALID_VALUE; |
| 753 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 754 | |
| 755 | /* add eid to list of eids supported by locator-set */ |
| 756 | vec_validate (lcm->locator_set_to_eids, a->locator_set_index); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 757 | eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, |
| 758 | a->locator_set_index); |
| 759 | vec_add1 (eid_indexes[0], map_index); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 760 | |
| 761 | if (a->local) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 762 | { |
| 763 | /* mark as local */ |
| 764 | vec_add1 (lcm->local_mappings_indexes, map_index); |
| 765 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 766 | map_index_result[0] = map_index; |
| 767 | } |
| 768 | else |
| 769 | { |
Florin Coras | 577c355 | 2016-04-21 00:45:40 +0200 | [diff] [blame] | 770 | if (mi == GID_LOOKUP_MISS) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 771 | { |
| 772 | clib_warning ("eid %U not found in the eid-table", |
| 773 | format_gid_address, &a->eid); |
| 774 | return VNET_API_ERROR_INVALID_VALUE; |
| 775 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 776 | |
| 777 | /* clear locator-set to eids binding */ |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 778 | eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, |
| 779 | a->locator_set_index); |
| 780 | for (i = 0; i < vec_len (eid_indexes[0]); i++) |
| 781 | { |
| 782 | map_indexp = vec_elt_at_index (eid_indexes[0], i); |
| 783 | if (map_indexp[0] == mi) |
| 784 | break; |
| 785 | } |
| 786 | vec_del1 (eid_indexes[0], i); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 787 | |
| 788 | /* remove local mark if needed */ |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 789 | m = pool_elt_at_index (lcm->mapping_pool, mi); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 790 | if (m->local) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 791 | { |
| 792 | u32 k, *lm_indexp; |
| 793 | for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++) |
| 794 | { |
| 795 | lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k); |
| 796 | if (lm_indexp[0] == mi) |
| 797 | break; |
| 798 | } |
| 799 | vec_del1 (lcm->local_mappings_indexes, k); |
| 800 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 801 | |
| 802 | /* remove mapping from dictionary */ |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 803 | gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0); |
Filip Tehlar | 324112f | 2016-06-02 16:07:38 +0200 | [diff] [blame] | 804 | gid_address_free (&m->eid); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 805 | pool_put_index (lcm->mapping_pool, mi); |
| 806 | } |
| 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 811 | /** |
| 812 | * Add/update/delete mapping to/in/from map-cache. |
| 813 | */ |
Florin Coras | 577c355 | 2016-04-21 00:45:40 +0200 | [diff] [blame] | 814 | int |
| 815 | vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 816 | u32 * map_index_result) |
Florin Coras | 577c355 | 2016-04-21 00:45:40 +0200 | [diff] [blame] | 817 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 818 | uword *dp_table = 0; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 819 | u32 vni; |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 820 | u8 type; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 821 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 822 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
Florin Coras | 577c355 | 2016-04-21 00:45:40 +0200 | [diff] [blame] | 823 | |
Andrej Kozemcak | 8ebb2a1 | 2016-06-07 12:25:20 +0200 | [diff] [blame] | 824 | if (vnet_lisp_enable_disable_status () == 0) |
| 825 | { |
| 826 | clib_warning ("LISP is disabled!"); |
| 827 | return VNET_API_ERROR_LISP_DISABLED; |
| 828 | } |
| 829 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 830 | vni = gid_address_vni (&a->eid); |
| 831 | type = gid_address_type (&a->eid); |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 832 | if (GID_ADDR_IP_PREFIX == type) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 833 | dp_table = hash_get (lcm->table_id_by_vni, vni); |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 834 | else if (GID_ADDR_MAC == type) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 835 | dp_table = hash_get (lcm->bd_id_by_vni, vni); |
Florin Coras | 577c355 | 2016-04-21 00:45:40 +0200 | [diff] [blame] | 836 | |
Filip Tehlar | ef2a5bf | 2017-05-30 07:14:46 +0200 | [diff] [blame] | 837 | if (!dp_table && GID_ADDR_NSH != type) |
Florin Coras | 577c355 | 2016-04-21 00:45:40 +0200 | [diff] [blame] | 838 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 839 | clib_warning ("vni %d not associated to a %s!", vni, |
| 840 | GID_ADDR_IP_PREFIX == type ? "vrf" : "bd"); |
Florin Coras | 577c355 | 2016-04-21 00:45:40 +0200 | [diff] [blame] | 841 | return VNET_API_ERROR_INVALID_VALUE; |
| 842 | } |
| 843 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 844 | /* store/remove mapping from map-cache */ |
| 845 | return vnet_lisp_map_cache_add_del (a, map_index_result); |
Florin Coras | 577c355 | 2016-04-21 00:45:40 +0200 | [diff] [blame] | 846 | } |
| 847 | |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 848 | static void |
| 849 | add_l2_arp_bd (BVT (clib_bihash_kv) * kvp, void *arg) |
| 850 | { |
| 851 | u32 **ht = arg; |
| 852 | u32 bd = (u32) kvp->key[0]; |
| 853 | hash_set (ht[0], bd, 0); |
| 854 | } |
| 855 | |
| 856 | u32 * |
| 857 | vnet_lisp_l2_arp_bds_get (void) |
| 858 | { |
| 859 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 860 | u32 *bds = 0; |
| 861 | |
| 862 | gid_dict_foreach_l2_arp_entry (&lcm->mapping_index_by_gid, |
| 863 | add_l2_arp_bd, &bds); |
| 864 | return bds; |
| 865 | } |
| 866 | |
| 867 | typedef struct |
| 868 | { |
| 869 | void *vector; |
| 870 | u32 bd; |
| 871 | } lisp_add_l2_arp_args_t; |
| 872 | |
| 873 | static void |
| 874 | add_l2_arp_entry (BVT (clib_bihash_kv) * kvp, void *arg) |
| 875 | { |
| 876 | lisp_add_l2_arp_args_t *a = arg; |
| 877 | lisp_api_l2_arp_entry_t **vector = a->vector, e; |
| 878 | |
| 879 | if ((u32) kvp->key[0] == a->bd) |
| 880 | { |
| 881 | mac_copy (e.mac, (void *) &kvp->value); |
| 882 | e.ip4 = (u32) kvp->key[1]; |
| 883 | vec_add1 (vector[0], e); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | lisp_api_l2_arp_entry_t * |
| 888 | vnet_lisp_l2_arp_entries_get_by_bd (u32 bd) |
| 889 | { |
| 890 | lisp_api_l2_arp_entry_t *entries = 0; |
| 891 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 892 | lisp_add_l2_arp_args_t a; |
| 893 | |
| 894 | a.vector = &entries; |
| 895 | a.bd = bd; |
| 896 | |
| 897 | gid_dict_foreach_l2_arp_entry (&lcm->mapping_index_by_gid, |
| 898 | add_l2_arp_entry, &a); |
| 899 | return entries; |
| 900 | } |
| 901 | |
| 902 | int |
| 903 | vnet_lisp_add_del_l2_arp_entry (gid_address_t * key, u8 * mac, u8 is_add) |
| 904 | { |
| 905 | if (vnet_lisp_enable_disable_status () == 0) |
| 906 | { |
| 907 | clib_warning ("LISP is disabled!"); |
| 908 | return VNET_API_ERROR_LISP_DISABLED; |
| 909 | } |
| 910 | |
| 911 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 912 | int rc = 0; |
| 913 | |
| 914 | u64 res = gid_dictionary_lookup (&lcm->mapping_index_by_gid, key); |
| 915 | if (is_add) |
| 916 | { |
| 917 | if (res != GID_LOOKUP_MISS_L2) |
| 918 | { |
| 919 | clib_warning ("Entry %U exists in DB!", format_gid_address, key); |
| 920 | return VNET_API_ERROR_ENTRY_ALREADY_EXISTS; |
| 921 | } |
| 922 | u64 val = mac_to_u64 (mac); |
| 923 | gid_dictionary_add_del (&lcm->mapping_index_by_gid, key, val, |
| 924 | 1 /* is_add */ ); |
| 925 | } |
| 926 | else |
| 927 | { |
| 928 | if (res == GID_LOOKUP_MISS_L2) |
| 929 | { |
| 930 | clib_warning ("ONE ARP entry %U not found - cannot delete!", |
| 931 | format_gid_address, key); |
| 932 | return -1; |
| 933 | } |
| 934 | gid_dictionary_add_del (&lcm->mapping_index_by_gid, key, 0, |
| 935 | 0 /* is_add */ ); |
| 936 | } |
| 937 | |
| 938 | return rc; |
| 939 | } |
| 940 | |
Filip Tehlar | 324112f | 2016-06-02 16:07:38 +0200 | [diff] [blame] | 941 | int |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 942 | vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add) |
Filip Tehlar | 324112f | 2016-06-02 16:07:38 +0200 | [diff] [blame] | 943 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 944 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 945 | uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table; |
Filip Tehlar | 324112f | 2016-06-02 16:07:38 +0200 | [diff] [blame] | 946 | |
| 947 | if (vnet_lisp_enable_disable_status () == 0) |
| 948 | { |
| 949 | clib_warning ("LISP is disabled!"); |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 950 | return VNET_API_ERROR_LISP_DISABLED; |
Filip Tehlar | 324112f | 2016-06-02 16:07:38 +0200 | [diff] [blame] | 951 | } |
| 952 | |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 953 | dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni; |
| 954 | vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id; |
| 955 | |
| 956 | if (!is_l2 && (vni == 0 || dp_id == 0)) |
Filip Tehlar | 324112f | 2016-06-02 16:07:38 +0200 | [diff] [blame] | 957 | { |
| 958 | clib_warning ("can't add/del default vni-vrf mapping!"); |
| 959 | return -1; |
| 960 | } |
| 961 | |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 962 | dp_idp = hash_get (dp_table_by_vni[0], vni); |
| 963 | vnip = hash_get (vni_by_dp_table[0], dp_id); |
Filip Tehlar | 324112f | 2016-06-02 16:07:38 +0200 | [diff] [blame] | 964 | |
| 965 | if (is_add) |
| 966 | { |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 967 | if (dp_idp || vnip) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 968 | { |
| 969 | clib_warning ("vni %d or vrf %d already used in vrf/vni " |
| 970 | "mapping!", vni, dp_id); |
| 971 | return -1; |
| 972 | } |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 973 | hash_set (dp_table_by_vni[0], vni, dp_id); |
| 974 | hash_set (vni_by_dp_table[0], dp_id, vni); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 975 | |
| 976 | /* create dp iface */ |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 977 | dp_add_del_iface (lcm, vni, is_l2, 1); |
Filip Tehlar | 324112f | 2016-06-02 16:07:38 +0200 | [diff] [blame] | 978 | } |
| 979 | else |
| 980 | { |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 981 | if (!dp_idp || !vnip) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 982 | { |
| 983 | clib_warning ("vni %d or vrf %d not used in any vrf/vni! " |
| 984 | "mapping!", vni, dp_id); |
| 985 | return -1; |
| 986 | } |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 987 | /* remove dp iface */ |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 988 | dp_add_del_iface (lcm, vni, is_l2, 0); |
Filip Tehlar | fc56841 | 2017-04-05 10:06:03 +0200 | [diff] [blame] | 989 | |
| 990 | hash_unset (dp_table_by_vni[0], vni); |
| 991 | hash_unset (vni_by_dp_table[0], dp_id); |
Filip Tehlar | 324112f | 2016-06-02 16:07:38 +0200 | [diff] [blame] | 992 | } |
| 993 | return 0; |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 994 | |
Filip Tehlar | 324112f | 2016-06-02 16:07:38 +0200 | [diff] [blame] | 995 | } |
| 996 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 997 | /* return 0 if the two locator sets are identical 1 otherwise */ |
| 998 | static u8 |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 999 | compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes, |
| 1000 | locator_t * new_locators) |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 1001 | { |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1002 | u32 i, old_li; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1003 | locator_t *old_loc, *new_loc; |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 1004 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1005 | if (vec_len (old_ls_indexes) != vec_len (new_locators)) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1006 | return 1; |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 1007 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1008 | for (i = 0; i < vec_len (new_locators); i++) |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 1009 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1010 | old_li = vec_elt (old_ls_indexes, i); |
| 1011 | old_loc = pool_elt_at_index (lcm->locator_pool, old_li); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1012 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1013 | new_loc = vec_elt_at_index (new_locators, i); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1014 | |
| 1015 | if (locator_cmp (old_loc, new_loc)) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1016 | return 1; |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 1017 | } |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1018 | return 0; |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 1019 | } |
| 1020 | |
Filip Tehlar | d5fcc46 | 2016-10-17 16:20:18 +0200 | [diff] [blame] | 1021 | typedef struct |
| 1022 | { |
| 1023 | u8 is_negative; |
| 1024 | void *lcm; |
| 1025 | gid_address_t *eids_to_be_deleted; |
| 1026 | } remove_mapping_args_t; |
| 1027 | |
| 1028 | /** |
| 1029 | * Callback invoked when a sub-prefix is found |
| 1030 | */ |
| 1031 | static void |
| 1032 | remove_mapping_if_needed (u32 mi, void *arg) |
| 1033 | { |
| 1034 | u8 delete = 0; |
| 1035 | remove_mapping_args_t *a = arg; |
| 1036 | lisp_cp_main_t *lcm = a->lcm; |
| 1037 | mapping_t *m; |
| 1038 | locator_set_t *ls; |
| 1039 | |
| 1040 | m = pool_elt_at_index (lcm->mapping_pool, mi); |
| 1041 | if (!m) |
| 1042 | return; |
| 1043 | |
| 1044 | ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index); |
| 1045 | |
| 1046 | if (a->is_negative) |
| 1047 | { |
| 1048 | if (0 != vec_len (ls->locator_indices)) |
| 1049 | delete = 1; |
| 1050 | } |
| 1051 | else |
| 1052 | { |
| 1053 | if (0 == vec_len (ls->locator_indices)) |
| 1054 | delete = 1; |
| 1055 | } |
| 1056 | |
| 1057 | if (delete) |
| 1058 | vec_add1 (a->eids_to_be_deleted, m->eid); |
| 1059 | } |
| 1060 | |
| 1061 | /** |
| 1062 | * This function searches map cache and looks for IP prefixes that are subset |
| 1063 | * of the provided one. If such prefix is found depending on 'is_negative' |
| 1064 | * it does follows: |
| 1065 | * |
| 1066 | * 1) if is_negative is true and found prefix points to positive mapping, |
| 1067 | * then the mapping is removed |
| 1068 | * 2) if is_negative is false and found prefix points to negative mapping, |
| 1069 | * then the mapping is removed |
| 1070 | */ |
| 1071 | static void |
| 1072 | remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid, |
| 1073 | u8 is_negative) |
| 1074 | { |
| 1075 | gid_address_t *e; |
| 1076 | remove_mapping_args_t a; |
Florin Coras | f3dc11a | 2017-01-24 03:13:43 -0800 | [diff] [blame] | 1077 | |
Filip Tehlar | d5fcc46 | 2016-10-17 16:20:18 +0200 | [diff] [blame] | 1078 | memset (&a, 0, sizeof (a)); |
| 1079 | |
| 1080 | /* do this only in src/dst mode ... */ |
| 1081 | if (MR_MODE_SRC_DST != lcm->map_request_mode) |
| 1082 | return; |
| 1083 | |
| 1084 | /* ... and only for IP prefix */ |
| 1085 | if (GID_ADDR_SRC_DST != gid_address_type (eid) |
| 1086 | || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid))) |
| 1087 | return; |
| 1088 | |
| 1089 | a.is_negative = is_negative; |
| 1090 | a.lcm = lcm; |
| 1091 | |
| 1092 | gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid, |
| 1093 | remove_mapping_if_needed, &a); |
| 1094 | |
| 1095 | vec_foreach (e, a.eids_to_be_deleted) |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 1096 | { |
Florin Coras | f3dc11a | 2017-01-24 03:13:43 -0800 | [diff] [blame] | 1097 | vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args; |
| 1098 | |
| 1099 | memset (adj_args, 0, sizeof (adj_args[0])); |
| 1100 | gid_address_copy (&adj_args->reid, e); |
| 1101 | adj_args->is_add = 0; |
| 1102 | if (vnet_lisp_add_del_adjacency (adj_args)) |
| 1103 | clib_warning ("failed to del adjacency!"); |
| 1104 | |
Filip Tehlar | d5fcc46 | 2016-10-17 16:20:18 +0200 | [diff] [blame] | 1105 | vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0); |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 1106 | } |
Filip Tehlar | d5fcc46 | 2016-10-17 16:20:18 +0200 | [diff] [blame] | 1107 | |
| 1108 | vec_free (a.eids_to_be_deleted); |
| 1109 | } |
| 1110 | |
Filip Tehlar | 9677a94 | 2016-11-28 10:23:31 +0100 | [diff] [blame] | 1111 | static void |
| 1112 | mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi) |
| 1113 | { |
| 1114 | timing_wheel_delete (&lcm->wheel, mi); |
| 1115 | } |
| 1116 | |
Filip Tehlar | a6bce49 | 2017-02-03 10:17:49 +0100 | [diff] [blame] | 1117 | static int |
| 1118 | is_local_ip (lisp_cp_main_t * lcm, ip_address_t * addr) |
| 1119 | { |
| 1120 | fib_node_index_t fei; |
| 1121 | fib_prefix_t prefix; |
| 1122 | fib_entry_flag_t flags; |
| 1123 | |
| 1124 | ip_address_to_fib_prefix (addr, &prefix); |
| 1125 | |
| 1126 | fei = fib_table_lookup (0, &prefix); |
| 1127 | flags = fib_entry_get_flags (fei); |
| 1128 | return (FIB_ENTRY_FLAG_LOCAL & flags); |
| 1129 | } |
| 1130 | |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 1131 | /** |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1132 | * Adds/removes/updates mapping. Does not program forwarding. |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 1133 | * |
Florin Coras | 71893ac | 2016-07-10 20:09:32 +0200 | [diff] [blame] | 1134 | * @param eid end-host identifier |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 1135 | * @param rlocs vector of remote locators |
| 1136 | * @param action action for negative map-reply |
| 1137 | * @param is_add add mapping if non-zero, delete otherwise |
Florin Coras | 71893ac | 2016-07-10 20:09:32 +0200 | [diff] [blame] | 1138 | * @param res_map_index the map-index that was created/updated/removed. It is |
| 1139 | * set to ~0 if no action is taken. |
Filip Tehlar | 3cd9e73 | 2016-08-23 10:52:44 +0200 | [diff] [blame] | 1140 | * @param is_static used for distinguishing between statically learned |
| 1141 | remote mappings and mappings obtained from MR |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 1142 | * @return return code |
| 1143 | */ |
| 1144 | int |
Florin Coras | 71893ac | 2016-07-10 20:09:32 +0200 | [diff] [blame] | 1145 | vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action, |
Filip Tehlar | 3cd9e73 | 2016-08-23 10:52:44 +0200 | [diff] [blame] | 1146 | u8 authoritative, u32 ttl, u8 is_add, u8 is_static, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1147 | u32 * res_map_index) |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 1148 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1149 | vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args; |
| 1150 | vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args; |
| 1151 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1152 | u32 mi, ls_index = 0, dst_map_index; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1153 | mapping_t *old_map; |
Filip Tehlar | a6bce49 | 2017-02-03 10:17:49 +0100 | [diff] [blame] | 1154 | locator_t *loc; |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 1155 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1156 | if (vnet_lisp_enable_disable_status () == 0) |
Andrej Kozemcak | 8ebb2a1 | 2016-06-07 12:25:20 +0200 | [diff] [blame] | 1157 | { |
| 1158 | clib_warning ("LISP is disabled!"); |
| 1159 | return VNET_API_ERROR_LISP_DISABLED; |
| 1160 | } |
| 1161 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1162 | if (res_map_index) |
| 1163 | res_map_index[0] = ~0; |
Filip Tehlar | 58f886a | 2016-05-30 15:57:40 +0200 | [diff] [blame] | 1164 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1165 | memset (m_args, 0, sizeof (m_args[0])); |
| 1166 | memset (ls_args, 0, sizeof (ls_args[0])); |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 1167 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1168 | ls_args->locators = rlocs; |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 1169 | |
Florin Coras | 71893ac | 2016-07-10 20:09:32 +0200 | [diff] [blame] | 1170 | mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1171 | old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1172 | |
| 1173 | if (is_add) |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 1174 | { |
Filip Tehlar | 272c69e | 2017-02-15 16:40:35 +0100 | [diff] [blame] | 1175 | /* check if none of the locators match localy configured address */ |
| 1176 | vec_foreach (loc, rlocs) |
| 1177 | { |
| 1178 | ip_prefix_t *p = &gid_address_ippref (&loc->address); |
| 1179 | if (is_local_ip (lcm, &ip_prefix_addr (p))) |
| 1180 | { |
| 1181 | clib_warning ("RLOC %U matches a local address!", |
| 1182 | format_gid_address, &loc->address); |
| 1183 | return VNET_API_ERROR_LISP_RLOC_LOCAL; |
| 1184 | } |
| 1185 | } |
| 1186 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1187 | /* overwrite: if mapping already exists, decide if locators should be |
| 1188 | * updated and be done */ |
Florin Coras | 71893ac | 2016-07-10 20:09:32 +0200 | [diff] [blame] | 1189 | if (old_map && gid_address_cmp (&old_map->eid, eid) == 0) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1190 | { |
Filip Tehlar | 3cd9e73 | 2016-08-23 10:52:44 +0200 | [diff] [blame] | 1191 | if (!is_static && (old_map->is_static || old_map->local)) |
| 1192 | { |
| 1193 | /* do not overwrite local or static remote mappings */ |
| 1194 | clib_warning ("mapping %U rejected due to collision with local " |
| 1195 | "or static remote mapping!", format_gid_address, |
Filip Tehlar | d5fcc46 | 2016-10-17 16:20:18 +0200 | [diff] [blame] | 1196 | eid); |
Filip Tehlar | 3cd9e73 | 2016-08-23 10:52:44 +0200 | [diff] [blame] | 1197 | return 0; |
| 1198 | } |
| 1199 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1200 | locator_set_t *old_ls; |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 1201 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1202 | /* update mapping attributes */ |
| 1203 | old_map->action = action; |
| 1204 | old_map->authoritative = authoritative; |
| 1205 | old_map->ttl = ttl; |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 1206 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1207 | old_ls = pool_elt_at_index (lcm->locator_set_pool, |
| 1208 | old_map->locator_set_index); |
| 1209 | if (compare_locators (lcm, old_ls->locator_indices, |
| 1210 | ls_args->locators)) |
| 1211 | { |
| 1212 | /* set locator-set index to overwrite */ |
| 1213 | ls_args->is_add = 1; |
| 1214 | ls_args->index = old_map->locator_set_index; |
| 1215 | vnet_lisp_add_del_locator_set (ls_args, 0); |
| 1216 | if (res_map_index) |
| 1217 | res_map_index[0] = mi; |
| 1218 | } |
| 1219 | } |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1220 | /* new mapping */ |
| 1221 | else |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1222 | { |
Filip Tehlar | d5fcc46 | 2016-10-17 16:20:18 +0200 | [diff] [blame] | 1223 | remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators); |
| 1224 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1225 | ls_args->is_add = 1; |
| 1226 | ls_args->index = ~0; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1227 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1228 | vnet_lisp_add_del_locator_set (ls_args, &ls_index); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1229 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1230 | /* add mapping */ |
| 1231 | gid_address_copy (&m_args->eid, eid); |
| 1232 | m_args->is_add = 1; |
| 1233 | m_args->action = action; |
| 1234 | m_args->locator_set_index = ls_index; |
Filip Tehlar | 3cd9e73 | 2016-08-23 10:52:44 +0200 | [diff] [blame] | 1235 | m_args->is_static = is_static; |
Filip Tehlar | 9677a94 | 2016-11-28 10:23:31 +0100 | [diff] [blame] | 1236 | m_args->ttl = ttl; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1237 | vnet_lisp_map_cache_add_del (m_args, &dst_map_index); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1238 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1239 | if (res_map_index) |
| 1240 | res_map_index[0] = dst_map_index; |
| 1241 | } |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 1242 | } |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1243 | else |
| 1244 | { |
Florin Coras | 71893ac | 2016-07-10 20:09:32 +0200 | [diff] [blame] | 1245 | if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1246 | { |
| 1247 | clib_warning ("cannot delete mapping for eid %U", |
| 1248 | format_gid_address, eid); |
| 1249 | return -1; |
| 1250 | } |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1251 | |
| 1252 | m_args->is_add = 0; |
Florin Coras | 71893ac | 2016-07-10 20:09:32 +0200 | [diff] [blame] | 1253 | gid_address_copy (&m_args->eid, eid); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1254 | m_args->locator_set_index = old_map->locator_set_index; |
| 1255 | |
| 1256 | /* delete mapping associated from map-cache */ |
| 1257 | vnet_lisp_map_cache_add_del (m_args, 0); |
| 1258 | |
| 1259 | ls_args->is_add = 0; |
| 1260 | ls_args->index = old_map->locator_set_index; |
| 1261 | /* delete locator set */ |
| 1262 | vnet_lisp_add_del_locator_set (ls_args, 0); |
Filip Tehlar | b93222f | 2016-07-07 09:58:08 +0200 | [diff] [blame] | 1263 | |
Filip Tehlar | 9677a94 | 2016-11-28 10:23:31 +0100 | [diff] [blame] | 1264 | /* delete timer associated to the mapping if any */ |
| 1265 | if (old_map->timer_set) |
| 1266 | mapping_delete_timer (lcm, mi); |
| 1267 | |
Filip Tehlar | b93222f | 2016-07-07 09:58:08 +0200 | [diff] [blame] | 1268 | /* return old mapping index */ |
Andrej Kozemcak | 438109d | 2016-07-22 12:54:12 +0200 | [diff] [blame] | 1269 | if (res_map_index) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1270 | res_map_index[0] = mi; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1271 | } |
| 1272 | |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 1273 | /* success */ |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1274 | return 0; |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 1275 | } |
| 1276 | |
Filip Tehlar | 58f886a | 2016-05-30 15:57:40 +0200 | [diff] [blame] | 1277 | int |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1278 | vnet_lisp_clear_all_remote_adjacencies (void) |
Filip Tehlar | 58f886a | 2016-05-30 15:57:40 +0200 | [diff] [blame] | 1279 | { |
| 1280 | int rv = 0; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1281 | u32 mi, *map_indices = 0, *map_indexp; |
| 1282 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 1283 | vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args; |
| 1284 | vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls; |
Filip Tehlar | 58f886a | 2016-05-30 15:57:40 +0200 | [diff] [blame] | 1285 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1286 | /* *INDENT-OFF* */ |
Filip Tehlar | 58f886a | 2016-05-30 15:57:40 +0200 | [diff] [blame] | 1287 | pool_foreach_index (mi, lcm->mapping_pool, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1288 | ({ |
| 1289 | vec_add1 (map_indices, mi); |
| 1290 | })); |
| 1291 | /* *INDENT-ON* */ |
Filip Tehlar | 58f886a | 2016-05-30 15:57:40 +0200 | [diff] [blame] | 1292 | |
| 1293 | vec_foreach (map_indexp, map_indices) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1294 | { |
| 1295 | mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]); |
| 1296 | if (!map->local) |
| 1297 | { |
| 1298 | dp_del_fwd_entry (lcm, 0, map_indexp[0]); |
Filip Tehlar | 58f886a | 2016-05-30 15:57:40 +0200 | [diff] [blame] | 1299 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1300 | dm_args->is_add = 0; |
| 1301 | gid_address_copy (&dm_args->eid, &map->eid); |
| 1302 | dm_args->locator_set_index = map->locator_set_index; |
Filip Tehlar | 58f886a | 2016-05-30 15:57:40 +0200 | [diff] [blame] | 1303 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1304 | /* delete mapping associated to fwd entry */ |
| 1305 | vnet_lisp_map_cache_add_del (dm_args, 0); |
Filip Tehlar | 58f886a | 2016-05-30 15:57:40 +0200 | [diff] [blame] | 1306 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1307 | ls->is_add = 0; |
| 1308 | ls->local = 0; |
| 1309 | ls->index = map->locator_set_index; |
| 1310 | /* delete locator set */ |
| 1311 | rv = vnet_lisp_add_del_locator_set (ls, 0); |
| 1312 | if (rv != 0) |
| 1313 | goto cleanup; |
| 1314 | } |
| 1315 | } |
Filip Tehlar | 58f886a | 2016-05-30 15:57:40 +0200 | [diff] [blame] | 1316 | |
| 1317 | cleanup: |
| 1318 | if (map_indices) |
| 1319 | vec_free (map_indices); |
| 1320 | return rv; |
| 1321 | } |
| 1322 | |
Filip Tehlar | 195bcee | 2016-05-13 17:37:35 +0200 | [diff] [blame] | 1323 | /** |
Florin Coras | 71893ac | 2016-07-10 20:09:32 +0200 | [diff] [blame] | 1324 | * Adds adjacency or removes forwarding entry associated to remote mapping. |
| 1325 | * Note that adjacencies are not stored, they only result in forwarding entries |
| 1326 | * being created. |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1327 | */ |
Florin Coras | f3dc11a | 2017-01-24 03:13:43 -0800 | [diff] [blame] | 1328 | int |
| 1329 | vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1330 | { |
Florin Coras | f3dc11a | 2017-01-24 03:13:43 -0800 | [diff] [blame] | 1331 | lisp_cp_main_t *lcm = &lisp_control_main; |
Florin Coras | 71893ac | 2016-07-10 20:09:32 +0200 | [diff] [blame] | 1332 | u32 local_mi, remote_mi = ~0; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1333 | |
| 1334 | if (vnet_lisp_enable_disable_status () == 0) |
| 1335 | { |
| 1336 | clib_warning ("LISP is disabled!"); |
| 1337 | return VNET_API_ERROR_LISP_DISABLED; |
| 1338 | } |
| 1339 | |
Filip Tehlar | 69a9b76 | 2016-09-23 10:00:52 +0200 | [diff] [blame] | 1340 | remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, |
Florin Coras | f3dc11a | 2017-01-24 03:13:43 -0800 | [diff] [blame] | 1341 | &a->reid, &a->leid); |
Florin Coras | 71893ac | 2016-07-10 20:09:32 +0200 | [diff] [blame] | 1342 | if (GID_LOOKUP_MISS == remote_mi) |
| 1343 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1344 | clib_warning ("Remote eid %U not found. Cannot add adjacency!", |
Florin Coras | f3dc11a | 2017-01-24 03:13:43 -0800 | [diff] [blame] | 1345 | format_gid_address, &a->reid); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1346 | |
Florin Coras | 71893ac | 2016-07-10 20:09:32 +0200 | [diff] [blame] | 1347 | return -1; |
| 1348 | } |
| 1349 | |
Florin Coras | f3dc11a | 2017-01-24 03:13:43 -0800 | [diff] [blame] | 1350 | if (a->is_add) |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1351 | { |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1352 | /* check if source eid has an associated mapping. If pitr mode is on, |
| 1353 | * just use the pitr's mapping */ |
Filip Tehlar | ef2a5bf | 2017-05-30 07:14:46 +0200 | [diff] [blame] | 1354 | if (lcm->lisp_pitr) |
| 1355 | local_mi = lcm->pitr_map_index; |
| 1356 | else |
| 1357 | { |
| 1358 | if (gid_address_type (&a->reid) == GID_ADDR_NSH) |
| 1359 | { |
| 1360 | if (lcm->nsh_map_index == ~0) |
| 1361 | local_mi = GID_LOOKUP_MISS; |
| 1362 | else |
| 1363 | local_mi = lcm->nsh_map_index; |
| 1364 | } |
| 1365 | else |
| 1366 | { |
| 1367 | local_mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, |
| 1368 | &a->leid); |
| 1369 | } |
| 1370 | } |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1371 | |
Florin Coras | 71893ac | 2016-07-10 20:09:32 +0200 | [diff] [blame] | 1372 | if (GID_LOOKUP_MISS == local_mi) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1373 | { |
| 1374 | clib_warning ("Local eid %U not found. Cannot add adjacency!", |
Florin Coras | f3dc11a | 2017-01-24 03:13:43 -0800 | [diff] [blame] | 1375 | format_gid_address, &a->leid); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1376 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1377 | return -1; |
| 1378 | } |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1379 | |
Florin Coras | 71893ac | 2016-07-10 20:09:32 +0200 | [diff] [blame] | 1380 | /* update forwarding */ |
| 1381 | dp_add_fwd_entry (lcm, local_mi, remote_mi); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1382 | } |
| 1383 | else |
Florin Coras | 71893ac | 2016-07-10 20:09:32 +0200 | [diff] [blame] | 1384 | dp_del_fwd_entry (lcm, 0, remote_mi); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1385 | |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
| 1389 | int |
Florin Coras | dca8804 | 2016-09-14 16:01:38 +0200 | [diff] [blame] | 1390 | vnet_lisp_set_map_request_mode (u8 mode) |
| 1391 | { |
| 1392 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 1393 | |
| 1394 | if (vnet_lisp_enable_disable_status () == 0) |
| 1395 | { |
| 1396 | clib_warning ("LISP is disabled!"); |
| 1397 | return VNET_API_ERROR_LISP_DISABLED; |
| 1398 | } |
| 1399 | |
| 1400 | if (mode >= _MR_MODE_MAX) |
| 1401 | { |
| 1402 | clib_warning ("Invalid LISP map request mode %d!", mode); |
| 1403 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 1404 | } |
| 1405 | |
| 1406 | lcm->map_request_mode = mode; |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 1410 | int |
Filip Tehlar | ef2a5bf | 2017-05-30 07:14:46 +0200 | [diff] [blame] | 1411 | vnet_lisp_nsh_set_locator_set (u8 * locator_set_name, u8 is_add) |
| 1412 | { |
| 1413 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 1414 | lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main (); |
| 1415 | u32 locator_set_index = ~0; |
| 1416 | mapping_t *m; |
| 1417 | uword *p; |
| 1418 | |
| 1419 | if (vnet_lisp_enable_disable_status () == 0) |
| 1420 | { |
| 1421 | clib_warning ("LISP is disabled!"); |
| 1422 | return VNET_API_ERROR_LISP_DISABLED; |
| 1423 | } |
| 1424 | |
| 1425 | if (is_add) |
| 1426 | { |
| 1427 | if (lcm->nsh_map_index == (u32) ~ 0) |
| 1428 | { |
| 1429 | p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name); |
| 1430 | if (!p) |
| 1431 | { |
| 1432 | clib_warning ("locator-set %v doesn't exist", locator_set_name); |
| 1433 | return -1; |
| 1434 | } |
| 1435 | locator_set_index = p[0]; |
| 1436 | |
| 1437 | pool_get (lcm->mapping_pool, m); |
| 1438 | memset (m, 0, sizeof *m); |
| 1439 | m->locator_set_index = locator_set_index; |
| 1440 | m->local = 1; |
| 1441 | m->nsh_set = 1; |
| 1442 | lcm->nsh_map_index = m - lcm->mapping_pool; |
| 1443 | |
| 1444 | if (~0 == vnet_lisp_gpe_add_nsh_iface (lgm)) |
| 1445 | return -1; |
| 1446 | } |
| 1447 | } |
| 1448 | else |
| 1449 | { |
| 1450 | if (lcm->nsh_map_index != (u32) ~ 0) |
| 1451 | { |
| 1452 | /* remove NSH mapping */ |
| 1453 | pool_put_index (lcm->mapping_pool, lcm->nsh_map_index); |
| 1454 | lcm->nsh_map_index = ~0; |
| 1455 | vnet_lisp_gpe_del_nsh_iface (lgm); |
| 1456 | } |
| 1457 | } |
| 1458 | return 0; |
| 1459 | } |
| 1460 | |
| 1461 | int |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 1462 | vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add) |
| 1463 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1464 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 1465 | u32 locator_set_index = ~0; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1466 | mapping_t *m; |
| 1467 | uword *p; |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 1468 | |
Andrej Kozemcak | 8ebb2a1 | 2016-06-07 12:25:20 +0200 | [diff] [blame] | 1469 | if (vnet_lisp_enable_disable_status () == 0) |
| 1470 | { |
| 1471 | clib_warning ("LISP is disabled!"); |
| 1472 | return VNET_API_ERROR_LISP_DISABLED; |
| 1473 | } |
| 1474 | |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 1475 | p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name); |
| 1476 | if (!p) |
| 1477 | { |
| 1478 | clib_warning ("locator-set %v doesn't exist", locator_set_name); |
| 1479 | return -1; |
| 1480 | } |
| 1481 | locator_set_index = p[0]; |
| 1482 | |
| 1483 | if (is_add) |
| 1484 | { |
| 1485 | pool_get (lcm->mapping_pool, m); |
| 1486 | m->locator_set_index = locator_set_index; |
| 1487 | m->local = 1; |
Filip Tehlar | 38206ee | 2017-02-20 17:31:57 +0100 | [diff] [blame] | 1488 | m->pitr_set = 1; |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 1489 | lcm->pitr_map_index = m - lcm->mapping_pool; |
| 1490 | |
| 1491 | /* enable pitr mode */ |
| 1492 | lcm->lisp_pitr = 1; |
| 1493 | } |
| 1494 | else |
| 1495 | { |
| 1496 | /* remove pitr mapping */ |
| 1497 | pool_put_index (lcm->mapping_pool, lcm->pitr_map_index); |
| 1498 | |
| 1499 | /* disable pitr mode */ |
| 1500 | lcm->lisp_pitr = 0; |
| 1501 | } |
| 1502 | return 0; |
| 1503 | } |
| 1504 | |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 1505 | int |
| 1506 | vnet_lisp_map_register_fallback_threshold_set (u32 value) |
| 1507 | { |
| 1508 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 1509 | if (0 == value) |
| 1510 | { |
| 1511 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 1512 | } |
| 1513 | |
| 1514 | lcm->max_expired_map_registers = value; |
| 1515 | return 0; |
| 1516 | } |
| 1517 | |
| 1518 | u32 |
| 1519 | vnet_lisp_map_register_fallback_threshold_get (void) |
| 1520 | { |
| 1521 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 1522 | return lcm->max_expired_map_registers; |
| 1523 | } |
| 1524 | |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 1525 | /** |
| 1526 | * Configure Proxy-ETR |
| 1527 | * |
| 1528 | * @param ip PETR's IP address |
| 1529 | * @param is_add Flag that indicates if this is an addition or removal |
| 1530 | * |
| 1531 | * return 0 on success |
| 1532 | */ |
| 1533 | int |
| 1534 | vnet_lisp_use_petr (ip_address_t * ip, u8 is_add) |
| 1535 | { |
| 1536 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 1537 | u32 ls_index = ~0; |
| 1538 | mapping_t *m; |
| 1539 | vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args; |
| 1540 | locator_t loc; |
| 1541 | |
| 1542 | if (vnet_lisp_enable_disable_status () == 0) |
| 1543 | { |
| 1544 | clib_warning ("LISP is disabled!"); |
| 1545 | return VNET_API_ERROR_LISP_DISABLED; |
| 1546 | } |
| 1547 | |
| 1548 | memset (ls_args, 0, sizeof (*ls_args)); |
| 1549 | |
| 1550 | if (is_add) |
| 1551 | { |
| 1552 | /* Create dummy petr locator-set */ |
Florin Coras | b69259a | 2017-03-09 14:39:54 -0800 | [diff] [blame] | 1553 | memset (&loc, 0, sizeof (loc)); |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 1554 | gid_address_from_ip (&loc.address, ip); |
| 1555 | loc.priority = 1; |
| 1556 | loc.state = loc.weight = 1; |
Florin Coras | 2743cc4 | 2017-01-29 16:42:20 -0800 | [diff] [blame] | 1557 | loc.local = 0; |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 1558 | |
| 1559 | ls_args->is_add = 1; |
| 1560 | ls_args->index = ~0; |
| 1561 | vec_add1 (ls_args->locators, loc); |
| 1562 | vnet_lisp_add_del_locator_set (ls_args, &ls_index); |
| 1563 | |
| 1564 | /* Add petr mapping */ |
| 1565 | pool_get (lcm->mapping_pool, m); |
| 1566 | m->locator_set_index = ls_index; |
| 1567 | lcm->petr_map_index = m - lcm->mapping_pool; |
| 1568 | |
| 1569 | /* Enable use-petr */ |
| 1570 | lcm->flags |= LISP_FLAG_USE_PETR; |
| 1571 | } |
| 1572 | else |
| 1573 | { |
| 1574 | m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index); |
| 1575 | |
| 1576 | /* Remove petr locator */ |
| 1577 | ls_args->is_add = 0; |
| 1578 | ls_args->index = m->locator_set_index; |
| 1579 | vnet_lisp_add_del_locator_set (ls_args, 0); |
| 1580 | |
| 1581 | /* Remove petr mapping */ |
| 1582 | pool_put_index (lcm->mapping_pool, lcm->petr_map_index); |
| 1583 | |
| 1584 | /* Disable use-petr */ |
| 1585 | lcm->flags &= ~LISP_FLAG_USE_PETR; |
| 1586 | } |
| 1587 | return 0; |
| 1588 | } |
| 1589 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1590 | /* cleans locator to locator-set data and removes locators not part of |
| 1591 | * any locator-set */ |
| 1592 | static void |
| 1593 | clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi) |
| 1594 | { |
Filip Tehlar | d1c5cc3 | 2016-05-30 10:39:20 +0200 | [diff] [blame] | 1595 | u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1596 | locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi); |
| 1597 | for (i = 0; i < vec_len (ls->locator_indices); i++) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1598 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1599 | loc_indexp = vec_elt_at_index (ls->locator_indices, i); |
| 1600 | ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, |
| 1601 | loc_indexp[0]); |
| 1602 | for (j = 0; j < vec_len (ls_indexes[0]); j++) |
| 1603 | { |
| 1604 | ls_indexp = vec_elt_at_index (ls_indexes[0], j); |
| 1605 | if (ls_indexp[0] == lsi) |
| 1606 | break; |
| 1607 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1608 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1609 | /* delete index for removed locator-set */ |
| 1610 | vec_del1 (ls_indexes[0], j); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1611 | |
| 1612 | /* delete locator if it's part of no locator-set */ |
| 1613 | if (vec_len (ls_indexes[0]) == 0) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1614 | { |
| 1615 | pool_put_index (lcm->locator_pool, loc_indexp[0]); |
| 1616 | vec_add1 (to_be_deleted, i); |
| 1617 | } |
Filip Tehlar | d1c5cc3 | 2016-05-30 10:39:20 +0200 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | if (to_be_deleted) |
| 1621 | { |
| 1622 | for (i = 0; i < vec_len (to_be_deleted); i++) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1623 | { |
| 1624 | loc_indexp = vec_elt_at_index (to_be_deleted, i); |
| 1625 | vec_del1 (ls->locator_indices, loc_indexp[0]); |
| 1626 | } |
Filip Tehlar | d1c5cc3 | 2016-05-30 10:39:20 +0200 | [diff] [blame] | 1627 | vec_free (to_be_deleted); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1628 | } |
| 1629 | } |
Filip Tehlar | d1c5cc3 | 2016-05-30 10:39:20 +0200 | [diff] [blame] | 1630 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1631 | static inline uword * |
| 1632 | get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p) |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1633 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1634 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1635 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1636 | ASSERT (a != NULL); |
| 1637 | ASSERT (p != NULL); |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1638 | |
| 1639 | /* find locator-set */ |
| 1640 | if (a->local) |
| 1641 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1642 | p = hash_get_mem (lcm->locator_set_index_by_name, a->name); |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1643 | } |
| 1644 | else |
| 1645 | { |
| 1646 | *p = a->index; |
| 1647 | } |
| 1648 | |
| 1649 | return p; |
| 1650 | } |
| 1651 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1652 | static inline int |
| 1653 | is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1654 | locator_t * loc) |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1655 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1656 | locator_t *itloc; |
| 1657 | u32 *locit; |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1658 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1659 | ASSERT (ls != NULL); |
| 1660 | ASSERT (loc != NULL); |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1661 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1662 | vec_foreach (locit, ls->locator_indices) |
| 1663 | { |
| 1664 | itloc = pool_elt_at_index (lcm->locator_pool, locit[0]); |
| 1665 | if ((ls->local && itloc->sw_if_index == loc->sw_if_index) || |
| 1666 | (!ls->local && !gid_address_cmp (&itloc->address, &loc->address))) |
| 1667 | { |
| 1668 | clib_warning ("Duplicate locator"); |
| 1669 | return VNET_API_ERROR_VALUE_EXIST; |
| 1670 | } |
| 1671 | } |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1672 | |
| 1673 | return 0; |
| 1674 | } |
| 1675 | |
Filip Tehlar | 68d2e24 | 2017-04-21 12:05:58 +0200 | [diff] [blame] | 1676 | static void |
| 1677 | update_adjacencies_by_map_index (lisp_cp_main_t * lcm, u8 is_local, |
| 1678 | u32 mapping_index, u8 remove_only) |
| 1679 | { |
| 1680 | fwd_entry_t *fwd; |
| 1681 | mapping_t *map; |
| 1682 | vnet_lisp_add_del_adjacency_args_t _a, *a = &_a; |
| 1683 | |
| 1684 | map = pool_elt_at_index (lcm->mapping_pool, mapping_index); |
| 1685 | |
| 1686 | /* *INDENT-OFF* */ |
| 1687 | pool_foreach(fwd, lcm->fwd_entry_pool, |
| 1688 | ({ |
| 1689 | if ((is_local && 0 == gid_address_cmp (&map->eid, &fwd->leid)) || |
| 1690 | (!is_local && 0 == gid_address_cmp (&map->eid, &fwd->reid))) |
| 1691 | { |
| 1692 | a->is_add = 0; |
| 1693 | gid_address_copy (&a->leid, &fwd->leid); |
| 1694 | gid_address_copy (&a->reid, &fwd->reid); |
| 1695 | |
| 1696 | vnet_lisp_add_del_adjacency (a); |
| 1697 | |
| 1698 | if (!remove_only) |
| 1699 | { |
| 1700 | a->is_add = 1; |
| 1701 | vnet_lisp_add_del_adjacency (a); |
| 1702 | } |
| 1703 | } |
| 1704 | })); |
| 1705 | /* *INDENT-ON* */ |
| 1706 | } |
| 1707 | |
| 1708 | static void |
| 1709 | update_fwd_entries_by_locator_set (lisp_cp_main_t * lcm, u8 is_local, |
| 1710 | u32 ls_index, u8 remove_only) |
| 1711 | { |
| 1712 | u32 i, *map_indexp; |
| 1713 | u32 **eid_indexes; |
Filip Tehlar | facee28 | 2017-04-27 14:29:27 +0200 | [diff] [blame] | 1714 | |
| 1715 | if (vec_len (lcm->locator_set_to_eids) <= ls_index) |
| 1716 | return; |
| 1717 | |
Filip Tehlar | 68d2e24 | 2017-04-21 12:05:58 +0200 | [diff] [blame] | 1718 | eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, ls_index); |
| 1719 | |
| 1720 | for (i = 0; i < vec_len (eid_indexes[0]); i++) |
| 1721 | { |
| 1722 | map_indexp = vec_elt_at_index (eid_indexes[0], i); |
| 1723 | update_adjacencies_by_map_index (lcm, is_local, map_indexp[0], |
| 1724 | remove_only); |
| 1725 | } |
| 1726 | } |
| 1727 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 1728 | static inline void |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1729 | remove_locator_from_locator_set (locator_set_t * ls, u32 * locit, |
| 1730 | u32 ls_index, u32 loc_id) |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1731 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1732 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 1733 | u32 **ls_indexes = NULL; |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1734 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1735 | ASSERT (ls != NULL); |
| 1736 | ASSERT (locit != NULL); |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1737 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1738 | ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]); |
| 1739 | pool_put_index (lcm->locator_pool, locit[0]); |
| 1740 | vec_del1 (ls->locator_indices, loc_id); |
| 1741 | vec_del1 (ls_indexes[0], ls_index); |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | int |
| 1745 | vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1746 | locator_set_t * ls, u32 * ls_result) |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1747 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1748 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 1749 | locator_t *loc = NULL, *itloc = NULL; |
| 1750 | uword _p = (u32) ~ 0, *p = &_p; |
| 1751 | u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL; |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1752 | u32 loc_id = ~0; |
| 1753 | int ret = 0; |
| 1754 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1755 | ASSERT (a != NULL); |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1756 | |
Andrej Kozemcak | 8ebb2a1 | 2016-06-07 12:25:20 +0200 | [diff] [blame] | 1757 | if (vnet_lisp_enable_disable_status () == 0) |
| 1758 | { |
| 1759 | clib_warning ("LISP is disabled!"); |
| 1760 | return VNET_API_ERROR_LISP_DISABLED; |
| 1761 | } |
| 1762 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1763 | p = get_locator_set_index (a, p); |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1764 | if (!p) |
| 1765 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1766 | clib_warning ("locator-set %v doesn't exist", a->name); |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1767 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 1768 | } |
| 1769 | |
| 1770 | if (ls == 0) |
| 1771 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1772 | ls = pool_elt_at_index (lcm->locator_set_pool, p[0]); |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1773 | if (!ls) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1774 | { |
| 1775 | clib_warning ("locator-set %d to be overwritten doesn't exist!", |
| 1776 | p[0]); |
| 1777 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 1778 | } |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | if (a->is_add) |
| 1782 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1783 | if (ls_result) |
| 1784 | ls_result[0] = p[0]; |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1785 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1786 | /* allocate locators */ |
| 1787 | vec_foreach (itloc, a->locators) |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1788 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1789 | ret = is_locator_in_locator_set (lcm, ls, itloc); |
| 1790 | if (0 != ret) |
| 1791 | { |
| 1792 | return ret; |
| 1793 | } |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1794 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1795 | pool_get (lcm->locator_pool, loc); |
| 1796 | loc[0] = itloc[0]; |
| 1797 | loc_index = loc - lcm->locator_pool; |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1798 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1799 | vec_add1 (ls->locator_indices, loc_index); |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1800 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1801 | vec_validate (lcm->locator_to_locator_sets, loc_index); |
| 1802 | ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, |
| 1803 | loc_index); |
Filip Tehlar | c5bb0d6 | 2016-09-02 12:14:31 +0200 | [diff] [blame] | 1804 | vec_add1 (ls_indexes[0], p[0]); |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1805 | } |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1806 | } |
| 1807 | else |
| 1808 | { |
| 1809 | ls_index = p[0]; |
Filip Tehlar | 68d2e24 | 2017-04-21 12:05:58 +0200 | [diff] [blame] | 1810 | u8 removed; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1811 | |
Filip Tehlar | 68d2e24 | 2017-04-21 12:05:58 +0200 | [diff] [blame] | 1812 | vec_foreach (itloc, a->locators) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1813 | { |
Filip Tehlar | 68d2e24 | 2017-04-21 12:05:58 +0200 | [diff] [blame] | 1814 | removed = 0; |
| 1815 | loc_id = 0; |
| 1816 | vec_foreach (locit, ls->locator_indices) |
| 1817 | { |
| 1818 | loc = pool_elt_at_index (lcm->locator_pool, locit[0]); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1819 | |
Filip Tehlar | 68d2e24 | 2017-04-21 12:05:58 +0200 | [diff] [blame] | 1820 | if (loc->local && loc->sw_if_index == itloc->sw_if_index) |
| 1821 | { |
| 1822 | removed = 1; |
| 1823 | remove_locator_from_locator_set (ls, locit, ls_index, loc_id); |
| 1824 | } |
| 1825 | if (0 == loc->local && |
| 1826 | !gid_address_cmp (&loc->address, &itloc->address)) |
| 1827 | { |
| 1828 | removed = 1; |
| 1829 | remove_locator_from_locator_set (ls, locit, ls_index, loc_id); |
| 1830 | } |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1831 | |
Filip Tehlar | 68d2e24 | 2017-04-21 12:05:58 +0200 | [diff] [blame] | 1832 | if (removed) |
| 1833 | { |
| 1834 | /* update fwd entries using this locator in DP */ |
| 1835 | update_fwd_entries_by_locator_set (lcm, loc->local, ls_index, |
| 1836 | vec_len (ls->locator_indices) |
| 1837 | == 0); |
| 1838 | } |
| 1839 | |
| 1840 | loc_id++; |
| 1841 | } |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1842 | } |
| 1843 | } |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1844 | |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1848 | int |
| 1849 | vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1850 | u32 * ls_result) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1851 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1852 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 1853 | locator_set_t *ls; |
| 1854 | uword _p = (u32) ~ 0, *p = &_p; |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1855 | u32 ls_index; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1856 | u32 **eid_indexes; |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1857 | int ret = 0; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1858 | |
Andrej Kozemcak | 8ebb2a1 | 2016-06-07 12:25:20 +0200 | [diff] [blame] | 1859 | if (vnet_lisp_enable_disable_status () == 0) |
| 1860 | { |
| 1861 | clib_warning ("LISP is disabled!"); |
| 1862 | return VNET_API_ERROR_LISP_DISABLED; |
| 1863 | } |
| 1864 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1865 | if (a->is_add) |
| 1866 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1867 | p = get_locator_set_index (a, p); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1868 | |
| 1869 | /* overwrite */ |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1870 | if (p && p[0] != (u32) ~ 0) |
| 1871 | { |
| 1872 | ls = pool_elt_at_index (lcm->locator_set_pool, p[0]); |
| 1873 | if (!ls) |
| 1874 | { |
| 1875 | clib_warning ("locator-set %d to be overwritten doesn't exist!", |
| 1876 | p[0]); |
| 1877 | return -1; |
| 1878 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1879 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1880 | /* clean locator to locator-set vectors and remove locators if |
| 1881 | * they're not part of another locator-set */ |
| 1882 | clean_locator_to_locator_set (lcm, p[0]); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1883 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1884 | /* remove locator indices from locator set */ |
| 1885 | vec_free (ls->locator_indices); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1886 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1887 | ls_index = p[0]; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1888 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1889 | if (ls_result) |
| 1890 | ls_result[0] = p[0]; |
| 1891 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1892 | /* new locator-set */ |
| 1893 | else |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1894 | { |
| 1895 | pool_get (lcm->locator_set_pool, ls); |
| 1896 | memset (ls, 0, sizeof (*ls)); |
| 1897 | ls_index = ls - lcm->locator_set_pool; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1898 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1899 | if (a->local) |
| 1900 | { |
| 1901 | ls->name = vec_dup (a->name); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1902 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1903 | if (!lcm->locator_set_index_by_name) |
| 1904 | lcm->locator_set_index_by_name = hash_create_vec ( |
| 1905 | /* size */ |
| 1906 | 0, |
| 1907 | sizeof |
| 1908 | (ls->name |
| 1909 | [0]), |
| 1910 | sizeof |
| 1911 | (uword)); |
| 1912 | hash_set_mem (lcm->locator_set_index_by_name, ls->name, |
| 1913 | ls_index); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1914 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1915 | /* mark as local locator-set */ |
| 1916 | vec_add1 (lcm->local_locator_set_indexes, ls_index); |
| 1917 | } |
| 1918 | ls->local = a->local; |
| 1919 | if (ls_result) |
| 1920 | ls_result[0] = ls_index; |
| 1921 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1922 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1923 | ret = vnet_lisp_add_del_locator (a, ls, NULL); |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1924 | if (0 != ret) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1925 | { |
| 1926 | return ret; |
| 1927 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1928 | } |
| 1929 | else |
| 1930 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1931 | p = get_locator_set_index (a, p); |
Andrej Kozemcak | 6a2e439 | 2016-05-26 12:20:08 +0200 | [diff] [blame] | 1932 | if (!p) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1933 | { |
| 1934 | clib_warning ("locator-set %v doesn't exists", a->name); |
| 1935 | return -1; |
| 1936 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1937 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1938 | ls = pool_elt_at_index (lcm->locator_set_pool, p[0]); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1939 | if (!ls) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1940 | { |
| 1941 | clib_warning ("locator-set with index %d doesn't exists", p[0]); |
| 1942 | return -1; |
| 1943 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1944 | |
Andrej Kozemcak | b6e4d39 | 2016-06-14 13:55:57 +0200 | [diff] [blame] | 1945 | if (lcm->mreq_itr_rlocs == p[0]) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1946 | { |
| 1947 | clib_warning ("Can't delete the locator-set used to constrain " |
| 1948 | "the itr-rlocs in map-requests!"); |
| 1949 | return -1; |
| 1950 | } |
Andrej Kozemcak | b6e4d39 | 2016-06-14 13:55:57 +0200 | [diff] [blame] | 1951 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1952 | if (vec_len (lcm->locator_set_to_eids) != 0) |
| 1953 | { |
| 1954 | eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]); |
| 1955 | if (vec_len (eid_indexes[0]) != 0) |
| 1956 | { |
| 1957 | clib_warning |
| 1958 | ("Can't delete a locator that supports a mapping!"); |
| 1959 | return -1; |
| 1960 | } |
| 1961 | } |
Andrej Kozemcak | b92feb6 | 2016-03-31 13:51:42 +0200 | [diff] [blame] | 1962 | |
| 1963 | /* clean locator to locator-sets data */ |
| 1964 | clean_locator_to_locator_set (lcm, p[0]); |
| 1965 | |
| 1966 | if (ls->local) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1967 | { |
| 1968 | u32 it, lsi; |
Andrej Kozemcak | b92feb6 | 2016-03-31 13:51:42 +0200 | [diff] [blame] | 1969 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 1970 | vec_foreach_index (it, lcm->local_locator_set_indexes) |
| 1971 | { |
| 1972 | lsi = vec_elt (lcm->local_locator_set_indexes, it); |
| 1973 | if (lsi == p[0]) |
| 1974 | { |
| 1975 | vec_del1 (lcm->local_locator_set_indexes, it); |
| 1976 | break; |
| 1977 | } |
| 1978 | } |
| 1979 | hash_unset_mem (lcm->locator_set_index_by_name, ls->name); |
| 1980 | } |
| 1981 | vec_free (ls->name); |
| 1982 | vec_free (ls->locator_indices); |
| 1983 | pool_put (lcm->locator_set_pool, ls); |
Andrej Kozemcak | b92feb6 | 2016-03-31 13:51:42 +0200 | [diff] [blame] | 1984 | } |
| 1985 | return 0; |
| 1986 | } |
| 1987 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 1988 | int |
| 1989 | vnet_lisp_rloc_probe_enable_disable (u8 is_enable) |
| 1990 | { |
| 1991 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 1992 | |
| 1993 | lcm->rloc_probing = is_enable; |
| 1994 | return 0; |
| 1995 | } |
| 1996 | |
| 1997 | int |
| 1998 | vnet_lisp_map_register_enable_disable (u8 is_enable) |
| 1999 | { |
| 2000 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 2001 | |
| 2002 | lcm->map_registering = is_enable; |
| 2003 | return 0; |
| 2004 | } |
| 2005 | |
Filip Tehlar | 46d4e36 | 2016-05-09 09:39:26 +0200 | [diff] [blame] | 2006 | clib_error_t * |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 2007 | vnet_lisp_enable_disable (u8 is_enable) |
Filip Tehlar | 46d4e36 | 2016-05-09 09:39:26 +0200 | [diff] [blame] | 2008 | { |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2009 | u32 vni, dp_table; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2010 | clib_error_t *error = 0; |
| 2011 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 2012 | vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a; |
Filip Tehlar | 46d4e36 | 2016-05-09 09:39:26 +0200 | [diff] [blame] | 2013 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 2014 | a->is_en = is_enable; |
Filip Tehlar | 46d4e36 | 2016-05-09 09:39:26 +0200 | [diff] [blame] | 2015 | error = vnet_lisp_gpe_enable_disable (a); |
| 2016 | if (error) |
| 2017 | { |
| 2018 | return clib_error_return (0, "failed to %s data-plane!", |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2019 | a->is_en ? "enable" : "disable"); |
Filip Tehlar | 46d4e36 | 2016-05-09 09:39:26 +0200 | [diff] [blame] | 2020 | } |
| 2021 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 2022 | if (is_enable) |
Filip Tehlar | 46d4e36 | 2016-05-09 09:39:26 +0200 | [diff] [blame] | 2023 | { |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2024 | /* enable all l2 and l3 ifaces */ |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2025 | |
| 2026 | /* *INDENT-OFF* */ |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2027 | hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({ |
| 2028 | dp_add_del_iface(lcm, vni, 0, 1); |
| 2029 | })); |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2030 | hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({ |
| 2031 | dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 2032 | })); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2033 | /* *INDENT-ON* */ |
Filip Tehlar | 46d4e36 | 2016-05-09 09:39:26 +0200 | [diff] [blame] | 2034 | } |
| 2035 | else |
| 2036 | { |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 2037 | /* clear interface table */ |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2038 | hash_free (lcm->fwd_entry_by_mapping_index); |
| 2039 | pool_free (lcm->fwd_entry_pool); |
Filip Tehlar | 46d4e36 | 2016-05-09 09:39:26 +0200 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | /* update global flag */ |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 2043 | lcm->is_enabled = is_enable; |
Filip Tehlar | 46d4e36 | 2016-05-09 09:39:26 +0200 | [diff] [blame] | 2044 | |
| 2045 | return 0; |
| 2046 | } |
| 2047 | |
Filip Tehlar | 46d4e36 | 2016-05-09 09:39:26 +0200 | [diff] [blame] | 2048 | u8 |
| 2049 | vnet_lisp_enable_disable_status (void) |
| 2050 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2051 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
Filip Tehlar | 46d4e36 | 2016-05-09 09:39:26 +0200 | [diff] [blame] | 2052 | return lcm->is_enabled; |
| 2053 | } |
| 2054 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2055 | int |
| 2056 | vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a) |
| 2057 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2058 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2059 | u32 i; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2060 | lisp_msmr_t _mr, *mr = &_mr; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2061 | |
Andrej Kozemcak | 8ebb2a1 | 2016-06-07 12:25:20 +0200 | [diff] [blame] | 2062 | if (vnet_lisp_enable_disable_status () == 0) |
| 2063 | { |
| 2064 | clib_warning ("LISP is disabled!"); |
| 2065 | return VNET_API_ERROR_LISP_DISABLED; |
| 2066 | } |
| 2067 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2068 | if (a->is_add) |
| 2069 | { |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2070 | |
| 2071 | if (get_map_resolver (&a->address)) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2072 | { |
| 2073 | clib_warning ("map-resolver %U already exists!", format_ip_address, |
| 2074 | &a->address); |
| 2075 | return -1; |
| 2076 | } |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2077 | |
| 2078 | memset (mr, 0, sizeof (*mr)); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2079 | ip_address_copy (&mr->address, &a->address); |
| 2080 | vec_add1 (lcm->map_resolvers, *mr); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2081 | |
| 2082 | if (vec_len (lcm->map_resolvers) == 1) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2083 | lcm->do_map_resolver_election = 1; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2084 | } |
| 2085 | else |
| 2086 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2087 | for (i = 0; i < vec_len (lcm->map_resolvers); i++) |
| 2088 | { |
| 2089 | mr = vec_elt_at_index (lcm->map_resolvers, i); |
| 2090 | if (!ip_address_cmp (&mr->address, &a->address)) |
| 2091 | { |
| 2092 | if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver)) |
| 2093 | lcm->do_map_resolver_election = 1; |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2094 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2095 | vec_del1 (lcm->map_resolvers, i); |
| 2096 | break; |
| 2097 | } |
| 2098 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2099 | } |
| 2100 | return 0; |
| 2101 | } |
| 2102 | |
Andrej Kozemcak | b6e4d39 | 2016-06-14 13:55:57 +0200 | [diff] [blame] | 2103 | int |
Filip Tehlar | 1e553a0 | 2017-08-02 12:45:07 +0200 | [diff] [blame] | 2104 | vnet_lisp_map_register_set_ttl (u32 ttl) |
| 2105 | { |
| 2106 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 2107 | lcm->map_register_ttl = ttl; |
| 2108 | return 0; |
| 2109 | } |
| 2110 | |
| 2111 | u32 |
| 2112 | vnet_lisp_map_register_get_ttl (void) |
| 2113 | { |
| 2114 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 2115 | return lcm->map_register_ttl; |
| 2116 | } |
| 2117 | |
| 2118 | int |
Andrej Kozemcak | b6e4d39 | 2016-06-14 13:55:57 +0200 | [diff] [blame] | 2119 | vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a) |
| 2120 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2121 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 2122 | uword *p = 0; |
Andrej Kozemcak | b6e4d39 | 2016-06-14 13:55:57 +0200 | [diff] [blame] | 2123 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 2124 | if (vnet_lisp_enable_disable_status () == 0) |
| 2125 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2126 | clib_warning ("LISP is disabled!"); |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 2127 | return VNET_API_ERROR_LISP_DISABLED; |
| 2128 | } |
Andrej Kozemcak | b6e4d39 | 2016-06-14 13:55:57 +0200 | [diff] [blame] | 2129 | |
| 2130 | if (a->is_add) |
| 2131 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2132 | p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name); |
Andrej Kozemcak | b6e4d39 | 2016-06-14 13:55:57 +0200 | [diff] [blame] | 2133 | if (!p) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2134 | { |
| 2135 | clib_warning ("locator-set %v doesn't exist", a->locator_set_name); |
| 2136 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 2137 | } |
Andrej Kozemcak | b6e4d39 | 2016-06-14 13:55:57 +0200 | [diff] [blame] | 2138 | |
| 2139 | lcm->mreq_itr_rlocs = p[0]; |
| 2140 | } |
| 2141 | else |
| 2142 | { |
| 2143 | lcm->mreq_itr_rlocs = ~0; |
| 2144 | } |
| 2145 | |
| 2146 | return 0; |
| 2147 | } |
| 2148 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2149 | /* Statistics (not really errors) */ |
| 2150 | #define foreach_lisp_cp_lookup_error \ |
| 2151 | _(DROP, "drop") \ |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 2152 | _(MAP_REQUESTS_SENT, "map-request sent") \ |
| 2153 | _(ARP_REPLY_TX, "ARP replies sent") |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2154 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2155 | static char *lisp_cp_lookup_error_strings[] = { |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2156 | #define _(sym,string) string, |
| 2157 | foreach_lisp_cp_lookup_error |
| 2158 | #undef _ |
| 2159 | }; |
| 2160 | |
| 2161 | typedef enum |
| 2162 | { |
| 2163 | #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2164 | foreach_lisp_cp_lookup_error |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2165 | #undef _ |
| 2166 | LISP_CP_LOOKUP_N_ERROR, |
| 2167 | } lisp_cp_lookup_error_t; |
| 2168 | |
| 2169 | typedef enum |
| 2170 | { |
| 2171 | LISP_CP_LOOKUP_NEXT_DROP, |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 2172 | LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX, |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2173 | LISP_CP_LOOKUP_N_NEXT, |
| 2174 | } lisp_cp_lookup_next_t; |
| 2175 | |
| 2176 | typedef struct |
| 2177 | { |
| 2178 | gid_address_t dst_eid; |
Andrej Kozemcak | 94e3476 | 2016-05-25 12:43:21 +0200 | [diff] [blame] | 2179 | ip_address_t map_resolver_ip; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2180 | } lisp_cp_lookup_trace_t; |
| 2181 | |
| 2182 | u8 * |
| 2183 | format_lisp_cp_lookup_trace (u8 * s, va_list * args) |
| 2184 | { |
| 2185 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 2186 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2187 | lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2188 | |
| 2189 | s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U", |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2190 | format_ip_address, &t->map_resolver_ip, format_gid_address, |
| 2191 | &t->dst_eid); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2192 | return s; |
| 2193 | } |
| 2194 | |
Florin Coras | 1c49484 | 2016-06-16 21:08:56 +0200 | [diff] [blame] | 2195 | int |
| 2196 | get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2197 | ip_address_t * sloc) |
Florin Coras | c2c9008 | 2016-06-13 18:37:15 +0200 | [diff] [blame] | 2198 | { |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2199 | lisp_msmr_t *mrit; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2200 | ip_address_t *a; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2201 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2202 | if (vec_len (lcm->map_resolvers) == 0) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2203 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2204 | clib_warning ("No map-resolver configured"); |
Florin Coras | 1c49484 | 2016-06-16 21:08:56 +0200 | [diff] [blame] | 2205 | return 0; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2206 | } |
| 2207 | |
Florin Coras | ddfafb8 | 2016-05-13 18:09:56 +0200 | [diff] [blame] | 2208 | /* find the first mr ip we have a route to and the ip of the |
| 2209 | * iface that has a route to it */ |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2210 | vec_foreach (mrit, lcm->map_resolvers) |
| 2211 | { |
| 2212 | a = &mrit->address; |
| 2213 | if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc)) |
| 2214 | { |
| 2215 | ip_address_copy (mr_ip, a); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2216 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2217 | /* also update globals */ |
| 2218 | return 1; |
| 2219 | } |
| 2220 | } |
Florin Coras | ddfafb8 | 2016-05-13 18:09:56 +0200 | [diff] [blame] | 2221 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2222 | clib_warning ("Can't find map-resolver and local interface ip!"); |
Florin Coras | 1c49484 | 2016-06-16 21:08:56 +0200 | [diff] [blame] | 2223 | return 0; |
Florin Coras | ddfafb8 | 2016-05-13 18:09:56 +0200 | [diff] [blame] | 2224 | } |
Filip Tehlar | 254b036 | 2016-04-07 10:04:34 +0200 | [diff] [blame] | 2225 | |
Filip Tehlar | beceab9 | 2016-04-20 17:21:55 +0200 | [diff] [blame] | 2226 | static gid_address_t * |
Filip Tehlar | 254b036 | 2016-04-07 10:04:34 +0200 | [diff] [blame] | 2227 | build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set) |
| 2228 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2229 | void *addr; |
Filip Tehlar | 254b036 | 2016-04-07 10:04:34 +0200 | [diff] [blame] | 2230 | u32 i; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2231 | locator_t *loc; |
| 2232 | u32 *loc_indexp; |
| 2233 | ip_interface_address_t *ia = 0; |
| 2234 | gid_address_t gid_data, *gid = &gid_data; |
| 2235 | gid_address_t *rlocs = 0; |
| 2236 | ip_prefix_t *ippref = &gid_address_ippref (gid); |
| 2237 | ip_address_t *rloc = &ip_prefix_addr (ippref); |
Filip Tehlar | 254b036 | 2016-04-07 10:04:34 +0200 | [diff] [blame] | 2238 | |
Filip Tehlar | 324112f | 2016-06-02 16:07:38 +0200 | [diff] [blame] | 2239 | memset (gid, 0, sizeof (gid[0])); |
Filip Tehlar | beceab9 | 2016-04-20 17:21:55 +0200 | [diff] [blame] | 2240 | gid_address_type (gid) = GID_ADDR_IP_PREFIX; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2241 | for (i = 0; i < vec_len (loc_set->locator_indices); i++) |
Filip Tehlar | 254b036 | 2016-04-07 10:04:34 +0200 | [diff] [blame] | 2242 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2243 | loc_indexp = vec_elt_at_index (loc_set->locator_indices, i); |
Filip Tehlar | 254b036 | 2016-04-07 10:04:34 +0200 | [diff] [blame] | 2244 | loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]); |
| 2245 | |
Filip Tehlar | 254b036 | 2016-04-07 10:04:34 +0200 | [diff] [blame] | 2246 | /* Add ipv4 locators first TODO sort them */ |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2247 | |
| 2248 | /* *INDENT-OFF* */ |
Filip Tehlar | 254b036 | 2016-04-07 10:04:34 +0200 | [diff] [blame] | 2249 | foreach_ip_interface_address (&lcm->im4->lookup_main, ia, |
| 2250 | loc->sw_if_index, 1 /* unnumbered */, |
| 2251 | ({ |
Florin Coras | 1c49484 | 2016-06-16 21:08:56 +0200 | [diff] [blame] | 2252 | addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia); |
| 2253 | ip_address_set (rloc, addr, IP4); |
Florin Coras | ddfafb8 | 2016-05-13 18:09:56 +0200 | [diff] [blame] | 2254 | ip_prefix_len (ippref) = 32; |
Andrej Kozemcak | 438109d | 2016-07-22 12:54:12 +0200 | [diff] [blame] | 2255 | ip_prefix_normalize (ippref); |
Filip Tehlar | beceab9 | 2016-04-20 17:21:55 +0200 | [diff] [blame] | 2256 | vec_add1 (rlocs, gid[0]); |
Filip Tehlar | 254b036 | 2016-04-07 10:04:34 +0200 | [diff] [blame] | 2257 | })); |
| 2258 | |
Filip Tehlar | 254b036 | 2016-04-07 10:04:34 +0200 | [diff] [blame] | 2259 | /* Add ipv6 locators */ |
| 2260 | foreach_ip_interface_address (&lcm->im6->lookup_main, ia, |
| 2261 | loc->sw_if_index, 1 /* unnumbered */, |
| 2262 | ({ |
Florin Coras | 1c49484 | 2016-06-16 21:08:56 +0200 | [diff] [blame] | 2263 | addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia); |
| 2264 | ip_address_set (rloc, addr, IP6); |
Florin Coras | ddfafb8 | 2016-05-13 18:09:56 +0200 | [diff] [blame] | 2265 | ip_prefix_len (ippref) = 128; |
Andrej Kozemcak | 438109d | 2016-07-22 12:54:12 +0200 | [diff] [blame] | 2266 | ip_prefix_normalize (ippref); |
Filip Tehlar | beceab9 | 2016-04-20 17:21:55 +0200 | [diff] [blame] | 2267 | vec_add1 (rlocs, gid[0]); |
Filip Tehlar | 254b036 | 2016-04-07 10:04:34 +0200 | [diff] [blame] | 2268 | })); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2269 | /* *INDENT-ON* */ |
| 2270 | |
Filip Tehlar | 254b036 | 2016-04-07 10:04:34 +0200 | [diff] [blame] | 2271 | } |
| 2272 | return rlocs; |
| 2273 | } |
| 2274 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2275 | static vlib_buffer_t * |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2276 | build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid, |
| 2277 | ip_address_t * sloc, ip_address_t * rloc, |
| 2278 | gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res) |
| 2279 | { |
| 2280 | vlib_buffer_t *b; |
| 2281 | u32 bi; |
| 2282 | vlib_main_t *vm = lcm->vlib_main; |
| 2283 | |
| 2284 | if (vlib_buffer_alloc (vm, &bi, 1) != 1) |
| 2285 | { |
| 2286 | clib_warning ("Can't allocate buffer for Map-Request!"); |
| 2287 | return 0; |
| 2288 | } |
| 2289 | |
| 2290 | b = vlib_get_buffer (vm, bi); |
| 2291 | |
| 2292 | /* leave some space for the encap headers */ |
| 2293 | vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN); |
| 2294 | |
| 2295 | /* put lisp msg */ |
| 2296 | lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ , |
| 2297 | 1 /* rloc probe */ , nonce_res); |
| 2298 | |
| 2299 | /* push outer ip header */ |
| 2300 | pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc, |
Florin Coras | fdbc382 | 2017-07-27 00:34:12 -0700 | [diff] [blame] | 2301 | rloc, 1); |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2302 | |
| 2303 | bi_res[0] = bi; |
| 2304 | |
| 2305 | return b; |
| 2306 | } |
| 2307 | |
| 2308 | static vlib_buffer_t * |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2309 | build_encapsulated_map_request (lisp_cp_main_t * lcm, |
| 2310 | gid_address_t * seid, gid_address_t * deid, |
| 2311 | locator_set_t * loc_set, ip_address_t * mr_ip, |
| 2312 | ip_address_t * sloc, u8 is_smr_invoked, |
| 2313 | u64 * nonce_res, u32 * bi_res) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2314 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2315 | vlib_buffer_t *b; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2316 | u32 bi; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2317 | gid_address_t *rlocs = 0; |
| 2318 | vlib_main_t *vm = lcm->vlib_main; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2319 | |
| 2320 | if (vlib_buffer_alloc (vm, &bi, 1) != 1) |
| 2321 | { |
| 2322 | clib_warning ("Can't allocate buffer for Map-Request!"); |
| 2323 | return 0; |
| 2324 | } |
| 2325 | |
| 2326 | b = vlib_get_buffer (vm, bi); |
Florin Coras | fdbc382 | 2017-07-27 00:34:12 -0700 | [diff] [blame] | 2327 | b->flags = 0; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2328 | |
| 2329 | /* leave some space for the encap headers */ |
| 2330 | vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN); |
| 2331 | |
Filip Tehlar | 254b036 | 2016-04-07 10:04:34 +0200 | [diff] [blame] | 2332 | /* get rlocs */ |
| 2333 | rlocs = build_itr_rloc_list (lcm, loc_set); |
| 2334 | |
Filip Tehlar | d5fcc46 | 2016-10-17 16:20:18 +0200 | [diff] [blame] | 2335 | if (MR_MODE_SRC_DST == lcm->map_request_mode |
| 2336 | && GID_ADDR_SRC_DST != gid_address_type (deid)) |
Florin Coras | dca8804 | 2016-09-14 16:01:38 +0200 | [diff] [blame] | 2337 | { |
| 2338 | gid_address_t sd; |
| 2339 | memset (&sd, 0, sizeof (sd)); |
| 2340 | build_src_dst (&sd, seid, deid); |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2341 | lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked, |
| 2342 | 0 /* rloc probe */ , nonce_res); |
Florin Coras | dca8804 | 2016-09-14 16:01:38 +0200 | [diff] [blame] | 2343 | } |
| 2344 | else |
| 2345 | { |
| 2346 | /* put lisp msg */ |
| 2347 | lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked, |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2348 | 0 /* rloc probe */ , nonce_res); |
Florin Coras | dca8804 | 2016-09-14 16:01:38 +0200 | [diff] [blame] | 2349 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2350 | |
| 2351 | /* push ecm: udp-ip-lisp */ |
| 2352 | lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid); |
| 2353 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2354 | /* push outer ip header */ |
Florin Coras | ddfafb8 | 2016-05-13 18:09:56 +0200 | [diff] [blame] | 2355 | pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc, |
Florin Coras | fdbc382 | 2017-07-27 00:34:12 -0700 | [diff] [blame] | 2356 | mr_ip, 1); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2357 | |
| 2358 | bi_res[0] = bi; |
Filip Tehlar | 254b036 | 2016-04-07 10:04:34 +0200 | [diff] [blame] | 2359 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2360 | vec_free (rlocs); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2361 | return b; |
| 2362 | } |
| 2363 | |
| 2364 | static void |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2365 | reset_pending_mr_counters (pending_map_request_t * r) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2366 | { |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2367 | r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME; |
| 2368 | r->retries_num = 0; |
| 2369 | } |
| 2370 | |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 2371 | #define foreach_msmr \ |
| 2372 | _(server) \ |
| 2373 | _(resolver) |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2374 | |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 2375 | #define _(name) \ |
| 2376 | static int \ |
| 2377 | elect_map_ ## name (lisp_cp_main_t * lcm) \ |
| 2378 | { \ |
| 2379 | lisp_msmr_t *mr; \ |
| 2380 | vec_foreach (mr, lcm->map_ ## name ## s) \ |
| 2381 | { \ |
| 2382 | if (!mr->is_down) \ |
| 2383 | { \ |
| 2384 | ip_address_copy (&lcm->active_map_ ##name, &mr->address); \ |
| 2385 | lcm->do_map_ ## name ## _election = 0; \ |
| 2386 | return 1; \ |
| 2387 | } \ |
| 2388 | } \ |
| 2389 | return 0; \ |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2390 | } |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 2391 | foreach_msmr |
| 2392 | #undef _ |
| 2393 | static void |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2394 | free_map_register_records (mapping_t * maps) |
| 2395 | { |
| 2396 | mapping_t *map; |
| 2397 | vec_foreach (map, maps) vec_free (map->locators); |
| 2398 | |
| 2399 | vec_free (maps); |
| 2400 | } |
| 2401 | |
| 2402 | static void |
| 2403 | add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index, |
| 2404 | ip_address_t * probed_loc) |
| 2405 | { |
| 2406 | u32 *li; |
| 2407 | locator_t *loc, new; |
| 2408 | ip_interface_address_t *ia = 0; |
| 2409 | void *addr; |
| 2410 | ip_address_t *new_ip = &gid_address_ip (&new.address); |
| 2411 | |
| 2412 | m->locators = 0; |
| 2413 | locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, |
| 2414 | locator_set_index); |
| 2415 | vec_foreach (li, ls->locator_indices) |
| 2416 | { |
| 2417 | loc = pool_elt_at_index (lcm->locator_pool, li[0]); |
| 2418 | new = loc[0]; |
| 2419 | if (loc->local) |
| 2420 | { |
| 2421 | /* *INDENT-OFF* */ |
| 2422 | foreach_ip_interface_address (&lcm->im4->lookup_main, ia, |
| 2423 | loc->sw_if_index, 1 /* unnumbered */, |
| 2424 | ({ |
| 2425 | addr = ip_interface_address_get_address (&lcm->im4->lookup_main, |
| 2426 | ia); |
| 2427 | ip_address_set (new_ip, addr, IP4); |
| 2428 | })); |
| 2429 | |
| 2430 | /* Add ipv6 locators */ |
| 2431 | foreach_ip_interface_address (&lcm->im6->lookup_main, ia, |
| 2432 | loc->sw_if_index, 1 /* unnumbered */, |
| 2433 | ({ |
| 2434 | addr = ip_interface_address_get_address (&lcm->im6->lookup_main, |
| 2435 | ia); |
| 2436 | ip_address_set (new_ip, addr, IP6); |
| 2437 | })); |
| 2438 | /* *INDENT-ON* */ |
| 2439 | |
| 2440 | if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0) |
| 2441 | new.probed = 1; |
| 2442 | } |
| 2443 | vec_add1 (m->locators, new); |
| 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | static mapping_t * |
| 2448 | build_map_register_record_list (lisp_cp_main_t * lcm) |
| 2449 | { |
| 2450 | mapping_t *recs = 0, rec, *m; |
| 2451 | |
| 2452 | /* *INDENT-OFF* */ |
| 2453 | pool_foreach(m, lcm->mapping_pool, |
| 2454 | { |
| 2455 | /* for now build only local mappings */ |
| 2456 | if (!m->local) |
| 2457 | continue; |
| 2458 | |
| 2459 | rec = m[0]; |
| 2460 | add_locators (lcm, &rec, m->locator_set_index, NULL); |
| 2461 | vec_add1 (recs, rec); |
| 2462 | }); |
| 2463 | /* *INDENT-ON* */ |
| 2464 | |
| 2465 | return recs; |
| 2466 | } |
| 2467 | |
| 2468 | static int |
| 2469 | update_map_register_auth_data (map_register_hdr_t * map_reg_hdr, |
| 2470 | lisp_key_type_t key_id, u8 * key, |
| 2471 | u16 auth_data_len, u32 msg_len) |
| 2472 | { |
| 2473 | MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id); |
| 2474 | MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len); |
| 2475 | |
| 2476 | unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key), |
| 2477 | (unsigned char *) map_reg_hdr, msg_len, NULL, |
| 2478 | NULL); |
| 2479 | clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len); |
| 2480 | |
| 2481 | return 0; |
| 2482 | } |
| 2483 | |
| 2484 | static vlib_buffer_t * |
| 2485 | build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc, |
| 2486 | ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif, |
| 2487 | mapping_t * records, lisp_key_type_t key_id, u8 * key, |
| 2488 | u32 * bi_res) |
| 2489 | { |
| 2490 | void *map_reg_hdr; |
| 2491 | vlib_buffer_t *b; |
| 2492 | u32 bi, auth_data_len = 0, msg_len = 0; |
| 2493 | vlib_main_t *vm = lcm->vlib_main; |
| 2494 | |
| 2495 | if (vlib_buffer_alloc (vm, &bi, 1) != 1) |
| 2496 | { |
| 2497 | clib_warning ("Can't allocate buffer for Map-Register!"); |
| 2498 | return 0; |
| 2499 | } |
| 2500 | |
| 2501 | b = vlib_get_buffer (vm, bi); |
| 2502 | |
| 2503 | /* leave some space for the encap headers */ |
| 2504 | vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN); |
| 2505 | |
| 2506 | auth_data_len = auth_data_len_by_key_id (key_id); |
| 2507 | map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif, |
| 2508 | auth_data_len, nonce_res, |
| 2509 | &msg_len); |
| 2510 | |
| 2511 | update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len, |
| 2512 | msg_len); |
| 2513 | |
| 2514 | /* push outer ip header */ |
| 2515 | pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc, |
Florin Coras | fdbc382 | 2017-07-27 00:34:12 -0700 | [diff] [blame] | 2516 | ms_ip, 1); |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2517 | |
| 2518 | bi_res[0] = bi; |
| 2519 | return b; |
| 2520 | } |
| 2521 | |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 2522 | #define _(name) \ |
| 2523 | static int \ |
| 2524 | get_egress_map_ ##name## _ip (lisp_cp_main_t * lcm, ip_address_t * ip) \ |
| 2525 | { \ |
| 2526 | lisp_msmr_t *mr; \ |
| 2527 | while (lcm->do_map_ ## name ## _election \ |
| 2528 | | (0 == ip_fib_get_first_egress_ip_for_dst \ |
| 2529 | (lcm, &lcm->active_map_ ##name, ip))) \ |
| 2530 | { \ |
| 2531 | if (0 == elect_map_ ## name (lcm)) \ |
| 2532 | /* all map resolvers/servers are down */ \ |
| 2533 | { \ |
| 2534 | /* restart MR/MS checking by marking all of them up */ \ |
| 2535 | vec_foreach (mr, lcm->map_ ## name ## s) mr->is_down = 0; \ |
| 2536 | return -1; \ |
| 2537 | } \ |
| 2538 | } \ |
| 2539 | return 0; \ |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2540 | } |
| 2541 | |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 2542 | foreach_msmr |
| 2543 | #undef _ |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 2544 | /* CP output statistics */ |
| 2545 | #define foreach_lisp_cp_output_error \ |
| 2546 | _(MAP_REGISTERS_SENT, "map-registers sent") \ |
| 2547 | _(RLOC_PROBES_SENT, "rloc-probes sent") |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 2548 | static char *lisp_cp_output_error_strings[] = { |
| 2549 | #define _(sym,string) string, |
| 2550 | foreach_lisp_cp_output_error |
| 2551 | #undef _ |
| 2552 | }; |
| 2553 | |
| 2554 | typedef enum |
| 2555 | { |
| 2556 | #define _(sym,str) LISP_CP_OUTPUT_ERROR_##sym, |
| 2557 | foreach_lisp_cp_output_error |
| 2558 | #undef _ |
| 2559 | LISP_CP_OUTPUT_N_ERROR, |
| 2560 | } lisp_cp_output_error_t; |
| 2561 | |
| 2562 | static uword |
| 2563 | lisp_cp_output (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 2564 | vlib_frame_t * from_frame) |
| 2565 | { |
| 2566 | return 0; |
| 2567 | } |
| 2568 | |
| 2569 | /* dummy node used only for statistics */ |
| 2570 | /* *INDENT-OFF* */ |
| 2571 | VLIB_REGISTER_NODE (lisp_cp_output_node) = { |
| 2572 | .function = lisp_cp_output, |
| 2573 | .name = "lisp-cp-output", |
| 2574 | .vector_size = sizeof (u32), |
| 2575 | .format_trace = format_lisp_cp_input_trace, |
| 2576 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 2577 | |
| 2578 | .n_errors = LISP_CP_OUTPUT_N_ERROR, |
| 2579 | .error_strings = lisp_cp_output_error_strings, |
| 2580 | |
| 2581 | .n_next_nodes = LISP_CP_INPUT_N_NEXT, |
| 2582 | |
| 2583 | .next_nodes = { |
| 2584 | [LISP_CP_INPUT_NEXT_DROP] = "error-drop", |
| 2585 | }, |
| 2586 | }; |
| 2587 | /* *INDENT-ON* */ |
| 2588 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2589 | static int |
| 2590 | send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid, |
| 2591 | u32 local_locator_set_index, ip_address_t * sloc, |
| 2592 | ip_address_t * rloc) |
| 2593 | { |
| 2594 | locator_set_t *ls; |
| 2595 | u32 bi; |
| 2596 | vlib_buffer_t *b; |
| 2597 | vlib_frame_t *f; |
| 2598 | u64 nonce = 0; |
| 2599 | u32 next_index, *to_next; |
| 2600 | gid_address_t *itr_rlocs; |
| 2601 | |
| 2602 | ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index); |
| 2603 | itr_rlocs = build_itr_rloc_list (lcm, ls); |
| 2604 | |
| 2605 | b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi); |
| 2606 | vec_free (itr_rlocs); |
| 2607 | if (!b) |
| 2608 | return -1; |
| 2609 | |
| 2610 | vnet_buffer (b)->sw_if_index[VLIB_TX] = 0; |
| 2611 | |
Filip Tehlar | 272c69e | 2017-02-15 16:40:35 +0100 | [diff] [blame] | 2612 | next_index = (ip_addr_version (rloc) == IP4) ? |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2613 | ip4_lookup_node.index : ip6_lookup_node.index; |
| 2614 | |
| 2615 | f = vlib_get_frame_to_node (lcm->vlib_main, next_index); |
| 2616 | |
| 2617 | /* Enqueue the packet */ |
| 2618 | to_next = vlib_frame_vector_args (f); |
| 2619 | to_next[0] = bi; |
| 2620 | f->n_vectors = 1; |
| 2621 | vlib_put_frame_to_node (lcm->vlib_main, next_index, f); |
| 2622 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2623 | return 0; |
| 2624 | } |
| 2625 | |
| 2626 | static int |
| 2627 | send_rloc_probes (lisp_cp_main_t * lcm) |
| 2628 | { |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 2629 | u8 lprio = 0; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2630 | mapping_t *lm; |
| 2631 | fwd_entry_t *e; |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 2632 | locator_pair_t *lp; |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 2633 | u32 si, rloc_probes_sent = 0; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2634 | |
| 2635 | /* *INDENT-OFF* */ |
| 2636 | pool_foreach (e, lcm->fwd_entry_pool, |
| 2637 | { |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 2638 | if (vec_len (e->locator_pairs) == 0) |
| 2639 | continue; |
| 2640 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2641 | si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid); |
| 2642 | if (~0 == si) |
| 2643 | { |
| 2644 | clib_warning ("internal error: cannot find local eid %U in " |
| 2645 | "map-cache!", format_gid_address, &e->leid); |
| 2646 | continue; |
| 2647 | } |
| 2648 | lm = pool_elt_at_index (lcm->mapping_pool, si); |
| 2649 | |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 2650 | /* get the best (lowest) priority */ |
| 2651 | lprio = e->locator_pairs[0].priority; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2652 | |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 2653 | /* send rloc-probe for pair(s) with the best remote locator priority */ |
| 2654 | vec_foreach (lp, e->locator_pairs) |
| 2655 | { |
| 2656 | if (lp->priority != lprio) |
| 2657 | break; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2658 | |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 2659 | /* get first remote locator */ |
| 2660 | send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc, |
| 2661 | &lp->rmt_loc); |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 2662 | rloc_probes_sent++; |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 2663 | } |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2664 | }); |
| 2665 | /* *INDENT-ON* */ |
| 2666 | |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 2667 | vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index, |
| 2668 | LISP_CP_OUTPUT_ERROR_RLOC_PROBES_SENT, |
| 2669 | rloc_probes_sent); |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2670 | return 0; |
| 2671 | } |
| 2672 | |
| 2673 | static int |
| 2674 | send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif) |
| 2675 | { |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 2676 | pending_map_register_t *pmr; |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 2677 | u32 bi, map_registers_sent = 0; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2678 | vlib_buffer_t *b; |
| 2679 | ip_address_t sloc; |
| 2680 | vlib_frame_t *f; |
| 2681 | u64 nonce = 0; |
| 2682 | u32 next_index, *to_next; |
Filip Tehlar | cf121c8 | 2017-05-03 10:12:44 +0200 | [diff] [blame] | 2683 | mapping_t *records, *r, *group, *k; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2684 | |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 2685 | if (get_egress_map_server_ip (lcm, &sloc) < 0) |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2686 | return -1; |
| 2687 | |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 2688 | records = build_map_register_record_list (lcm); |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2689 | if (!records) |
| 2690 | return -1; |
| 2691 | |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 2692 | vec_foreach (r, records) |
| 2693 | { |
| 2694 | u8 *key = r->key; |
| 2695 | u8 key_id = r->key_id; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2696 | |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 2697 | if (!key) |
| 2698 | continue; /* no secret key -> map-register cannot be sent */ |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2699 | |
Filip Tehlar | cf121c8 | 2017-05-03 10:12:44 +0200 | [diff] [blame] | 2700 | group = 0; |
| 2701 | vec_add1 (group, r[0]); |
| 2702 | |
| 2703 | /* group mappings that share common key */ |
| 2704 | for (k = r + 1; k < vec_end (records); k++) |
| 2705 | { |
| 2706 | if (k->key_id != r->key_id) |
| 2707 | continue; |
| 2708 | |
| 2709 | if (vec_is_equal (k->key, r->key)) |
| 2710 | { |
| 2711 | vec_add1 (group, k[0]); |
| 2712 | k->key = 0; /* don't process this mapping again */ |
| 2713 | } |
| 2714 | } |
| 2715 | |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 2716 | b = build_map_register (lcm, &sloc, &lcm->active_map_server, &nonce, |
| 2717 | want_map_notif, group, key_id, key, &bi); |
Filip Tehlar | cf121c8 | 2017-05-03 10:12:44 +0200 | [diff] [blame] | 2718 | vec_free (group); |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 2719 | if (!b) |
| 2720 | continue; |
| 2721 | |
| 2722 | vnet_buffer (b)->sw_if_index[VLIB_TX] = 0; |
| 2723 | |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 2724 | next_index = (ip_addr_version (&lcm->active_map_server) == IP4) ? |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 2725 | ip4_lookup_node.index : ip6_lookup_node.index; |
| 2726 | |
| 2727 | f = vlib_get_frame_to_node (lcm->vlib_main, next_index); |
| 2728 | |
| 2729 | /* Enqueue the packet */ |
| 2730 | to_next = vlib_frame_vector_args (f); |
| 2731 | to_next[0] = bi; |
| 2732 | f->n_vectors = 1; |
| 2733 | vlib_put_frame_to_node (lcm->vlib_main, next_index, f); |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 2734 | map_registers_sent++; |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 2735 | |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 2736 | pool_get (lcm->pending_map_registers_pool, pmr); |
| 2737 | memset (pmr, 0, sizeof (*pmr)); |
| 2738 | pmr->time_to_expire = PENDING_MREG_EXPIRATION_TIME; |
| 2739 | hash_set (lcm->map_register_messages_by_nonce, nonce, |
| 2740 | pmr - lcm->pending_map_registers_pool); |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 2741 | } |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2742 | free_map_register_records (records); |
| 2743 | |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 2744 | vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index, |
| 2745 | LISP_CP_OUTPUT_ERROR_MAP_REGISTERS_SENT, |
| 2746 | map_registers_sent); |
| 2747 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2748 | return 0; |
| 2749 | } |
| 2750 | |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2751 | #define send_encapsulated_map_request(lcm, seid, deid, smr) \ |
| 2752 | _send_encapsulated_map_request(lcm, seid, deid, smr, 0) |
| 2753 | |
| 2754 | #define resend_encapsulated_map_request(lcm, seid, deid, smr) \ |
| 2755 | _send_encapsulated_map_request(lcm, seid, deid, smr, 1) |
| 2756 | |
| 2757 | static int |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2758 | _send_encapsulated_map_request (lisp_cp_main_t * lcm, |
| 2759 | gid_address_t * seid, gid_address_t * deid, |
| 2760 | u8 is_smr_invoked, u8 is_resend) |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2761 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2762 | u32 next_index, bi = 0, *to_next, map_index; |
| 2763 | vlib_buffer_t *b; |
| 2764 | vlib_frame_t *f; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2765 | u64 nonce = 0; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2766 | locator_set_t *loc_set; |
| 2767 | mapping_t *map; |
| 2768 | pending_map_request_t *pmr, *duplicate_pmr = 0; |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2769 | ip_address_t sloc; |
Andrej Kozemcak | b6e4d39 | 2016-06-14 13:55:57 +0200 | [diff] [blame] | 2770 | u32 ls_index; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2771 | |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2772 | /* if there is already a pending request remember it */ |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2773 | |
| 2774 | /* *INDENT-OFF* */ |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2775 | pool_foreach(pmr, lcm->pending_map_requests_pool, |
| 2776 | ({ |
| 2777 | if (!gid_address_cmp (&pmr->src, seid) |
| 2778 | && !gid_address_cmp (&pmr->dst, deid)) |
| 2779 | { |
| 2780 | duplicate_pmr = pmr; |
| 2781 | break; |
| 2782 | } |
| 2783 | })); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2784 | /* *INDENT-ON* */ |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2785 | |
| 2786 | if (!is_resend && duplicate_pmr) |
| 2787 | { |
| 2788 | /* don't send the request if there is a pending map request already */ |
| 2789 | return 0; |
| 2790 | } |
| 2791 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2792 | /* get locator-set for seid */ |
Filip Tehlar | ef2a5bf | 2017-05-30 07:14:46 +0200 | [diff] [blame] | 2793 | if (!lcm->lisp_pitr && gid_address_type (deid) != GID_ADDR_NSH) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2794 | { |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 2795 | map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid); |
| 2796 | if (map_index == ~0) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2797 | { |
| 2798 | clib_warning ("No local mapping found in eid-table for %U!", |
| 2799 | format_gid_address, seid); |
| 2800 | return -1; |
| 2801 | } |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 2802 | |
| 2803 | map = pool_elt_at_index (lcm->mapping_pool, map_index); |
| 2804 | |
| 2805 | if (!map->local) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2806 | { |
| 2807 | clib_warning |
| 2808 | ("Mapping found for src eid %U is not marked as local!", |
| 2809 | format_gid_address, seid); |
| 2810 | return -1; |
| 2811 | } |
Andrej Kozemcak | b6e4d39 | 2016-06-14 13:55:57 +0200 | [diff] [blame] | 2812 | ls_index = map->locator_set_index; |
Filip Tehlar | 53f09e3 | 2016-05-19 14:25:44 +0200 | [diff] [blame] | 2813 | } |
| 2814 | else |
| 2815 | { |
Filip Tehlar | ef2a5bf | 2017-05-30 07:14:46 +0200 | [diff] [blame] | 2816 | if (lcm->lisp_pitr) |
| 2817 | { |
| 2818 | map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index); |
| 2819 | ls_index = map->locator_set_index; |
| 2820 | } |
| 2821 | else |
| 2822 | { |
| 2823 | if (lcm->nsh_map_index == (u32) ~ 0) |
| 2824 | { |
| 2825 | clib_warning ("No locator-set defined for NSH!"); |
| 2826 | return -1; |
| 2827 | } |
| 2828 | else |
| 2829 | { |
| 2830 | map = pool_elt_at_index (lcm->mapping_pool, lcm->nsh_map_index); |
| 2831 | ls_index = map->locator_set_index; |
| 2832 | } |
| 2833 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2834 | } |
| 2835 | |
Andrej Kozemcak | b6e4d39 | 2016-06-14 13:55:57 +0200 | [diff] [blame] | 2836 | /* overwrite locator set if map-request itr-rlocs configured */ |
| 2837 | if (~0 != lcm->mreq_itr_rlocs) |
| 2838 | { |
| 2839 | ls_index = lcm->mreq_itr_rlocs; |
| 2840 | } |
| 2841 | |
| 2842 | loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2843 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2844 | if (get_egress_map_resolver_ip (lcm, &sloc) < 0) |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2845 | { |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 2846 | if (duplicate_pmr) |
| 2847 | duplicate_pmr->to_be_removed = 1; |
| 2848 | return -1; |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2849 | } |
Florin Coras | ddfafb8 | 2016-05-13 18:09:56 +0200 | [diff] [blame] | 2850 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2851 | /* build the encapsulated map request */ |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2852 | b = build_encapsulated_map_request (lcm, seid, deid, loc_set, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2853 | &lcm->active_map_resolver, |
| 2854 | &sloc, is_smr_invoked, &nonce, &bi); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2855 | |
| 2856 | if (!b) |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2857 | return -1; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2858 | |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 2859 | /* set fib index to default and lookup node */ |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2860 | vnet_buffer (b)->sw_if_index[VLIB_TX] = 0; |
| 2861 | next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ? |
| 2862 | ip4_lookup_node.index : ip6_lookup_node.index; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2863 | |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2864 | f = vlib_get_frame_to_node (lcm->vlib_main, next_index); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2865 | |
| 2866 | /* Enqueue the packet */ |
| 2867 | to_next = vlib_frame_vector_args (f); |
| 2868 | to_next[0] = bi; |
| 2869 | f->n_vectors = 1; |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2870 | vlib_put_frame_to_node (lcm->vlib_main, next_index, f); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2871 | |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2872 | if (duplicate_pmr) |
| 2873 | /* if there is a pending request already update it */ |
| 2874 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2875 | if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN) |
| 2876 | { |
| 2877 | /* remove the oldest nonce */ |
| 2878 | u64 CLIB_UNUSED (tmp), *nonce_del; |
| 2879 | nonce_del = clib_fifo_head (duplicate_pmr->nonces); |
| 2880 | hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]); |
| 2881 | clib_fifo_sub1 (duplicate_pmr->nonces, tmp); |
| 2882 | } |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2883 | |
Florin Coras | f4691cd | 2016-08-15 19:16:32 +0200 | [diff] [blame] | 2884 | clib_fifo_add1 (duplicate_pmr->nonces, nonce); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2885 | hash_set (lcm->pending_map_requests_by_nonce, nonce, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2886 | duplicate_pmr - lcm->pending_map_requests_pool); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2887 | } |
| 2888 | else |
| 2889 | { |
| 2890 | /* add map-request to pending requests table */ |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2891 | pool_get (lcm->pending_map_requests_pool, pmr); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2892 | memset (pmr, 0, sizeof (*pmr)); |
| 2893 | gid_address_copy (&pmr->src, seid); |
| 2894 | gid_address_copy (&pmr->dst, deid); |
Florin Coras | f4691cd | 2016-08-15 19:16:32 +0200 | [diff] [blame] | 2895 | clib_fifo_add1 (pmr->nonces, nonce); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2896 | pmr->is_smr_invoked = is_smr_invoked; |
| 2897 | reset_pending_mr_counters (pmr); |
| 2898 | hash_set (lcm->pending_map_requests_by_nonce, nonce, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2899 | pmr - lcm->pending_map_requests_pool); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2900 | } |
| 2901 | |
| 2902 | return 0; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | static void |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2906 | get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2907 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2908 | ip4_header_t *ip4 = hdr; |
| 2909 | ip6_header_t *ip6; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2910 | |
| 2911 | if ((ip4->ip_version_and_header_length & 0xF0) == 0x40) |
| 2912 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2913 | ip_address_set (src, &ip4->src_address, IP4); |
| 2914 | ip_address_set (dst, &ip4->dst_address, IP4); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2915 | } |
| 2916 | else |
| 2917 | { |
| 2918 | ip6 = hdr; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2919 | ip_address_set (src, &ip6->src_address, IP6); |
| 2920 | ip_address_set (dst, &ip6->dst_address, IP6); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 2921 | } |
| 2922 | } |
| 2923 | |
Filip Tehlar | 324112f | 2016-06-02 16:07:38 +0200 | [diff] [blame] | 2924 | static u32 |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2925 | lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2926 | u8 version) |
Filip Tehlar | 324112f | 2016-06-02 16:07:38 +0200 | [diff] [blame] | 2927 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2928 | uword *vnip; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 2929 | u32 vni = ~0, table_id = ~0; |
Filip Tehlar | 324112f | 2016-06-02 16:07:38 +0200 | [diff] [blame] | 2930 | |
Florin Coras | 87e4069 | 2016-09-22 18:02:17 +0200 | [diff] [blame] | 2931 | table_id = fib_table_get_table_id_for_sw_if_index ((version == |
| 2932 | IP4 ? FIB_PROTOCOL_IP4 : |
| 2933 | FIB_PROTOCOL_IP6), |
| 2934 | vnet_buffer |
| 2935 | (b)->sw_if_index |
| 2936 | [VLIB_RX]); |
Filip Tehlar | 324112f | 2016-06-02 16:07:38 +0200 | [diff] [blame] | 2937 | |
| 2938 | vnip = hash_get (lcm->vni_by_table_id, table_id); |
| 2939 | if (vnip) |
| 2940 | vni = vnip[0]; |
| 2941 | else |
| 2942 | clib_warning ("vrf %d is not mapped to any vni!", table_id); |
| 2943 | |
| 2944 | return vni; |
| 2945 | } |
| 2946 | |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2947 | always_inline u32 |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 2948 | lisp_get_bd_from_buffer_eth (vlib_buffer_t * b) |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2949 | { |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2950 | u32 sw_if_index0; |
| 2951 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2952 | l2input_main_t *l2im = &l2input_main; |
| 2953 | l2_input_config_t *config; |
| 2954 | l2_bridge_domain_t *bd_config; |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2955 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2956 | sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX]; |
| 2957 | config = vec_elt_at_index (l2im->configs, sw_if_index0); |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2958 | bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index); |
| 2959 | |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 2960 | return bd_config->bd_id; |
| 2961 | } |
| 2962 | |
| 2963 | always_inline u32 |
| 2964 | lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b) |
| 2965 | { |
| 2966 | uword *vnip; |
| 2967 | u32 vni = ~0; |
| 2968 | u32 bd = lisp_get_bd_from_buffer_eth (b); |
| 2969 | |
| 2970 | vnip = hash_get (lcm->vni_by_bd_id, bd); |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2971 | if (vnip) |
| 2972 | vni = vnip[0]; |
| 2973 | else |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 2974 | clib_warning ("bridge domain %d is not mapped to any vni!", bd); |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2975 | |
| 2976 | return vni; |
| 2977 | } |
| 2978 | |
Filip Tehlar | 4868ff6 | 2017-03-09 16:48:39 +0100 | [diff] [blame] | 2979 | void |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2980 | get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b, |
Filip Tehlar | 4868ff6 | 2017-03-09 16:48:39 +0100 | [diff] [blame] | 2981 | gid_address_t * src, gid_address_t * dst, |
| 2982 | u16 type) |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2983 | { |
Filip Tehlar | ef2a5bf | 2017-05-30 07:14:46 +0200 | [diff] [blame] | 2984 | ethernet_header_t *eh; |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2985 | u32 vni = 0; |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2986 | |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 2987 | memset (src, 0, sizeof (*src)); |
| 2988 | memset (dst, 0, sizeof (*dst)); |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2989 | |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 2990 | gid_address_type (dst) = GID_ADDR_NO_ADDRESS; |
| 2991 | gid_address_type (src) = GID_ADDR_NO_ADDRESS; |
| 2992 | |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2993 | if (LISP_AFI_IP == type || LISP_AFI_IP6 == type) |
| 2994 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2995 | ip4_header_t *ip; |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 2996 | u8 version, preflen; |
| 2997 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 2998 | gid_address_type (src) = GID_ADDR_IP_PREFIX; |
| 2999 | gid_address_type (dst) = GID_ADDR_IP_PREFIX; |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 3000 | |
| 3001 | ip = vlib_buffer_get_current (b); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3002 | get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst)); |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 3003 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3004 | version = gid_address_ip_version (src); |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 3005 | preflen = ip_address_max_len (version); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3006 | gid_address_ippref_len (src) = preflen; |
| 3007 | gid_address_ippref_len (dst) = preflen; |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 3008 | |
| 3009 | vni = lisp_get_vni_from_buffer_ip (lcm, b, version); |
| 3010 | gid_address_vni (dst) = vni; |
| 3011 | gid_address_vni (src) = vni; |
| 3012 | } |
| 3013 | else if (LISP_AFI_MAC == type) |
| 3014 | { |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3015 | ethernet_arp_header_t *ah; |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 3016 | |
| 3017 | eh = vlib_buffer_get_current (b); |
| 3018 | |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3019 | if (clib_net_to_host_u16 (eh->type) == ETHERNET_TYPE_ARP) |
| 3020 | { |
| 3021 | ah = (ethernet_arp_header_t *) (((u8 *) eh) + sizeof (*eh)); |
| 3022 | if (clib_net_to_host_u16 (ah->opcode) |
| 3023 | != ETHERNET_ARP_OPCODE_request) |
| 3024 | return; |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 3025 | |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3026 | gid_address_type (dst) = GID_ADDR_ARP; |
| 3027 | gid_address_arp_bd (dst) = lisp_get_bd_from_buffer_eth (b); |
| 3028 | clib_memcpy (&gid_address_arp_ip4 (dst), |
| 3029 | &ah->ip4_over_ethernet[1].ip4, 4); |
| 3030 | } |
| 3031 | else |
| 3032 | { |
| 3033 | gid_address_type (src) = GID_ADDR_MAC; |
| 3034 | gid_address_type (dst) = GID_ADDR_MAC; |
| 3035 | mac_copy (&gid_address_mac (src), eh->src_address); |
| 3036 | mac_copy (&gid_address_mac (dst), eh->dst_address); |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 3037 | |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3038 | /* get vni */ |
| 3039 | vni = lisp_get_vni_from_buffer_eth (lcm, b); |
| 3040 | |
| 3041 | gid_address_vni (dst) = vni; |
| 3042 | gid_address_vni (src) = vni; |
| 3043 | } |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 3044 | } |
Florin Coras | ce1b4c7 | 2017-01-26 14:25:34 -0800 | [diff] [blame] | 3045 | else if (LISP_AFI_LCAF == type) |
| 3046 | { |
Filip Tehlar | ef2a5bf | 2017-05-30 07:14:46 +0200 | [diff] [blame] | 3047 | lisp_nsh_hdr_t *nh; |
| 3048 | eh = vlib_buffer_get_current (b); |
| 3049 | |
| 3050 | if (clib_net_to_host_u16 (eh->type) == ETHERNET_TYPE_NSH) |
| 3051 | { |
| 3052 | nh = (lisp_nsh_hdr_t *) (((u8 *) eh) + sizeof (*eh)); |
| 3053 | u32 spi = clib_net_to_host_u32 (nh->spi_si << 8); |
| 3054 | u8 si = (u8) clib_net_to_host_u32 (nh->spi_si); |
| 3055 | gid_address_nsh_spi (dst) = spi; |
| 3056 | gid_address_nsh_si (dst) = si; |
| 3057 | |
| 3058 | gid_address_type (dst) = GID_ADDR_NSH; |
| 3059 | } |
Florin Coras | ce1b4c7 | 2017-01-26 14:25:34 -0800 | [diff] [blame] | 3060 | } |
Florin Coras | 1a1adc7 | 2016-07-22 01:45:30 +0200 | [diff] [blame] | 3061 | } |
| 3062 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3063 | static uword |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 3064 | lisp_cp_lookup_inline (vlib_main_t * vm, |
| 3065 | vlib_node_runtime_t * node, |
| 3066 | vlib_frame_t * from_frame, int overlay) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3067 | { |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3068 | u32 *from, *to_next, di, si; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3069 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3070 | u32 pkts_mapped = 0, next_index; |
| 3071 | uword n_left_from, n_left_to_next; |
| 3072 | vnet_main_t *vnm = vnet_get_main (); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3073 | |
| 3074 | from = vlib_frame_vector_args (from_frame); |
| 3075 | n_left_from = from_frame->n_vectors; |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3076 | next_index = node->cached_next_index; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3077 | |
| 3078 | while (n_left_from > 0) |
| 3079 | { |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3080 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3081 | |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3082 | while (n_left_from > 0 && n_left_to_next > 0) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3083 | { |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3084 | u32 pi0, sw_if_index0, next0; |
| 3085 | u64 mac0; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3086 | vlib_buffer_t *b0; |
| 3087 | gid_address_t src, dst; |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3088 | ethernet_arp_header_t *arp0; |
| 3089 | ethernet_header_t *eth0; |
| 3090 | vnet_hw_interface_t *hw_if0; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3091 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3092 | pi0 = from[0]; |
| 3093 | from += 1; |
| 3094 | n_left_from -= 1; |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3095 | to_next[0] = pi0; |
| 3096 | to_next += 1; |
| 3097 | n_left_to_next -= 1; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3098 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3099 | b0 = vlib_get_buffer (vm, pi0); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3100 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3101 | /* src/dst eid pair */ |
Filip Tehlar | 4868ff6 | 2017-03-09 16:48:39 +0100 | [diff] [blame] | 3102 | get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst, overlay); |
Filip Tehlar | 324112f | 2016-06-02 16:07:38 +0200 | [diff] [blame] | 3103 | |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3104 | if (gid_address_type (&dst) == GID_ADDR_ARP) |
| 3105 | { |
| 3106 | mac0 = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst); |
| 3107 | if (GID_LOOKUP_MISS_L2 != mac0) |
| 3108 | { |
| 3109 | /* send ARP reply */ |
| 3110 | |
| 3111 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 3112 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0; |
| 3113 | |
| 3114 | hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0); |
| 3115 | |
| 3116 | eth0 = vlib_buffer_get_current (b0); |
| 3117 | arp0 = (ethernet_arp_header_t *) (((u8 *) eth0) |
| 3118 | + sizeof (*eth0)); |
| 3119 | arp0->opcode = |
| 3120 | clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply); |
| 3121 | arp0->ip4_over_ethernet[1] = arp0->ip4_over_ethernet[0]; |
| 3122 | clib_memcpy (arp0->ip4_over_ethernet[0].ethernet, |
| 3123 | (u8 *) & mac0, 6); |
| 3124 | clib_memcpy (&arp0->ip4_over_ethernet[0].ip4, |
| 3125 | &gid_address_arp_ip4 (&dst), 4); |
| 3126 | |
| 3127 | /* Hardware must be ethernet-like. */ |
| 3128 | ASSERT (vec_len (hw_if0->hw_address) == 6); |
| 3129 | |
| 3130 | clib_memcpy (eth0->dst_address, eth0->src_address, 6); |
| 3131 | clib_memcpy (eth0->src_address, hw_if0->hw_address, 6); |
| 3132 | |
| 3133 | b0->error = node->errors[LISP_CP_LOOKUP_ERROR_ARP_REPLY_TX]; |
| 3134 | next0 = LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX; |
| 3135 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 3136 | to_next, |
| 3137 | n_left_to_next, pi0, |
| 3138 | next0); |
Florin Coras | bae851f | 2017-08-09 17:50:09 -0700 | [diff] [blame] | 3139 | continue; |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3140 | } |
Florin Coras | bae851f | 2017-08-09 17:50:09 -0700 | [diff] [blame] | 3141 | goto done; |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3142 | } |
| 3143 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3144 | /* if we have remote mapping for destination already in map-chache |
| 3145 | add forwarding tunnel directly. If not send a map-request */ |
Florin Coras | dca8804 | 2016-09-14 16:01:38 +0200 | [diff] [blame] | 3146 | di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst, |
| 3147 | &src); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3148 | if (~0 != di) |
| 3149 | { |
| 3150 | mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di); |
| 3151 | /* send a map-request also in case of negative mapping entry |
| 3152 | with corresponding action */ |
| 3153 | if (m->action == LISP_SEND_MAP_REQUEST) |
| 3154 | { |
| 3155 | /* send map-request */ |
| 3156 | queue_map_request (&src, &dst, 0 /* smr_invoked */ , |
| 3157 | 0 /* is_resend */ ); |
| 3158 | pkts_mapped++; |
| 3159 | } |
| 3160 | else |
| 3161 | { |
Filip Tehlar | ef2a5bf | 2017-05-30 07:14:46 +0200 | [diff] [blame] | 3162 | if (GID_ADDR_NSH != gid_address_type (&dst)) |
| 3163 | { |
| 3164 | si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, |
| 3165 | &src); |
| 3166 | } |
| 3167 | else |
| 3168 | si = lcm->nsh_map_index; |
| 3169 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3170 | if (~0 != si) |
| 3171 | { |
Filip Tehlar | ce1aae4 | 2017-01-04 10:42:25 +0100 | [diff] [blame] | 3172 | dp_add_fwd_entry_from_mt (si, di); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3173 | } |
| 3174 | } |
| 3175 | } |
| 3176 | else |
| 3177 | { |
| 3178 | /* send map-request */ |
| 3179 | queue_map_request (&src, &dst, 0 /* smr_invoked */ , |
| 3180 | 0 /* is_resend */ ); |
| 3181 | pkts_mapped++; |
| 3182 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3183 | |
Florin Coras | bae851f | 2017-08-09 17:50:09 -0700 | [diff] [blame] | 3184 | done: |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3185 | b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP]; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3186 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 3187 | { |
| 3188 | lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0, |
| 3189 | sizeof (*tr)); |
Andrej Kozemcak | 94e3476 | 2016-05-25 12:43:21 +0200 | [diff] [blame] | 3190 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3191 | memset (tr, 0, sizeof (*tr)); |
| 3192 | gid_address_copy (&tr->dst_eid, &dst); |
Florin Coras | 5a1c11b | 2016-09-06 16:29:34 +0200 | [diff] [blame] | 3193 | ip_address_copy (&tr->map_resolver_ip, |
| 3194 | &lcm->active_map_resolver); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3195 | } |
| 3196 | gid_address_free (&dst); |
| 3197 | gid_address_free (&src); |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3198 | next0 = LISP_CP_LOOKUP_NEXT_DROP; |
| 3199 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 3200 | to_next, |
| 3201 | n_left_to_next, pi0, next0); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3202 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3203 | |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3204 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3205 | } |
| 3206 | vlib_node_increment_counter (vm, node->node_index, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3207 | LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT, |
| 3208 | pkts_mapped); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3209 | return from_frame->n_vectors; |
| 3210 | } |
| 3211 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 3212 | static uword |
| 3213 | lisp_cp_lookup_ip4 (vlib_main_t * vm, |
| 3214 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 3215 | { |
| 3216 | return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP)); |
| 3217 | } |
| 3218 | |
| 3219 | static uword |
| 3220 | lisp_cp_lookup_ip6 (vlib_main_t * vm, |
| 3221 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 3222 | { |
| 3223 | return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6)); |
| 3224 | } |
| 3225 | |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 3226 | static uword |
| 3227 | lisp_cp_lookup_l2 (vlib_main_t * vm, |
| 3228 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 3229 | { |
| 3230 | return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC)); |
| 3231 | } |
| 3232 | |
Florin Coras | ce1b4c7 | 2017-01-26 14:25:34 -0800 | [diff] [blame] | 3233 | static uword |
| 3234 | lisp_cp_lookup_nsh (vlib_main_t * vm, |
| 3235 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 3236 | { |
| 3237 | /* TODO decide if NSH should be propagated as LCAF or not */ |
| 3238 | return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_LCAF)); |
| 3239 | } |
| 3240 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3241 | /* *INDENT-OFF* */ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 3242 | VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = { |
| 3243 | .function = lisp_cp_lookup_ip4, |
| 3244 | .name = "lisp-cp-lookup-ip4", |
| 3245 | .vector_size = sizeof (u32), |
| 3246 | .format_trace = format_lisp_cp_lookup_trace, |
| 3247 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 3248 | |
| 3249 | .n_errors = LISP_CP_LOOKUP_N_ERROR, |
| 3250 | .error_strings = lisp_cp_lookup_error_strings, |
| 3251 | |
| 3252 | .n_next_nodes = LISP_CP_LOOKUP_N_NEXT, |
| 3253 | |
| 3254 | .next_nodes = { |
| 3255 | [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop", |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3256 | [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output", |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 3257 | }, |
| 3258 | }; |
| 3259 | /* *INDENT-ON* */ |
| 3260 | |
| 3261 | /* *INDENT-OFF* */ |
| 3262 | VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = { |
| 3263 | .function = lisp_cp_lookup_ip6, |
| 3264 | .name = "lisp-cp-lookup-ip6", |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3265 | .vector_size = sizeof (u32), |
| 3266 | .format_trace = format_lisp_cp_lookup_trace, |
| 3267 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 3268 | |
| 3269 | .n_errors = LISP_CP_LOOKUP_N_ERROR, |
| 3270 | .error_strings = lisp_cp_lookup_error_strings, |
| 3271 | |
| 3272 | .n_next_nodes = LISP_CP_LOOKUP_N_NEXT, |
| 3273 | |
| 3274 | .next_nodes = { |
| 3275 | [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop", |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3276 | [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output", |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 3277 | }, |
| 3278 | }; |
| 3279 | /* *INDENT-ON* */ |
| 3280 | |
| 3281 | /* *INDENT-OFF* */ |
| 3282 | VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = { |
| 3283 | .function = lisp_cp_lookup_l2, |
| 3284 | .name = "lisp-cp-lookup-l2", |
| 3285 | .vector_size = sizeof (u32), |
| 3286 | .format_trace = format_lisp_cp_lookup_trace, |
| 3287 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 3288 | |
| 3289 | .n_errors = LISP_CP_LOOKUP_N_ERROR, |
| 3290 | .error_strings = lisp_cp_lookup_error_strings, |
| 3291 | |
| 3292 | .n_next_nodes = LISP_CP_LOOKUP_N_NEXT, |
| 3293 | |
| 3294 | .next_nodes = { |
| 3295 | [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop", |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3296 | [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output", |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3297 | }, |
| 3298 | }; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3299 | /* *INDENT-ON* */ |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3300 | |
Florin Coras | ce1b4c7 | 2017-01-26 14:25:34 -0800 | [diff] [blame] | 3301 | /* *INDENT-OFF* */ |
| 3302 | VLIB_REGISTER_NODE (lisp_cp_lookup_nsh_node) = { |
| 3303 | .function = lisp_cp_lookup_nsh, |
| 3304 | .name = "lisp-cp-lookup-nsh", |
| 3305 | .vector_size = sizeof (u32), |
| 3306 | .format_trace = format_lisp_cp_lookup_trace, |
| 3307 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 3308 | |
| 3309 | .n_errors = LISP_CP_LOOKUP_N_ERROR, |
| 3310 | .error_strings = lisp_cp_lookup_error_strings, |
| 3311 | |
| 3312 | .n_next_nodes = LISP_CP_LOOKUP_N_NEXT, |
| 3313 | |
| 3314 | .next_nodes = { |
| 3315 | [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop", |
Filip Tehlar | d5a65db | 2017-05-17 17:21:10 +0200 | [diff] [blame] | 3316 | [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output", |
Florin Coras | ce1b4c7 | 2017-01-26 14:25:34 -0800 | [diff] [blame] | 3317 | }, |
| 3318 | }; |
| 3319 | /* *INDENT-ON* */ |
| 3320 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3321 | /* lisp_cp_input statistics */ |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 3322 | #define foreach_lisp_cp_input_error \ |
| 3323 | _(DROP, "drop") \ |
| 3324 | _(RLOC_PROBE_REQ_RECEIVED, "rloc-probe requests received") \ |
| 3325 | _(RLOC_PROBE_REP_RECEIVED, "rloc-probe replies received") \ |
| 3326 | _(MAP_NOTIFIES_RECEIVED, "map-notifies received") \ |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3327 | _(MAP_REPLIES_RECEIVED, "map-replies received") |
| 3328 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3329 | static char *lisp_cp_input_error_strings[] = { |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3330 | #define _(sym,string) string, |
| 3331 | foreach_lisp_cp_input_error |
| 3332 | #undef _ |
| 3333 | }; |
| 3334 | |
| 3335 | typedef enum |
| 3336 | { |
| 3337 | #define _(sym,str) LISP_CP_INPUT_ERROR_##sym, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3338 | foreach_lisp_cp_input_error |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3339 | #undef _ |
| 3340 | LISP_CP_INPUT_N_ERROR, |
| 3341 | } lisp_cp_input_error_t; |
| 3342 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3343 | typedef struct |
| 3344 | { |
| 3345 | gid_address_t dst_eid; |
| 3346 | ip4_address_t map_resolver_ip; |
| 3347 | } lisp_cp_input_trace_t; |
| 3348 | |
| 3349 | u8 * |
| 3350 | format_lisp_cp_input_trace (u8 * s, va_list * args) |
| 3351 | { |
| 3352 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 3353 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3354 | CLIB_UNUSED (lisp_cp_input_trace_t * t) = |
| 3355 | va_arg (*args, lisp_cp_input_trace_t *); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3356 | |
| 3357 | s = format (s, "LISP-CP-INPUT: TODO"); |
| 3358 | return s; |
| 3359 | } |
| 3360 | |
Filip Tehlar | 9677a94 | 2016-11-28 10:23:31 +0100 | [diff] [blame] | 3361 | static void |
| 3362 | remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi) |
| 3363 | { |
| 3364 | mapping_t *m; |
Florin Coras | f3dc11a | 2017-01-24 03:13:43 -0800 | [diff] [blame] | 3365 | vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args; |
| 3366 | memset (adj_args, 0, sizeof (adj_args[0])); |
Filip Tehlar | 9677a94 | 2016-11-28 10:23:31 +0100 | [diff] [blame] | 3367 | |
| 3368 | m = pool_elt_at_index (lcm->mapping_pool, mi); |
Florin Coras | f3dc11a | 2017-01-24 03:13:43 -0800 | [diff] [blame] | 3369 | |
| 3370 | gid_address_copy (&adj_args->reid, &m->eid); |
| 3371 | adj_args->is_add = 0; |
| 3372 | if (vnet_lisp_add_del_adjacency (adj_args)) |
| 3373 | clib_warning ("failed to del adjacency!"); |
| 3374 | |
Filip Tehlar | 9677a94 | 2016-11-28 10:23:31 +0100 | [diff] [blame] | 3375 | vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ , |
| 3376 | 0 /* is_static */ , 0); |
| 3377 | mapping_delete_timer (lcm, mi); |
| 3378 | } |
| 3379 | |
| 3380 | static void |
| 3381 | mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi, |
| 3382 | f64 expiration_time) |
| 3383 | { |
| 3384 | mapping_t *m; |
| 3385 | u64 now = clib_cpu_time_now (); |
| 3386 | u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second; |
| 3387 | u64 exp_clock_time = now + expiration_time * cpu_cps; |
| 3388 | |
| 3389 | m = pool_elt_at_index (lcm->mapping_pool, mi); |
| 3390 | |
| 3391 | m->timer_set = 1; |
| 3392 | timing_wheel_insert (&lcm->wheel, exp_clock_time, mi); |
| 3393 | } |
| 3394 | |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3395 | static void |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3396 | map_records_arg_free (map_records_arg_t * a) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3397 | { |
Florin Coras | acd4c63 | 2017-06-15 14:33:48 -0700 | [diff] [blame] | 3398 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3399 | mapping_t *m; |
| 3400 | vec_foreach (m, a->mappings) |
| 3401 | { |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3402 | vec_free (m->locators); |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3403 | gid_address_free (&m->eid); |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3404 | } |
Florin Coras | acd4c63 | 2017-06-15 14:33:48 -0700 | [diff] [blame] | 3405 | pool_put (lcm->map_records_args_pool[vlib_get_thread_index ()], a); |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | void * |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3409 | process_map_reply (map_records_arg_t * a) |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3410 | { |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3411 | mapping_t *m; |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3412 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 3413 | u32 dst_map_index = 0; |
| 3414 | pending_map_request_t *pmr; |
| 3415 | u64 *noncep; |
| 3416 | uword *pmr_index; |
| 3417 | |
| 3418 | if (a->is_rloc_probe) |
| 3419 | goto done; |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 3420 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3421 | /* Check pending requests table and nonce */ |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3422 | pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3423 | if (!pmr_index) |
| 3424 | { |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3425 | clib_warning ("No pending map-request entry with nonce %lu!", a->nonce); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 3426 | goto done; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3427 | } |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3428 | pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3429 | |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3430 | vec_foreach (m, a->mappings) |
| 3431 | { |
| 3432 | /* insert/update mappings cache */ |
| 3433 | vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action, |
| 3434 | m->authoritative, m->ttl, |
| 3435 | 1, 0 /* is_static */ , &dst_map_index); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3436 | |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 3437 | if (dst_map_index == (u32) ~ 0) |
| 3438 | continue; |
| 3439 | |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3440 | /* try to program forwarding only if mapping saved or updated */ |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 3441 | vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args; |
| 3442 | memset (adj_args, 0, sizeof (adj_args[0])); |
Florin Coras | f3dc11a | 2017-01-24 03:13:43 -0800 | [diff] [blame] | 3443 | |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 3444 | gid_address_copy (&adj_args->leid, &pmr->src); |
| 3445 | gid_address_copy (&adj_args->reid, &m->eid); |
| 3446 | adj_args->is_add = 1; |
| 3447 | if (vnet_lisp_add_del_adjacency (adj_args)) |
| 3448 | clib_warning ("failed to add adjacency!"); |
Florin Coras | f3dc11a | 2017-01-24 03:13:43 -0800 | [diff] [blame] | 3449 | |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 3450 | if ((u32) ~ 0 != m->ttl) |
| 3451 | mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60); |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3452 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3453 | |
| 3454 | /* remove pending map request entry */ |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3455 | |
| 3456 | /* *INDENT-OFF* */ |
Florin Coras | f4691cd | 2016-08-15 19:16:32 +0200 | [diff] [blame] | 3457 | clib_fifo_foreach (noncep, pmr->nonces, ({ |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 3458 | hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]); |
Florin Coras | f4691cd | 2016-08-15 19:16:32 +0200 | [diff] [blame] | 3459 | })); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3460 | /* *INDENT-ON* */ |
| 3461 | |
| 3462 | clib_fifo_free (pmr->nonces); |
| 3463 | pool_put (lcm->pending_map_requests_pool, pmr); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 3464 | |
| 3465 | done: |
Florin Coras | acd4c63 | 2017-06-15 14:33:48 -0700 | [diff] [blame] | 3466 | a->is_free = 1; |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 3467 | return 0; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3468 | } |
| 3469 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3470 | static int |
| 3471 | is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len, |
| 3472 | lisp_key_type_t key_id, u8 * key) |
| 3473 | { |
| 3474 | u8 *auth_data = 0; |
| 3475 | u16 auth_data_len; |
| 3476 | int result; |
| 3477 | |
| 3478 | auth_data_len = auth_data_len_by_key_id (key_id); |
| 3479 | if ((u16) ~ 0 == auth_data_len) |
| 3480 | { |
| 3481 | clib_warning ("invalid length for key_id %d!", key_id); |
| 3482 | return 0; |
| 3483 | } |
| 3484 | |
| 3485 | /* save auth data */ |
| 3486 | vec_validate (auth_data, auth_data_len - 1); |
| 3487 | clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len); |
| 3488 | |
| 3489 | /* clear auth data */ |
| 3490 | memset (MNOTIFY_DATA (h), 0, auth_data_len); |
| 3491 | |
| 3492 | /* get hash of the message */ |
| 3493 | unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key), |
| 3494 | (unsigned char *) h, msg_len, NULL, NULL); |
| 3495 | |
| 3496 | result = memcmp (code, auth_data, auth_data_len); |
| 3497 | |
| 3498 | vec_free (auth_data); |
| 3499 | |
| 3500 | return !result; |
| 3501 | } |
| 3502 | |
| 3503 | static void |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3504 | process_map_notify (map_records_arg_t * a) |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3505 | { |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3506 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3507 | uword *pmr_index; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3508 | |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3509 | pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce); |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3510 | if (!pmr_index) |
| 3511 | { |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3512 | clib_warning ("No pending map-register entry with nonce %lu!", |
| 3513 | a->nonce); |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3514 | return; |
| 3515 | } |
| 3516 | |
Florin Coras | acd4c63 | 2017-06-15 14:33:48 -0700 | [diff] [blame] | 3517 | a->is_free = 1; |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 3518 | pool_put_index (lcm->pending_map_registers_pool, pmr_index[0]); |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3519 | hash_unset (lcm->map_register_messages_by_nonce, a->nonce); |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 3520 | |
| 3521 | /* reset map-notify counter */ |
| 3522 | lcm->expired_map_registers = 0; |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3523 | } |
| 3524 | |
| 3525 | static mapping_t * |
| 3526 | get_mapping (lisp_cp_main_t * lcm, gid_address_t * e) |
| 3527 | { |
| 3528 | u32 mi; |
| 3529 | |
| 3530 | mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e); |
| 3531 | if (~0 == mi) |
| 3532 | { |
| 3533 | clib_warning ("eid %U not found in map-cache!", unformat_gid_address, |
| 3534 | e); |
| 3535 | return 0; |
| 3536 | } |
| 3537 | return pool_elt_at_index (lcm->mapping_pool, mi); |
| 3538 | } |
| 3539 | |
| 3540 | /** |
| 3541 | * When map-notify is received it is necessary that all EIDs in the record |
| 3542 | * list share common key. The key is then used to verify authentication |
| 3543 | * data in map-notify message. |
| 3544 | */ |
| 3545 | static int |
| 3546 | map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps, |
| 3547 | u32 key_id, u8 ** key_out) |
| 3548 | { |
| 3549 | u32 i, len = vec_len (maps); |
| 3550 | mapping_t *m; |
| 3551 | |
| 3552 | /* get key of the first mapping */ |
| 3553 | m = get_mapping (lcm, &maps[0].eid); |
| 3554 | if (!m || !m->key) |
| 3555 | return -1; |
| 3556 | |
| 3557 | key_out[0] = m->key; |
| 3558 | |
| 3559 | for (i = 1; i < len; i++) |
| 3560 | { |
| 3561 | m = get_mapping (lcm, &maps[i].eid); |
| 3562 | if (!m || !m->key) |
| 3563 | return -1; |
| 3564 | |
| 3565 | if (key_id != m->key_id || vec_cmp (m->key, key_out[0])) |
| 3566 | { |
| 3567 | clib_warning ("keys does not match! %v, %v", key_out[0], m->key); |
| 3568 | return -1; |
| 3569 | } |
| 3570 | } |
| 3571 | return 0; |
| 3572 | } |
| 3573 | |
| 3574 | static int |
| 3575 | parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count) |
| 3576 | { |
| 3577 | locator_t *locators = 0; |
| 3578 | u32 i, len; |
| 3579 | gid_address_t deid; |
| 3580 | mapping_t m; |
| 3581 | locator_t *loc; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3582 | |
Filip Tehlar | 68c74fc | 2017-05-04 14:52:42 +0200 | [diff] [blame] | 3583 | memset (&m, 0, sizeof (m)); |
| 3584 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3585 | /* parse record eid */ |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3586 | for (i = 0; i < count; i++) |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3587 | { |
Florin Coras | e68de8c | 2017-06-05 15:38:50 -0700 | [diff] [blame] | 3588 | locators = 0; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3589 | len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL); |
| 3590 | if (len == ~0) |
| 3591 | { |
| 3592 | clib_warning ("Failed to parse mapping record!"); |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3593 | vec_foreach (loc, locators) locator_free (loc); |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3594 | vec_free (locators); |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3595 | return -1; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3596 | } |
| 3597 | |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3598 | m.locators = locators; |
| 3599 | gid_address_copy (&m.eid, &deid); |
| 3600 | vec_add1 (a->mappings, m); |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3601 | } |
| 3602 | |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3603 | return 0; |
| 3604 | } |
| 3605 | |
| 3606 | static map_records_arg_t * |
Florin Coras | acd4c63 | 2017-06-15 14:33:48 -0700 | [diff] [blame] | 3607 | map_record_args_get () |
| 3608 | { |
| 3609 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 3610 | map_records_arg_t *rec; |
| 3611 | |
| 3612 | /* Cleanup first */ |
| 3613 | /* *INDENT-OFF* */ |
| 3614 | pool_foreach (rec, lcm->map_records_args_pool[vlib_get_thread_index()], ({ |
| 3615 | if (rec->is_free) |
| 3616 | map_records_arg_free (rec); |
| 3617 | })); |
| 3618 | /* *INDENT-ON* */ |
| 3619 | |
| 3620 | pool_get (lcm->map_records_args_pool[vlib_get_thread_index ()], rec); |
| 3621 | return rec; |
| 3622 | } |
| 3623 | |
| 3624 | static map_records_arg_t * |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3625 | parse_map_notify (vlib_buffer_t * b) |
| 3626 | { |
| 3627 | int rc = 0; |
| 3628 | map_notify_hdr_t *mnotif_hdr; |
| 3629 | lisp_key_type_t key_id; |
| 3630 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 3631 | u8 *key = 0; |
| 3632 | gid_address_t deid; |
| 3633 | u16 auth_data_len = 0; |
| 3634 | u8 record_count; |
Florin Coras | acd4c63 | 2017-06-15 14:33:48 -0700 | [diff] [blame] | 3635 | map_records_arg_t *a; |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3636 | |
Florin Coras | acd4c63 | 2017-06-15 14:33:48 -0700 | [diff] [blame] | 3637 | a = map_record_args_get (); |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3638 | memset (a, 0, sizeof (*a)); |
| 3639 | mnotif_hdr = vlib_buffer_get_current (b); |
| 3640 | vlib_buffer_pull (b, sizeof (*mnotif_hdr)); |
| 3641 | memset (&deid, 0, sizeof (deid)); |
| 3642 | |
| 3643 | a->nonce = MNOTIFY_NONCE (mnotif_hdr); |
| 3644 | key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr)); |
| 3645 | auth_data_len = auth_data_len_by_key_id (key_id); |
| 3646 | |
| 3647 | /* advance buffer by authentication data */ |
| 3648 | vlib_buffer_pull (b, auth_data_len); |
| 3649 | |
| 3650 | record_count = MNOTIFY_REC_COUNT (mnotif_hdr); |
| 3651 | rc = parse_map_records (b, a, record_count); |
| 3652 | if (rc != 0) |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3653 | { |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3654 | map_records_arg_free (a); |
| 3655 | return 0; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3656 | } |
| 3657 | |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3658 | rc = map_record_integrity_check (lcm, a->mappings, key_id, &key); |
| 3659 | if (rc != 0) |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3660 | { |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3661 | map_records_arg_free (a); |
| 3662 | return 0; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3663 | } |
| 3664 | |
| 3665 | /* verify authentication data */ |
| 3666 | if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b) |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3667 | - (u8 *) mnotif_hdr, key_id, key)) |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3668 | { |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 3669 | clib_warning ("Map-notify auth data verification failed for nonce " |
| 3670 | "0x%lx!", a->nonce); |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3671 | map_records_arg_free (a); |
| 3672 | return 0; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3673 | } |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3674 | return a; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | static vlib_buffer_t * |
| 3678 | build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc, |
| 3679 | ip_address_t * dst, u64 nonce, u8 probe_bit, |
| 3680 | mapping_t * records, u16 dst_port, u32 * bi_res) |
| 3681 | { |
| 3682 | vlib_buffer_t *b; |
| 3683 | u32 bi; |
| 3684 | vlib_main_t *vm = lcm->vlib_main; |
| 3685 | |
| 3686 | if (vlib_buffer_alloc (vm, &bi, 1) != 1) |
| 3687 | { |
| 3688 | clib_warning ("Can't allocate buffer for Map-Register!"); |
| 3689 | return 0; |
| 3690 | } |
| 3691 | |
| 3692 | b = vlib_get_buffer (vm, bi); |
| 3693 | |
| 3694 | /* leave some space for the encap headers */ |
| 3695 | vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN); |
| 3696 | |
| 3697 | lisp_msg_put_map_reply (b, records, nonce, probe_bit); |
| 3698 | |
| 3699 | /* push outer ip header */ |
Florin Coras | fdbc382 | 2017-07-27 00:34:12 -0700 | [diff] [blame] | 3700 | pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst, 1); |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3701 | |
| 3702 | bi_res[0] = bi; |
| 3703 | return b; |
| 3704 | } |
| 3705 | |
| 3706 | static int |
| 3707 | send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst, |
| 3708 | u8 probe_bit, u64 nonce, u16 dst_port, |
| 3709 | ip_address_t * probed_loc) |
| 3710 | { |
| 3711 | ip_address_t src; |
| 3712 | u32 bi; |
| 3713 | vlib_buffer_t *b; |
| 3714 | vlib_frame_t *f; |
| 3715 | u32 next_index, *to_next; |
| 3716 | mapping_t *records = 0, *m; |
| 3717 | |
| 3718 | m = pool_elt_at_index (lcm->mapping_pool, mi); |
| 3719 | if (!m) |
| 3720 | return -1; |
| 3721 | |
| 3722 | vec_add1 (records, m[0]); |
| 3723 | add_locators (lcm, &records[0], m->locator_set_index, probed_loc); |
| 3724 | memset (&src, 0, sizeof (src)); |
| 3725 | |
| 3726 | if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src)) |
| 3727 | { |
| 3728 | clib_warning ("can't find inteface address for %U", format_ip_address, |
| 3729 | dst); |
| 3730 | return -1; |
| 3731 | } |
| 3732 | |
| 3733 | b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port, |
| 3734 | &bi); |
| 3735 | if (!b) |
| 3736 | return -1; |
| 3737 | free_map_register_records (records); |
| 3738 | |
| 3739 | vnet_buffer (b)->sw_if_index[VLIB_TX] = 0; |
| 3740 | next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ? |
| 3741 | ip4_lookup_node.index : ip6_lookup_node.index; |
| 3742 | |
| 3743 | f = vlib_get_frame_to_node (lcm->vlib_main, next_index); |
| 3744 | |
| 3745 | /* Enqueue the packet */ |
| 3746 | to_next = vlib_frame_vector_args (f); |
| 3747 | to_next[0] = bi; |
| 3748 | f->n_vectors = 1; |
| 3749 | vlib_put_frame_to_node (lcm->vlib_main, next_index, f); |
| 3750 | return 0; |
| 3751 | } |
| 3752 | |
Filip Tehlar | b601f22 | 2017-01-02 10:22:56 +0100 | [diff] [blame] | 3753 | static void |
| 3754 | find_ip_header (vlib_buffer_t * b, u8 ** ip_hdr) |
| 3755 | { |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 3756 | const i32 start = vnet_buffer (b)->l3_hdr_offset; |
Filip Tehlar | b601f22 | 2017-01-02 10:22:56 +0100 | [diff] [blame] | 3757 | if (start < 0 && start < -sizeof (b->pre_data)) |
| 3758 | { |
| 3759 | *ip_hdr = 0; |
| 3760 | return; |
| 3761 | } |
| 3762 | |
| 3763 | *ip_hdr = b->data + start; |
| 3764 | if ((u8 *) * ip_hdr > (u8 *) vlib_buffer_get_current (b)) |
| 3765 | *ip_hdr = 0; |
| 3766 | } |
| 3767 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3768 | void |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 3769 | process_map_request (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 3770 | lisp_cp_main_t * lcm, vlib_buffer_t * b) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3771 | { |
Filip Tehlar | b601f22 | 2017-01-02 10:22:56 +0100 | [diff] [blame] | 3772 | u8 *ip_hdr = 0; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3773 | ip_address_t *dst_loc = 0, probed_loc, src_loc; |
| 3774 | mapping_t m; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3775 | map_request_hdr_t *mreq_hdr; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3776 | gid_address_t src, dst; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3777 | u64 nonce; |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 3778 | u32 i, len = 0, rloc_probe_recv = 0; |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3779 | gid_address_t *itr_rlocs = 0; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3780 | |
| 3781 | mreq_hdr = vlib_buffer_get_current (b); |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3782 | if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr)) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3783 | { |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3784 | clib_warning |
| 3785 | ("Only SMR Map-Requests and RLOC probe supported for now!"); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3786 | return; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3787 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3788 | |
Filip Tehlar | b601f22 | 2017-01-02 10:22:56 +0100 | [diff] [blame] | 3789 | vlib_buffer_pull (b, sizeof (*mreq_hdr)); |
| 3790 | nonce = MREQ_NONCE (mreq_hdr); |
| 3791 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3792 | /* parse src eid */ |
| 3793 | len = lisp_msg_parse_addr (b, &src); |
| 3794 | if (len == ~0) |
| 3795 | return; |
| 3796 | |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3797 | len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs, |
| 3798 | MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3799 | if (len == ~0) |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 3800 | goto done; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3801 | |
| 3802 | /* parse eid records and send SMR-invoked map-requests */ |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3803 | for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3804 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3805 | memset (&dst, 0, sizeof (dst)); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3806 | len = lisp_msg_parse_eid_rec (b, &dst); |
| 3807 | if (len == ~0) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3808 | { |
| 3809 | clib_warning ("Can't parse map-request EID-record"); |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3810 | goto done; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3811 | } |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3812 | |
| 3813 | if (MREQ_SMR (mreq_hdr)) |
| 3814 | { |
| 3815 | /* send SMR-invoked map-requests */ |
| 3816 | queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ ); |
| 3817 | } |
| 3818 | else if (MREQ_RLOC_PROBE (mreq_hdr)) |
| 3819 | { |
Filip Tehlar | b601f22 | 2017-01-02 10:22:56 +0100 | [diff] [blame] | 3820 | find_ip_header (b, &ip_hdr); |
| 3821 | if (!ip_hdr) |
| 3822 | { |
| 3823 | clib_warning ("Cannot find the IP header!"); |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 3824 | goto done; |
Filip Tehlar | b601f22 | 2017-01-02 10:22:56 +0100 | [diff] [blame] | 3825 | } |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 3826 | rloc_probe_recv++; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3827 | memset (&m, 0, sizeof (m)); |
| 3828 | u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst); |
| 3829 | |
| 3830 | // TODO: select best locator; for now use the first one |
| 3831 | dst_loc = &gid_address_ip (&itr_rlocs[0]); |
| 3832 | |
| 3833 | /* get src/dst IP addresses */ |
| 3834 | get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc); |
| 3835 | |
| 3836 | // TODO get source port from buffer |
| 3837 | u16 src_port = LISP_CONTROL_PORT; |
| 3838 | |
| 3839 | send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce, |
| 3840 | src_port, &probed_loc); |
| 3841 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3842 | } |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3843 | |
| 3844 | done: |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 3845 | vlib_node_increment_counter (vm, node->node_index, |
| 3846 | LISP_CP_INPUT_ERROR_RLOC_PROBE_REQ_RECEIVED, |
| 3847 | rloc_probe_recv); |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3848 | vec_free (itr_rlocs); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3849 | } |
| 3850 | |
Filip Tehlar | 816f437 | 2017-04-26 16:09:06 +0200 | [diff] [blame] | 3851 | map_records_arg_t * |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3852 | parse_map_reply (vlib_buffer_t * b) |
| 3853 | { |
| 3854 | locator_t probed; |
| 3855 | gid_address_t deid; |
| 3856 | void *h; |
| 3857 | u32 i, len = 0; |
| 3858 | mapping_t m; |
| 3859 | map_reply_hdr_t *mrep_hdr; |
Florin Coras | acd4c63 | 2017-06-15 14:33:48 -0700 | [diff] [blame] | 3860 | map_records_arg_t *a; |
| 3861 | |
| 3862 | a = map_record_args_get (); |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3863 | memset (a, 0, sizeof (*a)); |
Florin Coras | acd4c63 | 2017-06-15 14:33:48 -0700 | [diff] [blame] | 3864 | |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3865 | locator_t *locators; |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 3866 | |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3867 | mrep_hdr = vlib_buffer_get_current (b); |
| 3868 | a->nonce = MREP_NONCE (mrep_hdr); |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3869 | a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr); |
Filip Tehlar | 816f437 | 2017-04-26 16:09:06 +0200 | [diff] [blame] | 3870 | if (!vlib_buffer_has_space (b, sizeof (*mrep_hdr))) |
| 3871 | { |
| 3872 | clib_mem_free (a); |
| 3873 | return 0; |
| 3874 | } |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3875 | vlib_buffer_pull (b, sizeof (*mrep_hdr)); |
| 3876 | |
| 3877 | for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++) |
| 3878 | { |
| 3879 | memset (&m, 0, sizeof (m)); |
| 3880 | locators = 0; |
| 3881 | h = vlib_buffer_get_current (b); |
| 3882 | |
| 3883 | m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h)); |
| 3884 | m.action = MAP_REC_ACTION (h); |
| 3885 | m.authoritative = MAP_REC_AUTH (h); |
| 3886 | |
| 3887 | len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed); |
| 3888 | if (len == ~0) |
| 3889 | { |
| 3890 | clib_warning ("Failed to parse mapping record!"); |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3891 | map_records_arg_free (a); |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3892 | return 0; |
| 3893 | } |
| 3894 | |
| 3895 | m.locators = locators; |
| 3896 | gid_address_copy (&m.eid, &deid); |
| 3897 | vec_add1 (a->mappings, m); |
| 3898 | } |
| 3899 | return a; |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 3900 | } |
| 3901 | |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3902 | static void |
| 3903 | queue_map_reply_for_processing (map_records_arg_t * a) |
| 3904 | { |
Florin Coras | 655fcc4 | 2017-01-10 08:57:54 -0800 | [diff] [blame] | 3905 | vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a)); |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3906 | } |
| 3907 | |
| 3908 | static void |
| 3909 | queue_map_notify_for_processing (map_records_arg_t * a) |
| 3910 | { |
| 3911 | vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0])); |
| 3912 | } |
| 3913 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3914 | static uword |
| 3915 | lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3916 | vlib_frame_t * from_frame) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3917 | { |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 3918 | u32 n_left_from, *from, *to_next_drop, rloc_probe_rep_recv = 0, |
| 3919 | map_notifies_recv = 0; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3920 | lisp_msg_type_e type; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3921 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3922 | map_records_arg_t *a; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3923 | |
| 3924 | from = vlib_frame_vector_args (from_frame); |
| 3925 | n_left_from = from_frame->n_vectors; |
| 3926 | |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3927 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3928 | while (n_left_from > 0) |
| 3929 | { |
| 3930 | u32 n_left_to_next_drop; |
| 3931 | |
| 3932 | vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3933 | to_next_drop, n_left_to_next_drop); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3934 | while (n_left_from > 0 && n_left_to_next_drop > 0) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3935 | { |
| 3936 | u32 bi0; |
| 3937 | vlib_buffer_t *b0; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3938 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3939 | bi0 = from[0]; |
| 3940 | from += 1; |
| 3941 | n_left_from -= 1; |
| 3942 | to_next_drop[0] = bi0; |
| 3943 | to_next_drop += 1; |
| 3944 | n_left_to_next_drop -= 1; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3945 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3946 | b0 = vlib_get_buffer (vm, bi0); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3947 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3948 | type = lisp_msg_type (vlib_buffer_get_current (b0)); |
| 3949 | switch (type) |
| 3950 | { |
| 3951 | case LISP_MAP_REPLY: |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 3952 | a = parse_map_reply (b0); |
| 3953 | if (a) |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 3954 | { |
| 3955 | if (a->is_rloc_probe) |
| 3956 | rloc_probe_rep_recv++; |
| 3957 | queue_map_reply_for_processing (a); |
| 3958 | } |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3959 | break; |
| 3960 | case LISP_MAP_REQUEST: |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 3961 | process_map_request (vm, node, lcm, b0); |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3962 | break; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3963 | case LISP_MAP_NOTIFY: |
Filip Tehlar | fb9931f | 2016-12-09 13:52:38 +0100 | [diff] [blame] | 3964 | a = parse_map_notify (b0); |
| 3965 | if (a) |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 3966 | { |
| 3967 | map_notifies_recv++; |
| 3968 | queue_map_notify_for_processing (a); |
| 3969 | } |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 3970 | break; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3971 | default: |
| 3972 | clib_warning ("Unsupported LISP message type %d", type); |
| 3973 | break; |
| 3974 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3975 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3976 | b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP]; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3977 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3978 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 3979 | { |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3980 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3981 | } |
| 3982 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3983 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3984 | vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP, |
| 3985 | n_left_to_next_drop); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3986 | } |
Filip Tehlar | cda7066 | 2017-01-16 10:30:03 +0100 | [diff] [blame] | 3987 | vlib_node_increment_counter (vm, node->node_index, |
| 3988 | LISP_CP_INPUT_ERROR_RLOC_PROBE_REP_RECEIVED, |
| 3989 | rloc_probe_rep_recv); |
| 3990 | vlib_node_increment_counter (vm, node->node_index, |
| 3991 | LISP_CP_INPUT_ERROR_MAP_NOTIFIES_RECEIVED, |
| 3992 | map_notifies_recv); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3993 | return from_frame->n_vectors; |
| 3994 | } |
| 3995 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 3996 | /* *INDENT-OFF* */ |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 3997 | VLIB_REGISTER_NODE (lisp_cp_input_node) = { |
| 3998 | .function = lisp_cp_input, |
| 3999 | .name = "lisp-cp-input", |
| 4000 | .vector_size = sizeof (u32), |
| 4001 | .format_trace = format_lisp_cp_input_trace, |
| 4002 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 4003 | |
| 4004 | .n_errors = LISP_CP_INPUT_N_ERROR, |
| 4005 | .error_strings = lisp_cp_input_error_strings, |
| 4006 | |
| 4007 | .n_next_nodes = LISP_CP_INPUT_N_NEXT, |
| 4008 | |
| 4009 | .next_nodes = { |
| 4010 | [LISP_CP_INPUT_NEXT_DROP] = "error-drop", |
| 4011 | }, |
| 4012 | }; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4013 | /* *INDENT-ON* */ |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 4014 | |
| 4015 | clib_error_t * |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4016 | lisp_cp_init (vlib_main_t * vm) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 4017 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4018 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 4019 | clib_error_t *error = 0; |
Florin Coras | acd4c63 | 2017-06-15 14:33:48 -0700 | [diff] [blame] | 4020 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
| 4021 | u32 num_threads; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 4022 | |
| 4023 | if ((error = vlib_call_init_function (vm, lisp_gpe_init))) |
| 4024 | return error; |
| 4025 | |
| 4026 | lcm->im4 = &ip4_main; |
| 4027 | lcm->im6 = &ip6_main; |
| 4028 | lcm->vlib_main = vm; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4029 | lcm->vnet_main = vnet_get_main (); |
Andrej Kozemcak | b6e4d39 | 2016-06-14 13:55:57 +0200 | [diff] [blame] | 4030 | lcm->mreq_itr_rlocs = ~0; |
Florin Coras | f727db9 | 2016-06-23 15:01:58 +0200 | [diff] [blame] | 4031 | lcm->lisp_pitr = 0; |
Florin Coras | ba888e4 | 2017-01-24 11:38:18 -0800 | [diff] [blame] | 4032 | lcm->flags = 0; |
Florin Coras | 5a1c11b | 2016-09-06 16:29:34 +0200 | [diff] [blame] | 4033 | memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver)); |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 4034 | memset (&lcm->active_map_server, 0, sizeof (lcm->active_map_server)); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 4035 | |
| 4036 | gid_dictionary_init (&lcm->mapping_index_by_gid); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4037 | lcm->do_map_resolver_election = 1; |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 4038 | lcm->do_map_server_election = 1; |
Florin Coras | dca8804 | 2016-09-14 16:01:38 +0200 | [diff] [blame] | 4039 | lcm->map_request_mode = MR_MODE_DST_ONLY; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 4040 | |
Florin Coras | acd4c63 | 2017-06-15 14:33:48 -0700 | [diff] [blame] | 4041 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 4042 | vec_validate (lcm->map_records_args_pool, num_threads - 1); |
| 4043 | |
Florin Coras | 577c355 | 2016-04-21 00:45:40 +0200 | [diff] [blame] | 4044 | /* default vrf mapped to vni 0 */ |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4045 | hash_set (lcm->table_id_by_vni, 0, 0); |
| 4046 | hash_set (lcm->vni_by_table_id, 0, 0); |
Florin Coras | 577c355 | 2016-04-21 00:45:40 +0200 | [diff] [blame] | 4047 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 4048 | udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4049 | lisp_cp_input_node.index, 1 /* is_ip4 */ ); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 4050 | udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4051 | lisp_cp_input_node.index, 0 /* is_ip4 */ ); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 4052 | |
Filip Tehlar | 9677a94 | 2016-11-28 10:23:31 +0100 | [diff] [blame] | 4053 | u64 now = clib_cpu_time_now (); |
| 4054 | timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second); |
Filip Tehlar | ef2a5bf | 2017-05-30 07:14:46 +0200 | [diff] [blame] | 4055 | lcm->nsh_map_index = ~0; |
Filip Tehlar | 1e553a0 | 2017-08-02 12:45:07 +0200 | [diff] [blame] | 4056 | lcm->map_register_ttl = MAP_REGISTER_DEFAULT_TTL; |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 4057 | lcm->max_expired_map_registers = MAX_EXPIRED_MAP_REGISTERS_DEFAULT; |
| 4058 | lcm->expired_map_registers = 0; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 4059 | return 0; |
| 4060 | } |
| 4061 | |
Filip Tehlar | 4868ff6 | 2017-03-09 16:48:39 +0100 | [diff] [blame] | 4062 | static int |
| 4063 | lisp_stats_api_fill (lisp_cp_main_t * lcm, lisp_gpe_main_t * lgm, |
| 4064 | lisp_api_stats_t * stat, lisp_stats_key_t * key, |
| 4065 | u32 stats_index) |
| 4066 | { |
Filip Tehlar | 2151191 | 2017-04-07 10:41:42 +0200 | [diff] [blame] | 4067 | vlib_counter_t v; |
| 4068 | vlib_combined_counter_main_t *cm = &lgm->counters; |
Filip Tehlar | 4868ff6 | 2017-03-09 16:48:39 +0100 | [diff] [blame] | 4069 | lisp_gpe_fwd_entry_key_t fwd_key; |
| 4070 | const lisp_gpe_tunnel_t *lgt; |
| 4071 | fwd_entry_t *fe; |
| 4072 | |
| 4073 | memset (stat, 0, sizeof (*stat)); |
| 4074 | memset (&fwd_key, 0, sizeof (fwd_key)); |
| 4075 | |
| 4076 | fe = pool_elt_at_index (lcm->fwd_entry_pool, key->fwd_entry_index); |
| 4077 | ASSERT (fe != 0); |
| 4078 | |
| 4079 | gid_to_dp_address (&fe->reid, &stat->deid); |
| 4080 | gid_to_dp_address (&fe->leid, &stat->seid); |
| 4081 | stat->vni = gid_address_vni (&fe->reid); |
| 4082 | |
| 4083 | lgt = lisp_gpe_tunnel_get (key->tunnel_index); |
| 4084 | stat->loc_rloc = lgt->key->lcl; |
| 4085 | stat->rmt_rloc = lgt->key->rmt; |
| 4086 | |
Filip Tehlar | 2151191 | 2017-04-07 10:41:42 +0200 | [diff] [blame] | 4087 | vlib_get_combined_counter (cm, stats_index, &v); |
| 4088 | stat->counters = v; |
Filip Tehlar | 4868ff6 | 2017-03-09 16:48:39 +0100 | [diff] [blame] | 4089 | return 1; |
| 4090 | } |
| 4091 | |
| 4092 | lisp_api_stats_t * |
| 4093 | vnet_lisp_get_stats (void) |
| 4094 | { |
| 4095 | lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main (); |
| 4096 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 4097 | lisp_api_stats_t *stats = 0, stat; |
| 4098 | lisp_stats_key_t *key; |
| 4099 | u32 index; |
| 4100 | |
| 4101 | /* *INDENT-OFF* */ |
| 4102 | hash_foreach_mem (key, index, lgm->lisp_stats_index_by_key, |
| 4103 | { |
| 4104 | if (lisp_stats_api_fill (lcm, lgm, &stat, key, index)) |
| 4105 | vec_add1 (stats, stat); |
| 4106 | }); |
| 4107 | /* *INDENT-ON* */ |
| 4108 | |
| 4109 | return stats; |
| 4110 | } |
| 4111 | |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4112 | static void * |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4113 | send_map_request_thread_fn (void *arg) |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4114 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4115 | map_request_args_t *a = arg; |
| 4116 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4117 | |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4118 | if (a->is_resend) |
| 4119 | resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked); |
| 4120 | else |
Filip Tehlar | cdab4bd | 2016-12-06 10:31:57 +0100 | [diff] [blame] | 4121 | send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4122 | |
| 4123 | return 0; |
| 4124 | } |
| 4125 | |
| 4126 | static int |
| 4127 | queue_map_request (gid_address_t * seid, gid_address_t * deid, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4128 | u8 smr_invoked, u8 is_resend) |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4129 | { |
| 4130 | map_request_args_t a; |
| 4131 | |
| 4132 | a.is_resend = is_resend; |
| 4133 | gid_address_copy (&a.seid, seid); |
| 4134 | gid_address_copy (&a.deid, deid); |
| 4135 | a.smr_invoked = smr_invoked; |
| 4136 | |
| 4137 | vl_api_rpc_call_main_thread (send_map_request_thread_fn, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4138 | (u8 *) & a, sizeof (a)); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4139 | return 0; |
| 4140 | } |
| 4141 | |
| 4142 | /** |
| 4143 | * Take an action with a pending map request depending on expiration time |
| 4144 | * and re-try counters. |
| 4145 | */ |
| 4146 | static void |
| 4147 | update_pending_request (pending_map_request_t * r, f64 dt) |
| 4148 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4149 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 4150 | lisp_msmr_t *mr; |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4151 | |
| 4152 | if (r->time_to_expire - dt < 0) |
| 4153 | /* it's time to decide what to do with this pending request */ |
| 4154 | { |
| 4155 | if (r->retries_num >= NUMBER_OF_RETRIES) |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4156 | /* too many retries -> assume current map resolver is not available */ |
| 4157 | { |
| 4158 | mr = get_map_resolver (&lcm->active_map_resolver); |
| 4159 | if (!mr) |
| 4160 | { |
| 4161 | clib_warning ("Map resolver %U not found - probably deleted " |
| 4162 | "by the user recently.", format_ip_address, |
| 4163 | &lcm->active_map_resolver); |
| 4164 | } |
| 4165 | else |
| 4166 | { |
| 4167 | clib_warning ("map resolver %U is unreachable, ignoring", |
| 4168 | format_ip_address, &lcm->active_map_resolver); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4169 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4170 | /* mark current map resolver unavailable so it won't be |
| 4171 | * selected next time */ |
| 4172 | mr->is_down = 1; |
| 4173 | mr->last_update = vlib_time_now (lcm->vlib_main); |
| 4174 | } |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4175 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4176 | reset_pending_mr_counters (r); |
| 4177 | elect_map_resolver (lcm); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4178 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4179 | /* try to find a next eligible map resolver and re-send */ |
| 4180 | queue_map_request (&r->src, &r->dst, r->is_smr_invoked, |
| 4181 | 1 /* resend */ ); |
| 4182 | } |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4183 | else |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4184 | { |
| 4185 | /* try again */ |
| 4186 | queue_map_request (&r->src, &r->dst, r->is_smr_invoked, |
| 4187 | 1 /* resend */ ); |
| 4188 | r->retries_num++; |
| 4189 | r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME; |
| 4190 | } |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4191 | } |
| 4192 | else |
| 4193 | r->time_to_expire -= dt; |
| 4194 | } |
| 4195 | |
| 4196 | static void |
| 4197 | remove_dead_pending_map_requests (lisp_cp_main_t * lcm) |
| 4198 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4199 | u64 *nonce; |
| 4200 | pending_map_request_t *pmr; |
| 4201 | u32 *to_be_removed = 0, *pmr_index; |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4202 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4203 | /* *INDENT-OFF* */ |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4204 | pool_foreach (pmr, lcm->pending_map_requests_pool, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4205 | ({ |
| 4206 | if (pmr->to_be_removed) |
| 4207 | { |
| 4208 | clib_fifo_foreach (nonce, pmr->nonces, ({ |
| 4209 | hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]); |
| 4210 | })); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4211 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4212 | vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool); |
| 4213 | } |
| 4214 | })); |
| 4215 | /* *INDENT-ON* */ |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4216 | |
| 4217 | vec_foreach (pmr_index, to_be_removed) |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 4218 | pool_put_index (lcm->pending_map_requests_pool, pmr_index[0]); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4219 | |
| 4220 | vec_free (to_be_removed); |
| 4221 | } |
| 4222 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 4223 | static void |
| 4224 | update_rloc_probing (lisp_cp_main_t * lcm, f64 dt) |
| 4225 | { |
| 4226 | static f64 time_left = RLOC_PROBING_INTERVAL; |
| 4227 | |
| 4228 | if (!lcm->is_enabled || !lcm->rloc_probing) |
| 4229 | return; |
| 4230 | |
| 4231 | time_left -= dt; |
| 4232 | if (time_left <= 0) |
| 4233 | { |
| 4234 | time_left = RLOC_PROBING_INTERVAL; |
| 4235 | send_rloc_probes (lcm); |
| 4236 | } |
| 4237 | } |
| 4238 | |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 4239 | static int |
| 4240 | update_pending_map_register (pending_map_register_t * r, f64 dt, u8 * del_all) |
| 4241 | { |
| 4242 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 4243 | lisp_msmr_t *ms; |
| 4244 | del_all[0] = 0; |
| 4245 | |
| 4246 | r->time_to_expire -= dt; |
| 4247 | |
| 4248 | if (r->time_to_expire < 0) |
| 4249 | { |
| 4250 | lcm->expired_map_registers++; |
| 4251 | |
| 4252 | if (lcm->expired_map_registers >= lcm->max_expired_map_registers) |
| 4253 | { |
| 4254 | ms = get_map_server (&lcm->active_map_server); |
| 4255 | if (!ms) |
| 4256 | { |
| 4257 | clib_warning ("Map server %U not found - probably deleted " |
| 4258 | "by the user recently.", format_ip_address, |
| 4259 | &lcm->active_map_server); |
| 4260 | } |
| 4261 | else |
| 4262 | { |
| 4263 | clib_warning ("map server %U is unreachable, ignoring", |
| 4264 | format_ip_address, &lcm->active_map_server); |
| 4265 | |
| 4266 | /* mark current map server unavailable so it won't be |
| 4267 | * elected next time */ |
| 4268 | ms->is_down = 1; |
| 4269 | ms->last_update = vlib_time_now (lcm->vlib_main); |
| 4270 | } |
| 4271 | |
| 4272 | elect_map_server (lcm); |
| 4273 | |
| 4274 | /* indication for deleting all pending map registers */ |
| 4275 | del_all[0] = 1; |
| 4276 | lcm->expired_map_registers = 0; |
| 4277 | return 0; |
| 4278 | } |
| 4279 | else |
| 4280 | { |
| 4281 | /* delete pending map register */ |
| 4282 | return 0; |
| 4283 | } |
| 4284 | } |
| 4285 | return 1; |
| 4286 | } |
| 4287 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 4288 | static void |
| 4289 | update_map_register (lisp_cp_main_t * lcm, f64 dt) |
| 4290 | { |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 4291 | u32 *to_be_removed = 0, *pmr_index; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 4292 | static f64 time_left = QUICK_MAP_REGISTER_INTERVAL; |
| 4293 | static u64 mreg_sent_counter = 0; |
| 4294 | |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 4295 | pending_map_register_t *pmr; |
| 4296 | u8 del_all = 0; |
| 4297 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 4298 | if (!lcm->is_enabled || !lcm->map_registering) |
| 4299 | return; |
| 4300 | |
Filip Tehlar | 7048ff1 | 2017-07-27 08:09:14 +0200 | [diff] [blame] | 4301 | /* *INDENT-OFF* */ |
| 4302 | pool_foreach (pmr, lcm->pending_map_registers_pool, |
| 4303 | ({ |
| 4304 | if (!update_pending_map_register (pmr, dt, &del_all)) |
| 4305 | { |
| 4306 | if (del_all) |
| 4307 | break; |
| 4308 | vec_add1 (to_be_removed, pmr - lcm->pending_map_registers_pool); |
| 4309 | } |
| 4310 | })); |
| 4311 | /* *INDENT-ON* */ |
| 4312 | |
| 4313 | if (del_all) |
| 4314 | { |
| 4315 | /* delete all pending map register messages so they won't |
| 4316 | * trigger another map server election.. */ |
| 4317 | pool_free (lcm->pending_map_registers_pool); |
| 4318 | hash_free (lcm->map_register_messages_by_nonce); |
| 4319 | |
| 4320 | /* ..and trigger registration against next map server (if any) */ |
| 4321 | time_left = 0; |
| 4322 | } |
| 4323 | else |
| 4324 | { |
| 4325 | vec_foreach (pmr_index, to_be_removed) |
| 4326 | pool_put_index (lcm->pending_map_registers_pool, pmr_index[0]); |
| 4327 | } |
| 4328 | |
| 4329 | vec_free (to_be_removed); |
| 4330 | |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 4331 | time_left -= dt; |
| 4332 | if (time_left <= 0) |
| 4333 | { |
| 4334 | if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT) |
| 4335 | time_left = MAP_REGISTER_INTERVAL; |
| 4336 | else |
| 4337 | { |
| 4338 | mreg_sent_counter++; |
| 4339 | time_left = QUICK_MAP_REGISTER_INTERVAL; |
| 4340 | } |
| 4341 | send_map_register (lcm, 1 /* want map notify */ ); |
| 4342 | } |
| 4343 | } |
| 4344 | |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4345 | static uword |
| 4346 | send_map_resolver_service (vlib_main_t * vm, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4347 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4348 | { |
Filip Tehlar | 9677a94 | 2016-11-28 10:23:31 +0100 | [diff] [blame] | 4349 | u32 *expired = 0; |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4350 | f64 period = 2.0; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4351 | pending_map_request_t *pmr; |
| 4352 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4353 | |
| 4354 | while (1) |
| 4355 | { |
| 4356 | vlib_process_wait_for_event_or_clock (vm, period); |
| 4357 | |
| 4358 | /* currently no signals are expected - just wait for clock */ |
| 4359 | (void) vlib_process_get_events (vm, 0); |
| 4360 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4361 | /* *INDENT-OFF* */ |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4362 | pool_foreach (pmr, lcm->pending_map_requests_pool, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4363 | ({ |
| 4364 | if (!pmr->to_be_removed) |
| 4365 | update_pending_request (pmr, period); |
| 4366 | })); |
| 4367 | /* *INDENT-ON* */ |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4368 | |
| 4369 | remove_dead_pending_map_requests (lcm); |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 4370 | |
| 4371 | update_map_register (lcm, period); |
| 4372 | update_rloc_probing (lcm, period); |
Filip Tehlar | 9677a94 | 2016-11-28 10:23:31 +0100 | [diff] [blame] | 4373 | |
| 4374 | u64 now = clib_cpu_time_now (); |
| 4375 | |
| 4376 | expired = timing_wheel_advance (&lcm->wheel, now, expired, 0); |
| 4377 | if (vec_len (expired) > 0) |
| 4378 | { |
| 4379 | u32 *mi = 0; |
| 4380 | vec_foreach (mi, expired) |
| 4381 | { |
| 4382 | remove_expired_mapping (lcm, mi[0]); |
| 4383 | } |
| 4384 | _vec_len (expired) = 0; |
| 4385 | } |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4386 | } |
| 4387 | |
| 4388 | /* unreachable */ |
| 4389 | return 0; |
| 4390 | } |
| 4391 | |
Filip Tehlar | 7eaf0e5 | 2017-03-08 08:46:51 +0100 | [diff] [blame] | 4392 | vnet_api_error_t |
| 4393 | vnet_lisp_stats_enable_disable (u8 enable) |
| 4394 | { |
| 4395 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 4396 | |
| 4397 | if (vnet_lisp_enable_disable_status () == 0) |
| 4398 | return VNET_API_ERROR_LISP_DISABLED; |
| 4399 | |
Filip Tehlar | 4868ff6 | 2017-03-09 16:48:39 +0100 | [diff] [blame] | 4400 | if (enable) |
| 4401 | lcm->flags |= LISP_FLAG_STATS_ENABLED; |
| 4402 | else |
| 4403 | lcm->flags &= ~LISP_FLAG_STATS_ENABLED; |
| 4404 | |
Filip Tehlar | 7eaf0e5 | 2017-03-08 08:46:51 +0100 | [diff] [blame] | 4405 | return 0; |
| 4406 | } |
| 4407 | |
| 4408 | u8 |
| 4409 | vnet_lisp_stats_enable_disable_state (void) |
| 4410 | { |
| 4411 | lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); |
| 4412 | |
| 4413 | if (vnet_lisp_enable_disable_status () == 0) |
| 4414 | return VNET_API_ERROR_LISP_DISABLED; |
| 4415 | |
Filip Tehlar | 4868ff6 | 2017-03-09 16:48:39 +0100 | [diff] [blame] | 4416 | return lcm->flags & LISP_FLAG_STATS_ENABLED; |
Filip Tehlar | 7eaf0e5 | 2017-03-08 08:46:51 +0100 | [diff] [blame] | 4417 | } |
| 4418 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4419 | /* *INDENT-OFF* */ |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4420 | VLIB_REGISTER_NODE (lisp_retry_service_node,static) = { |
| 4421 | .function = send_map_resolver_service, |
| 4422 | .type = VLIB_NODE_TYPE_PROCESS, |
| 4423 | .name = "lisp-retry-service", |
| 4424 | .process_log2_n_stack_bytes = 16, |
| 4425 | }; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4426 | /* *INDENT-ON* */ |
Filip Tehlar | a5abdeb | 2016-07-18 17:35:40 +0200 | [diff] [blame] | 4427 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 4428 | VLIB_INIT_FUNCTION (lisp_cp_init); |
| 4429 | |
| 4430 | /* |
| 4431 | * fd.io coding-style-patch-verification: ON |
| 4432 | * |
| 4433 | * Local Variables: |
| 4434 | * eval: (c-set-style "gnu") |
| 4435 | * End: |
| 4436 | */ |