blob: 6340e57e0954adb7f0daf9983c6393939a545924 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Luke16bcf7d2016-09-01 14:31:46 -040040/**
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)79b5f632016-08-02 05:40:20 -070047 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070048#ifndef included_ip_lookup_h
49#define included_ip_lookup_h
50
51#include <vnet/vnet.h>
52#include <vlib/buffer.h>
Damjan Marion102ec522016-03-29 13:18:17 +020053#include <vnet/ip/ip4_packet.h>
Pierre Pfister1dabaaf2016-04-25 14:15:15 +010054#include <vnet/ip/ip6_packet.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010055#include <vnet/fib/fib_node.h>
Neale Rannsfa5d1982017-02-20 14:19:51 -080056#include <vnet/adj/adj.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010057#include <vnet/dpo/dpo.h>
Damjan Marion22311502016-10-28 20:30:15 +020058#include <vnet/feature/feature.h>
Damjan Marionb2707892016-04-13 11:21:07 +020059
Keith Burns (alagalah)79b5f632016-08-02 05:40:20 -070060/** Flow hash configuration */
Ed Warnickecb9cada2015-12-08 15:45:58 -070061#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)
Mohsin Kazmi2af0e3a2018-11-20 11:11:12 +010067#define IP_FLOW_HASH_SYMMETRIC (1<<6)
Ed Warnickecb9cada2015-12-08 15:45:58 -070068
Keith Burns (alagalah)79b5f632016-08-02 05:40:20 -070069/** Default: 5-tuple without the "reverse" bit */
Ed Warnickecb9cada2015-12-08 15:45:58 -070070#define IP_FLOW_HASH_DEFAULT (0x1F)
71
72#define foreach_flow_hash_bit \
73_(src, IP_FLOW_HASH_SRC_ADDR) \
74_(dst, IP_FLOW_HASH_DST_ADDR) \
75_(sport, IP_FLOW_HASH_SRC_PORT) \
76_(dport, IP_FLOW_HASH_DST_PORT) \
77_(proto, IP_FLOW_HASH_PROTO) \
Mohsin Kazmi2af0e3a2018-11-20 11:11:12 +010078_(reverse, IP_FLOW_HASH_REVERSE_SRC_DST) \
79_(symmetric, IP_FLOW_HASH_SYMMETRIC)
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
Neale Ranns0bfe5d82016-08-25 15:29:12 +010081/**
82 * A flow hash configuration is a mask of the flow hash options
83 */
84typedef u32 flow_hash_config_t;
85
Neale Ranns0bfe5d82016-08-25 15:29:12 +010086/* An all zeros address */
87extern const ip46_address_t zero_addr;
Dave Barachdbf19ca2016-03-15 10:21:54 +010088
Matthew Smith6c92f5b2019-08-07 11:46:30 -050089typedef struct
90{
91 fib_prefix_t prefix;
92
93 u32 sw_if_index;
94} ip_interface_prefix_key_t;
95
96typedef struct
97{
98 /* key - prefix and sw_if_index */
99 ip_interface_prefix_key_t key;
100
101 /* number of addresses in this prefix on the interface */
102 u16 ref_count;
103
104 /* index of the interface address used as a default source address */
105 u32 src_ia_index;
106} ip_interface_prefix_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107
Dave Barachd7cb1b52016-12-09 09:52:16 -0500108typedef struct
109{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 /* Key for mhash; in fact, just a byte offset into mhash key vector. */
111 u32 address_key;
112
113 /* Interface which has this address. */
114 u32 sw_if_index;
115
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 /* Address (prefix) length for this interface. */
117 u16 address_length;
118
119 /* Will be used for something eventually. Primary vs. secondary? */
120 u16 flags;
121
122 /* Next and previous pointers for doubly linked list of
123 addresses per software interface. */
124 u32 next_this_sw_interface;
125 u32 prev_this_sw_interface;
126} ip_interface_address_t;
127
Dave Barachd7cb1b52016-12-09 09:52:16 -0500128typedef enum
129{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130 IP_LOCAL_NEXT_DROP,
131 IP_LOCAL_NEXT_PUNT,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132 IP_LOCAL_NEXT_UDP_LOOKUP,
133 IP_LOCAL_NEXT_ICMP,
Juraj Sloboda3048b632018-10-02 11:13:53 +0200134 IP_LOCAL_NEXT_REASSEMBLY,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 IP_LOCAL_N_NEXT,
136} ip_local_next_t;
137
138struct ip_lookup_main_t;
139
Dave Barachd7cb1b52016-12-09 09:52:16 -0500140typedef struct ip_lookup_main_t
141{
Keith Burns (alagalah)79b5f632016-08-02 05:40:20 -0700142 /** Pool of addresses that are assigned to interfaces. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500143 ip_interface_address_t *if_address_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144
Keith Burns (alagalah)79b5f632016-08-02 05:40:20 -0700145 /** Hash table mapping address to index in interface address pool. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146 mhash_t address_to_if_address_index;
147
Keith Burns (alagalah)79b5f632016-08-02 05:40:20 -0700148 /** Head of doubly linked list of interface addresses for each software interface.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 ~0 means this interface has no address. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500150 u32 *if_address_pool_index_by_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500152 /** Pool of prefixes containing addresses assigned to interfaces */
153 ip_interface_prefix_t *if_prefix_pool;
154
155 /** Hash table mapping prefix to index in interface prefix pool */
156 mhash_t prefix_to_if_prefix_index;
157
Keith Burns (alagalah)79b5f632016-08-02 05:40:20 -0700158 /** First table index to use for this interface, ~0 => none */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500159 u32 *classify_table_index_by_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160
Damjan Marion8b3191e2016-11-09 19:54:20 +0100161 /** Feature arc indices */
162 u8 mcast_feature_arc_index;
163 u8 ucast_feature_arc_index;
164 u8 output_feature_arc_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
Keith Burns (alagalah)79b5f632016-08-02 05:40:20 -0700166 /** Number of bytes in a fib result. Must be at least
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 sizeof (uword). First word is always adjacency index. */
168 u32 fib_result_n_bytes, fib_result_n_words;
169
Keith Burns (alagalah)79b5f632016-08-02 05:40:20 -0700170 /** 1 for ip6; 0 for ip4. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171 u32 is_ip6;
172
Keith Burns (alagalah)79b5f632016-08-02 05:40:20 -0700173 /** Either format_ip4_address_and_length or format_ip6_address_and_length. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500174 format_function_t *format_address_and_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
Keith Burns (alagalah)79b5f632016-08-02 05:40:20 -0700176 /** Table mapping ip protocol to ip[46]-local node next index. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 u8 local_next_by_ip_protocol[256];
178
Keith Burns (alagalah)79b5f632016-08-02 05:40:20 -0700179 /** IP_BUILTIN_PROTOCOL_{TCP,UDP,ICMP,OTHER} by protocol in IP header. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180 u8 builtin_protocol_by_ip_protocol[256];
181} ip_lookup_main_t;
182
Dave Barachd7cb1b52016-12-09 09:52:16 -0500183clib_error_t *ip_interface_address_add_del (ip_lookup_main_t * lm,
184 u32 sw_if_index,
185 void *address,
186 u32 address_length,
187 u32 is_del, u32 * result_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
Dave Barachd7cb1b52016-12-09 09:52:16 -0500189u8 *format_ip_flow_hash_config (u8 * s, va_list * args);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100190
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191always_inline ip_interface_address_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500192ip_get_interface_address (ip_lookup_main_t * lm, void *addr_fib)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500194 uword *p = mhash_get (&lm->address_to_if_address_index, addr_fib);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195 return p ? pool_elt_at_index (lm->if_address_pool, p[0]) : 0;
196}
197
198always_inline void *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500199ip_interface_address_get_address (ip_lookup_main_t * lm,
200 ip_interface_address_t * a)
201{
202 return mhash_key_to_mem (&lm->address_to_if_address_index, a->address_key);
203}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500205always_inline ip_interface_prefix_t *
206ip_get_interface_prefix (ip_lookup_main_t * lm, ip_interface_prefix_key_t * k)
207{
208 uword *p = mhash_get (&lm->prefix_to_if_prefix_index, k);
209 return p ? pool_elt_at_index (lm->if_prefix_pool, p[0]) : 0;
210}
211
Dave Barachd7cb1b52016-12-09 09:52:16 -0500212/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213#define foreach_ip_interface_address(lm,a,sw_if_index,loop,body) \
214do { \
Neale Rannsd5b6aa12017-05-16 08:46:45 -0700215 vnet_main_t *_vnm = vnet_get_main(); \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216 u32 _sw_if_index = sw_if_index; \
217 vnet_sw_interface_t *_swif; \
218 _swif = vnet_get_sw_interface (_vnm, _sw_if_index); \
219 \
220 /* \
221 * Loop => honor unnumbered interface addressing. \
222 */ \
Neale Rannsd5b6aa12017-05-16 08:46:45 -0700223 if (_swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) \
224 { \
225 if (loop) \
226 _sw_if_index = _swif->unnumbered_sw_if_index; \
227 else \
228 /* the interface is unnumbered, by the caller does not want \
229 * unnumbered interfaces considered/honoured */ \
230 break; \
231 } \
232 u32 _ia = ((vec_len((lm)->if_address_pool_index_by_sw_if_index) \
233 > (_sw_if_index)) ? \
234 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, \
235 (_sw_if_index)) : \
236 (u32)~0); \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 ip_interface_address_t * _a; \
238 while (_ia != ~0) \
239 { \
240 _a = pool_elt_at_index ((lm)->if_address_pool, _ia); \
241 _ia = _a->next_this_sw_interface; \
242 (a) = _a; \
243 body; \
244 } \
245} while (0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500246/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
Florin Coras3a0325f2018-06-01 12:22:23 -0700248always_inline void
249ip_lookup_set_buffer_fib_index (u32 * fib_index_by_sw_if_index,
250 vlib_buffer_t * b)
251{
Neale Ranns9b2f51a2018-09-11 05:55:50 -0700252 /* *INDENT-OFF* */
Florin Coras3a0325f2018-06-01 12:22:23 -0700253 vnet_buffer (b)->ip.fib_index =
254 vec_elt (fib_index_by_sw_if_index, vnet_buffer (b)->sw_if_index[VLIB_RX]);
255 vnet_buffer (b)->ip.fib_index =
Neale Ranns9b2f51a2018-09-11 05:55:50 -0700256 ((vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
257 vnet_buffer (b)->ip.fib_index :
258 vnet_buffer (b)->sw_if_index[VLIB_TX]);
259 /* *INDENT-ON* */
Florin Coras3a0325f2018-06-01 12:22:23 -0700260}
261
Florin Coras595992c2017-11-06 17:17:08 -0800262typedef struct _vnet_ip_container_proxy_args
263{
264 fib_prefix_t prefix;
265 u32 sw_if_index;
266 u8 is_add;
267} vnet_ip_container_proxy_args_t;
268
269clib_error_t *vnet_ip_container_proxy_add_del (vnet_ip_container_proxy_args_t
270 * args);
271
Matus Fabian75b9f452018-10-02 23:27:21 -0700272typedef int (*ip_container_proxy_cb_t) (const fib_prefix_t * pfx,
273 u32 sw_if_index, void *ctx);
274void ip_container_proxy_walk (ip_container_proxy_cb_t cb, void *ctx);
275
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276void ip_lookup_init (ip_lookup_main_t * lm, u32 ip_lookup_node_index);
277
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278#endif /* included_ip_lookup_h */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500279/*
280 * fd.io coding-style-patch-verification: ON
281 *
282 * Local Variables:
283 * eval: (c-set-style "gnu")
284 * End:
285 */