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