Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | /* |
| 16 | * ip/ip_lookup.h: ip (4 or 6) lookup structures, adjacencies, ... |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 40 | /** |
| 41 | * @file |
| 42 | * Definitions for all things IP (v4|v6) unicast and multicast lookup related. |
| 43 | * |
| 44 | * - Adjacency definitions and registration. |
| 45 | * - Callbacks on route add. |
| 46 | * - Callbacks on interface address change. |
Keith Burns (alagalah) | 79b5f63 | 2016-08-02 05:40:20 -0700 | [diff] [blame] | 47 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | #ifndef included_ip_lookup_h |
| 49 | #define included_ip_lookup_h |
| 50 | |
| 51 | #include <vnet/vnet.h> |
| 52 | #include <vlib/buffer.h> |
Damjan Marion | 102ec52 | 2016-03-29 13:18:17 +0200 | [diff] [blame] | 53 | #include <vnet/ip/ip4_packet.h> |
Pierre Pfister | 1dabaaf | 2016-04-25 14:15:15 +0100 | [diff] [blame] | 54 | #include <vnet/ip/ip6_packet.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 55 | #include <vnet/fib/fib_node.h> |
Neale Ranns | fa5d198 | 2017-02-20 14:19:51 -0800 | [diff] [blame] | 56 | #include <vnet/adj/adj.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 57 | #include <vnet/dpo/dpo.h> |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 58 | #include <vnet/feature/feature.h> |
Damjan Marion | b270789 | 2016-04-13 11:21:07 +0200 | [diff] [blame] | 59 | |
Keith Burns (alagalah) | 79b5f63 | 2016-08-02 05:40:20 -0700 | [diff] [blame] | 60 | /** Flow hash configuration */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | #define IP_FLOW_HASH_SRC_ADDR (1<<0) |
| 62 | #define IP_FLOW_HASH_DST_ADDR (1<<1) |
| 63 | #define IP_FLOW_HASH_PROTO (1<<2) |
| 64 | #define IP_FLOW_HASH_SRC_PORT (1<<3) |
| 65 | #define IP_FLOW_HASH_DST_PORT (1<<4) |
| 66 | #define IP_FLOW_HASH_REVERSE_SRC_DST (1<<5) |
| 67 | |
Keith Burns (alagalah) | 79b5f63 | 2016-08-02 05:40:20 -0700 | [diff] [blame] | 68 | /** Default: 5-tuple without the "reverse" bit */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | #define IP_FLOW_HASH_DEFAULT (0x1F) |
| 70 | |
| 71 | #define foreach_flow_hash_bit \ |
| 72 | _(src, IP_FLOW_HASH_SRC_ADDR) \ |
| 73 | _(dst, IP_FLOW_HASH_DST_ADDR) \ |
| 74 | _(sport, IP_FLOW_HASH_SRC_PORT) \ |
| 75 | _(dport, IP_FLOW_HASH_DST_PORT) \ |
| 76 | _(proto, IP_FLOW_HASH_PROTO) \ |
| 77 | _(reverse, IP_FLOW_HASH_REVERSE_SRC_DST) |
| 78 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 79 | /** |
| 80 | * A flow hash configuration is a mask of the flow hash options |
| 81 | */ |
| 82 | typedef u32 flow_hash_config_t; |
| 83 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 84 | /* An all zeros address */ |
| 85 | extern const ip46_address_t zero_addr; |
Dave Barach | dbf19ca | 2016-03-15 10:21:54 +0100 | [diff] [blame] | 86 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 88 | typedef struct |
| 89 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | /* Key for mhash; in fact, just a byte offset into mhash key vector. */ |
| 91 | u32 address_key; |
| 92 | |
| 93 | /* Interface which has this address. */ |
| 94 | u32 sw_if_index; |
| 95 | |
| 96 | /* Adjacency for neighbor probe (ARP) for this interface address. */ |
| 97 | u32 neighbor_probe_adj_index; |
| 98 | |
| 99 | /* Address (prefix) length for this interface. */ |
| 100 | u16 address_length; |
| 101 | |
| 102 | /* Will be used for something eventually. Primary vs. secondary? */ |
| 103 | u16 flags; |
| 104 | |
| 105 | /* Next and previous pointers for doubly linked list of |
| 106 | addresses per software interface. */ |
| 107 | u32 next_this_sw_interface; |
| 108 | u32 prev_this_sw_interface; |
| 109 | } ip_interface_address_t; |
| 110 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 111 | typedef enum |
| 112 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | IP_LOCAL_NEXT_DROP, |
| 114 | IP_LOCAL_NEXT_PUNT, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | IP_LOCAL_NEXT_UDP_LOOKUP, |
| 116 | IP_LOCAL_NEXT_ICMP, |
| 117 | IP_LOCAL_N_NEXT, |
| 118 | } ip_local_next_t; |
| 119 | |
| 120 | struct ip_lookup_main_t; |
| 121 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 122 | typedef struct ip_lookup_main_t |
| 123 | { |
Keith Burns (alagalah) | 79b5f63 | 2016-08-02 05:40:20 -0700 | [diff] [blame] | 124 | /** Pool of addresses that are assigned to interfaces. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 125 | ip_interface_address_t *if_address_pool; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | |
Keith Burns (alagalah) | 79b5f63 | 2016-08-02 05:40:20 -0700 | [diff] [blame] | 127 | /** Hash table mapping address to index in interface address pool. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | mhash_t address_to_if_address_index; |
| 129 | |
Keith Burns (alagalah) | 79b5f63 | 2016-08-02 05:40:20 -0700 | [diff] [blame] | 130 | /** Head of doubly linked list of interface addresses for each software interface. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | ~0 means this interface has no address. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 132 | u32 *if_address_pool_index_by_sw_if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | |
Keith Burns (alagalah) | 79b5f63 | 2016-08-02 05:40:20 -0700 | [diff] [blame] | 134 | /** First table index to use for this interface, ~0 => none */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 135 | u32 *classify_table_index_by_sw_if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 137 | /** Feature arc indices */ |
| 138 | u8 mcast_feature_arc_index; |
| 139 | u8 ucast_feature_arc_index; |
| 140 | u8 output_feature_arc_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | |
Keith Burns (alagalah) | 79b5f63 | 2016-08-02 05:40:20 -0700 | [diff] [blame] | 142 | /** Number of bytes in a fib result. Must be at least |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | sizeof (uword). First word is always adjacency index. */ |
| 144 | u32 fib_result_n_bytes, fib_result_n_words; |
| 145 | |
Keith Burns (alagalah) | 79b5f63 | 2016-08-02 05:40:20 -0700 | [diff] [blame] | 146 | /** 1 for ip6; 0 for ip4. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | u32 is_ip6; |
| 148 | |
Keith Burns (alagalah) | 79b5f63 | 2016-08-02 05:40:20 -0700 | [diff] [blame] | 149 | /** Either format_ip4_address_and_length or format_ip6_address_and_length. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 150 | format_function_t *format_address_and_length; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | |
Keith Burns (alagalah) | 79b5f63 | 2016-08-02 05:40:20 -0700 | [diff] [blame] | 152 | /** Table mapping ip protocol to ip[46]-local node next index. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | u8 local_next_by_ip_protocol[256]; |
| 154 | |
Keith Burns (alagalah) | 79b5f63 | 2016-08-02 05:40:20 -0700 | [diff] [blame] | 155 | /** IP_BUILTIN_PROTOCOL_{TCP,UDP,ICMP,OTHER} by protocol in IP header. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | u8 builtin_protocol_by_ip_protocol[256]; |
| 157 | } ip_lookup_main_t; |
| 158 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 159 | clib_error_t *ip_interface_address_add_del (ip_lookup_main_t * lm, |
| 160 | u32 sw_if_index, |
| 161 | void *address, |
| 162 | u32 address_length, |
| 163 | u32 is_del, u32 * result_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 165 | u8 *format_ip_flow_hash_config (u8 * s, va_list * args); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 166 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | always_inline ip_interface_address_t * |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 168 | ip_get_interface_address (ip_lookup_main_t * lm, void *addr_fib) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 170 | uword *p = mhash_get (&lm->address_to_if_address_index, addr_fib); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | return p ? pool_elt_at_index (lm->if_address_pool, p[0]) : 0; |
| 172 | } |
| 173 | |
| 174 | always_inline void * |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 175 | ip_interface_address_get_address (ip_lookup_main_t * lm, |
| 176 | ip_interface_address_t * a) |
| 177 | { |
| 178 | return mhash_key_to_mem (&lm->address_to_if_address_index, a->address_key); |
| 179 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 181 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | #define foreach_ip_interface_address(lm,a,sw_if_index,loop,body) \ |
| 183 | do { \ |
| 184 | vnet_main_t *_vnm = vnet_get_main(); \ |
| 185 | u32 _sw_if_index = sw_if_index; \ |
| 186 | vnet_sw_interface_t *_swif; \ |
| 187 | _swif = vnet_get_sw_interface (_vnm, _sw_if_index); \ |
| 188 | \ |
| 189 | /* \ |
| 190 | * Loop => honor unnumbered interface addressing. \ |
| 191 | */ \ |
| 192 | if (loop && _swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) \ |
| 193 | _sw_if_index = _swif->unnumbered_sw_if_index; \ |
| 194 | u32 _ia = \ |
| 195 | (vec_len((lm)->if_address_pool_index_by_sw_if_index) \ |
| 196 | > (_sw_if_index)) \ |
| 197 | ? vec_elt ((lm)->if_address_pool_index_by_sw_if_index, \ |
| 198 | (_sw_if_index)) : (u32)~0; \ |
| 199 | ip_interface_address_t * _a; \ |
| 200 | while (_ia != ~0) \ |
| 201 | { \ |
| 202 | _a = pool_elt_at_index ((lm)->if_address_pool, _ia); \ |
| 203 | _ia = _a->next_this_sw_interface; \ |
| 204 | (a) = _a; \ |
| 205 | body; \ |
| 206 | } \ |
| 207 | } while (0) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 208 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | |
| 210 | void ip_lookup_init (ip_lookup_main_t * lm, u32 ip_lookup_node_index); |
| 211 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | #endif /* included_ip_lookup_h */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 213 | |
| 214 | /* |
| 215 | * fd.io coding-style-patch-verification: ON |
| 216 | * |
| 217 | * Local Variables: |
| 218 | * eval: (c-set-style "gnu") |
| 219 | * End: |
| 220 | */ |