ip: Replace Sematics for Interface IP addresses
[vpp.git] / src / vnet / ip / lookup.h
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
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.
47  */
48 #ifndef included_ip_lookup_h
49 #define included_ip_lookup_h
50
51 #include <vnet/vnet.h>
52 #include <vlib/buffer.h>
53 #include <vnet/ip/ip4_packet.h>
54 #include <vnet/ip/ip6_packet.h>
55 #include <vnet/fib/fib_node.h>
56 #include <vnet/adj/adj.h>
57 #include <vnet/dpo/dpo.h>
58 #include <vnet/feature/feature.h>
59
60 /** Flow hash configuration */
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 #define IP_FLOW_HASH_SYMMETRIC (1<<6)
68
69 /** Default: 5-tuple without the "reverse" bit */
70 #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)                    \
78 _(reverse, IP_FLOW_HASH_REVERSE_SRC_DST)        \
79 _(symmetric, IP_FLOW_HASH_SYMMETRIC)
80
81 /**
82  * A flow hash configuration is a mask of the flow hash options
83  */
84 typedef u32 flow_hash_config_t;
85
86 /* An all zeros address */
87 extern const ip46_address_t zero_addr;
88
89 typedef enum ip_interface_address_flags_t_
90 {
91   IP_INTERFACE_ADDRESS_FLAG_STALE = (1 << 0),
92 } __clib_packed ip_interface_address_flags_t;
93
94 typedef struct
95 {
96   fib_prefix_t prefix;
97
98   u32 sw_if_index;
99 } ip_interface_prefix_key_t;
100
101 typedef struct
102 {
103   /* key - prefix and sw_if_index */
104   ip_interface_prefix_key_t key;
105
106   /* number of addresses in this prefix on the interface */
107   u16 ref_count;
108
109   /* index of the interface address used as a default source address */
110   u32 src_ia_index;
111 } ip_interface_prefix_t;
112
113 typedef struct
114 {
115   /* Key for mhash; in fact, just a byte offset into mhash key vector. */
116   u32 address_key;
117
118   /* Interface which has this address. */
119   u32 sw_if_index;
120
121   /* Address (prefix) length for this interface. */
122   u16 address_length;
123
124   /* flags relating to this prefix */
125   ip_interface_address_flags_t flags;
126
127   /* Next and previous pointers for doubly linked list of
128      addresses per software interface. */
129   u32 next_this_sw_interface;
130   u32 prev_this_sw_interface;
131 } ip_interface_address_t;
132
133 typedef enum
134 {
135   IP_LOCAL_NEXT_DROP,
136   IP_LOCAL_NEXT_PUNT,
137   IP_LOCAL_NEXT_UDP_LOOKUP,
138   IP_LOCAL_NEXT_ICMP,
139   IP_LOCAL_NEXT_REASSEMBLY,
140   IP_LOCAL_N_NEXT,
141 } ip_local_next_t;
142
143 struct ip_lookup_main_t;
144
145 typedef struct ip_lookup_main_t
146 {
147   /** Pool of addresses that are assigned to interfaces. */
148   ip_interface_address_t *if_address_pool;
149
150   /** Hash table mapping address to index in interface address pool. */
151   mhash_t address_to_if_address_index;
152
153   /** Head of doubly linked list of interface addresses for each software interface.
154      ~0 means this interface has no address. */
155   u32 *if_address_pool_index_by_sw_if_index;
156
157   /** Pool of prefixes containing addresses assigned to interfaces */
158   ip_interface_prefix_t *if_prefix_pool;
159
160   /** Hash table mapping prefix to index in interface prefix pool */
161   mhash_t prefix_to_if_prefix_index;
162
163   /** First table index to use for this interface, ~0 => none */
164   u32 *classify_table_index_by_sw_if_index;
165
166   /** Feature arc indices */
167   u8 mcast_feature_arc_index;
168   u8 ucast_feature_arc_index;
169   u8 output_feature_arc_index;
170
171   /** Number of bytes in a fib result.  Must be at least
172      sizeof (uword).  First word is always adjacency index. */
173   u32 fib_result_n_bytes, fib_result_n_words;
174
175   /** 1 for ip6; 0 for ip4. */
176   u32 is_ip6;
177
178   /** Either format_ip4_address_and_length or format_ip6_address_and_length. */
179   format_function_t *format_address_and_length;
180
181   /** Table mapping ip protocol to ip[46]-local node next index. */
182   u8 local_next_by_ip_protocol[256];
183
184   /** IP_BUILTIN_PROTOCOL_{TCP,UDP,ICMP,OTHER} by protocol in IP header. */
185   u8 builtin_protocol_by_ip_protocol[256];
186 } ip_lookup_main_t;
187
188 u8 *format_ip_flow_hash_config (u8 * s, va_list * args);
189
190
191 always_inline void
192 ip_lookup_set_buffer_fib_index (u32 * fib_index_by_sw_if_index,
193                                 vlib_buffer_t * b)
194 {
195   /* *INDENT-OFF* */
196   vnet_buffer (b)->ip.fib_index =
197     vec_elt (fib_index_by_sw_if_index, vnet_buffer (b)->sw_if_index[VLIB_RX]);
198   vnet_buffer (b)->ip.fib_index =
199     ((vnet_buffer (b)->sw_if_index[VLIB_TX] ==  (u32) ~ 0) ?
200      vnet_buffer (b)->ip.fib_index :
201      vnet_buffer (b)->sw_if_index[VLIB_TX]);
202   /* *INDENT-ON* */
203 }
204
205 typedef struct _vnet_ip_container_proxy_args
206 {
207   fib_prefix_t prefix;
208   u32 sw_if_index;
209   u8 is_add;
210 } vnet_ip_container_proxy_args_t;
211
212 clib_error_t *vnet_ip_container_proxy_add_del (vnet_ip_container_proxy_args_t
213                                                * args);
214
215 typedef int (*ip_container_proxy_cb_t) (const fib_prefix_t * pfx,
216                                         u32 sw_if_index, void *ctx);
217 void ip_container_proxy_walk (ip_container_proxy_cb_t cb, void *ctx);
218
219 void ip_lookup_init (ip_lookup_main_t * lm, u32 ip_lookup_node_index);
220
221 #endif /* included_ip_lookup_h */
222 /*
223  * fd.io coding-style-patch-verification: ON
224  *
225  * Local Variables:
226  * eval: (c-set-style "gnu")
227  * End:
228  */