FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
lookup.h
Go to the documentation of this file.
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/dpo/dpo.h>
57 #include <vnet/feature/feature.h>
58 
59 /** @brief Common (IP4/IP6) next index stored in adjacency. */
60 typedef enum {
61  /** Adjacency to drop this packet. */
63  /** Adjacency to punt this packet. */
65 
66  /** This packet is for one of our own IP addresses. */
68 
69  /** This packet matches an "incomplete adjacency" and packets
70  need to be passed to ARP to find rewrite string for
71  this destination. */
73 
74  /** This packet matches an "interface route" and packets
75  need to be passed to ARP to find rewrite string for
76  this destination. */
78 
79  /** This packet is to be rewritten and forwarded to the next
80  processing node. This is typically the output interface but
81  might be another node for further output processing. */
83 
84  /** This packets follow a load-balance */
86 
87  /** This packets follow a mid-chain adjacency */
89 
90  /** This packets needs to go to ICMP error */
92 
95 
96 typedef enum {
99 
100 typedef enum {
101  /* Hop-by-hop header handling */
107 
108 #define IP4_LOOKUP_NEXT_NODES { \
109  [IP_LOOKUP_NEXT_DROP] = "ip4-drop", \
110  [IP_LOOKUP_NEXT_PUNT] = "ip4-punt", \
111  [IP_LOOKUP_NEXT_LOCAL] = "ip4-local", \
112  [IP_LOOKUP_NEXT_ARP] = "ip4-arp", \
113  [IP_LOOKUP_NEXT_GLEAN] = "ip4-glean", \
114  [IP_LOOKUP_NEXT_REWRITE] = "ip4-rewrite-transit", \
115  [IP_LOOKUP_NEXT_MIDCHAIN] = "ip4-midchain", \
116  [IP_LOOKUP_NEXT_LOAD_BALANCE] = "ip4-load-balance", \
117  [IP_LOOKUP_NEXT_ICMP_ERROR] = "ip4-icmp-error", \
118 }
119 
120 #define IP6_LOOKUP_NEXT_NODES { \
121  [IP_LOOKUP_NEXT_DROP] = "ip6-drop", \
122  [IP_LOOKUP_NEXT_PUNT] = "ip6-punt", \
123  [IP_LOOKUP_NEXT_LOCAL] = "ip6-local", \
124  [IP_LOOKUP_NEXT_ARP] = "ip6-discover-neighbor", \
125  [IP_LOOKUP_NEXT_GLEAN] = "ip6-glean", \
126  [IP_LOOKUP_NEXT_REWRITE] = "ip6-rewrite", \
127  [IP_LOOKUP_NEXT_MIDCHAIN] = "ip6-midchain", \
128  [IP_LOOKUP_NEXT_LOAD_BALANCE] = "ip6-load-balance", \
129  [IP_LOOKUP_NEXT_ICMP_ERROR] = "ip6-icmp-error", \
130  [IP6_LOOKUP_NEXT_HOP_BY_HOP] = "ip6-hop-by-hop", \
131  [IP6_LOOKUP_NEXT_ADD_HOP_BY_HOP] = "ip6-add-hop-by-hop", \
132  [IP6_LOOKUP_NEXT_POP_HOP_BY_HOP] = "ip6-pop-hop-by-hop", \
133 }
134 
135 /** Flow hash configuration */
136 #define IP_FLOW_HASH_SRC_ADDR (1<<0)
137 #define IP_FLOW_HASH_DST_ADDR (1<<1)
138 #define IP_FLOW_HASH_PROTO (1<<2)
139 #define IP_FLOW_HASH_SRC_PORT (1<<3)
140 #define IP_FLOW_HASH_DST_PORT (1<<4)
141 #define IP_FLOW_HASH_REVERSE_SRC_DST (1<<5)
142 
143 /** Default: 5-tuple without the "reverse" bit */
144 #define IP_FLOW_HASH_DEFAULT (0x1F)
145 
146 #define foreach_flow_hash_bit \
147 _(src, IP_FLOW_HASH_SRC_ADDR) \
148 _(dst, IP_FLOW_HASH_DST_ADDR) \
149 _(sport, IP_FLOW_HASH_SRC_PORT) \
150 _(dport, IP_FLOW_HASH_DST_PORT) \
151 _(proto, IP_FLOW_HASH_PROTO) \
152 _(reverse, IP_FLOW_HASH_REVERSE_SRC_DST)
153 
154 /**
155  * A flow hash configuration is a mask of the flow hash options
156  */
158 
159 /**
160  * Forward delcartion
161  */
162 struct ip_adjacency_t_;
163 
164 /**
165  * @brief A function type for post-rewrite fixups on midchain adjacency
166  */
167 typedef void (*adj_midchain_fixup_t)(vlib_main_t * vm,
168  struct ip_adjacency_t_ *adj,
169  vlib_buffer_t * b0);
170 
171 /** @brief IP unicast adjacency.
172  @note cache aligned.
173 */
174 typedef struct ip_adjacency_t_ {
175  CLIB_CACHE_LINE_ALIGN_MARK(cacheline0);
176 
177  /** Number of adjecencies in block. Greater than 1 means multipath;
178  otherwise equal to 1. */
180 
181  /** Next hop after ip4-lookup. */
182  union {
183  ip_lookup_next_t lookup_next_index : 16;
185  };
186 
187  /** Interface address index for this local/arp adjacency. */
189 
190  /** Force re-lookup in a different FIB. ~0 => normal behavior */
192 
193  /** Highest possible perf subgraph arc interposition, e.g. for ip6 ioam */
195 
196  /*
197  * link/ether-type
198  */
201 
202  union {
203  /**
204  * IP_LOOKUP_NEXT_ARP/IP_LOOKUP_NEXT_REWRITE
205  *
206  * neighbour adjacency sub-type;
207  */
208  struct {
209  ip46_address_t next_hop;
210  } nbr;
211  /**
212  * IP_LOOKUP_NEXT_MIDCHAIN
213  *
214  * A nbr adj that is also recursive. Think tunnels.
215  * A nbr adj can transition to be of type MDICHAIN
216  * so be sure to leave the two structs with the next_hop
217  * fields aligned.
218  */
219  struct {
220  /**
221  * The recursive next-hop
222  */
223  ip46_address_t next_hop;
224  /**
225  * The node index of the tunnel's post rewrite/TX function.
226  */
228  /**
229  * The next DPO to use
230  */
232  /**
233  * A function to perform the post-rewrite fixup
234  */
236  } midchain;
237  /**
238  * IP_LOOKUP_NEXT_GLEAN
239  *
240  * Glean the address to ARP for from the packet's destination
241  */
242  struct {
243  ip46_address_t receive_addr;
244  } glean;
245  } sub_type;
246 
247  CLIB_CACHE_LINE_ALIGN_MARK(cacheline1);
248 
249  /* Rewrite in second/third cache lines */
251 
252  /*
253  * member not accessed in the data plane are relgated to the
254  * remaining cachelines
255  */
258 
259 _Static_assert((STRUCT_OFFSET_OF(ip_adjacency_t, cacheline0) == 0),
260  "IP adjacency cachline 0 is not offset");
261 _Static_assert((STRUCT_OFFSET_OF(ip_adjacency_t, cacheline1) ==
263  "IP adjacency cachline 1 is more than one cachline size offset");
264 
265 /* An all zeros address */
266 extern const ip46_address_t zero_addr;
267 
268 /* IP multicast adjacency. */
269 typedef struct {
270  /* Handle for this adjacency in adjacency heap. */
272 
273  /* Number of adjecencies in block. */
275 
276  /* Rewrite string. */
277  vnet_declare_rewrite (64 - 2*sizeof(u32));
279 
280 typedef struct {
281  /* ip4-multicast-rewrite next index. */
283 
285 
286  u8 rewrite_string[64 - 1*sizeof(u32) - 1*sizeof(u8)];
288 
289 typedef struct {
291 
293 
294  /* Negative rewrite string index; >= 0 sw_if_index.
295  Sorted. Used to hash. */
297 
300 
301 typedef struct {
302  /* Key for mhash; in fact, just a byte offset into mhash key vector. */
304 
305  /* Interface which has this address. */
307 
308  /* Adjacency for neighbor probe (ARP) for this interface address. */
310 
311  /* Address (prefix) length for this interface. */
313 
314  /* Will be used for something eventually. Primary vs. secondary? */
316 
317  /* Next and previous pointers for doubly linked list of
318  addresses per software interface. */
322 
323 typedef enum {
330 
331 struct ip_lookup_main_t;
332 
333 typedef struct ip_lookup_main_t {
334  /* Adjacency heap. */
336 
337  /** load-balance packet/byte counters indexed by LB index. */
339 
340  /** any-tx-feature-enabled interface bitmap */
342 
343  /** count of enabled features, per sw_if_index, to maintain bitmap */
345 
346  /** Pool of addresses that are assigned to interfaces. */
348 
349  /** Hash table mapping address to index in interface address pool. */
351 
352  /** Head of doubly linked list of interface addresses for each software interface.
353  ~0 means this interface has no address. */
355 
356  /** First table index to use for this interface, ~0 => none */
358 
359  /** rx unicast, multicast, tx interface/feature configuration. */
361 
362  /** Number of bytes in a fib result. Must be at least
363  sizeof (uword). First word is always adjacency index. */
364  u32 fib_result_n_bytes, fib_result_n_words;
365 
367 
368  /** 1 for ip6; 0 for ip4. */
370 
371  /** Either format_ip4_address_and_length or format_ip6_address_and_length. */
373 
374  /** Special adjacency format functions */
376 
377  /** Table mapping ip protocol to ip[46]-local node next index. */
378  u8 local_next_by_ip_protocol[256];
379 
380  /** IP_BUILTIN_PROTOCOL_{TCP,UDP,ICMP,OTHER} by protocol in IP header. */
381  u8 builtin_protocol_by_ip_protocol[256];
383 
386  u32 adj_index)
387 {
388  ip_adjacency_t * adj;
389 
390  adj = vec_elt_at_index (lm->adjacency_heap, adj_index);
391 
392  return adj;
393 }
394 
395 #define ip_prefetch_adjacency(lm,adj_index,type) \
396 do { \
397  ip_adjacency_t * _adj = (lm)->adjacency_heap + (adj_index); \
398  CLIB_PREFETCH (_adj, sizeof (_adj[0]), type); \
399 } while (0)
400 
401 /* Create new block of given number of contiguous adjacencies. */
404  ip_adjacency_t * adj,
405  u32 n_adj,
406  u32 * adj_index_result);
407 
408 clib_error_t *
410  u32 sw_if_index,
411  void * address,
412  u32 address_length,
413  u32 is_del,
414  u32 * result_index);
415 
416 u8 *
417 format_ip_flow_hash_config (u8 * s, va_list * args);
418 
421 {
422  uword * p = mhash_get (&lm->address_to_if_address_index, addr_fib);
423  return p ? pool_elt_at_index (lm->if_address_pool, p[0]) : 0;
424 }
425 
426 u32
428  u32 table_id);
429 
430 always_inline void *
433 
434 #define foreach_ip_interface_address(lm,a,sw_if_index,loop,body) \
435 do { \
436  vnet_main_t *_vnm = vnet_get_main(); \
437  u32 _sw_if_index = sw_if_index; \
438  vnet_sw_interface_t *_swif; \
439  _swif = vnet_get_sw_interface (_vnm, _sw_if_index); \
440  \
441  /* \
442  * Loop => honor unnumbered interface addressing. \
443  */ \
444  if (loop && _swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) \
445  _sw_if_index = _swif->unnumbered_sw_if_index; \
446  u32 _ia = \
447  (vec_len((lm)->if_address_pool_index_by_sw_if_index) \
448  > (_sw_if_index)) \
449  ? vec_elt ((lm)->if_address_pool_index_by_sw_if_index, \
450  (_sw_if_index)) : (u32)~0; \
451  ip_interface_address_t * _a; \
452  while (_ia != ~0) \
453  { \
454  _a = pool_elt_at_index ((lm)->if_address_pool, _ia); \
455  _ia = _a->next_this_sw_interface; \
456  (a) = _a; \
457  body; \
458  } \
459 } while (0)
460 
461 void ip_lookup_init (ip_lookup_main_t * lm, u32 ip_lookup_node_index);
462 
463 #endif /* included_ip_lookup_h */
This packets follow a load-balance.
Definition: lookup.h:85
uword * adjacency_by_id_vector
Definition: lookup.h:298
format_function_t * format_fib_result
Definition: lookup.h:366
ip_lookup_next_t
Common (IP4/IP6) next index stored in adjacency.
Definition: lookup.h:60
Definition: mhash.h:46
uword * tx_sw_if_has_ip_output_features
any-tx-feature-enabled interface bitmap
Definition: lookup.h:341
ip_adjacency_t * adjacency_heap
Definition: lookup.h:335
a
Definition: bitmap.h:516
ip_multicast_rewrite_t * rewrite_heap
Definition: lookup.h:290
clib_error_t * ip_interface_address_add_del(ip_lookup_main_t *lm, u32 sw_if_index, void *address, u32 address_length, u32 is_del, u32 *result_index)
Definition: lookup.c:59
ip_interface_address_t * if_address_pool
Pool of addresses that are assigned to interfaces.
Definition: lookup.h:347
IP unicast adjacency.
Definition: lookup.h:174
struct ip_adjacency_t_::@164::@165 nbr
IP_LOOKUP_NEXT_ARP/IP_LOOKUP_NEXT_REWRITE.
#define STRUCT_OFFSET_OF(t, f)
Definition: clib.h:62
struct ip_adjacency_t_ ip_adjacency_t
IP unicast adjacency.
adj_midchain_fixup_t fixup_func
A function to perform the post-rewrite fixup.
Definition: lookup.h:235
u32 neighbor_probe_adj_index
Definition: lookup.h:309
mhash_t address_to_if_address_index
Hash table mapping address to index in interface address pool.
Definition: lookup.h:350
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
Adjacency to drop this packet.
Definition: lookup.h:62
vnet_link_t ia_link
Definition: lookup.h:199
ip46_address_t receive_addr
Definition: lookup.h:243
ip6_lookup_next_t
Definition: lookup.h:100
This packets needs to go to ICMP error.
Definition: lookup.h:91
#define always_inline
Definition: clib.h:84
int i32
Definition: types.h:81
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
ip46_address_t next_hop
The recursive next-hop.
Definition: lookup.h:209
Adjacency to punt this packet.
Definition: lookup.h:64
This packet is for one of our own IP addresses.
Definition: lookup.h:67
This packet matches an "interface route" and packets need to be passed to ARP to find rewrite string ...
Definition: lookup.h:77
#define VLIB_BUFFER_PRE_DATA_SIZE
Definition: buffer.h:52
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:138
dpo_id_t next_dpo
The next DPO to use.
Definition: lookup.h:231
struct ip_lookup_main_t ip_lookup_main_t
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
CLIB_CACHE_LINE_ALIGN_MARK(cacheline0)
u32 * classify_table_index_by_sw_if_index
First table index to use for this interface, ~0 => none.
Definition: lookup.h:357
u16 lookup_next_index_as_int
Definition: lookup.h:184
u32 tx_function_node
The node index of the tunnel&#39;s post rewrite/TX function.
Definition: lookup.h:227
i16 * tx_feature_count_by_sw_if_index
count of enabled features, per sw_if_index, to maintain bitmap
Definition: lookup.h:344
u8 * format_ip_flow_hash_config(u8 *s, va_list *args)
Definition: lookup.c:210
An node in the FIB graph.
Definition: fib_node.h:242
fib_node_t ia_node
Definition: lookup.h:256
union ip_adjacency_t_::@164 sub_type
const ip46_address_t zero_addr
Definition: lookup.c:309
u16 mcast_group_index
Force re-lookup in a different FIB.
Definition: lookup.h:191
static ip_interface_address_t * ip_get_interface_address(ip_lookup_main_t *lm, void *addr_fib)
Definition: lookup.h:420
static uword * mhash_get(mhash_t *h, void *key)
Definition: mhash.h:110
ip_adjacency_t * ip_add_adjacency(ip_lookup_main_t *lm, ip_adjacency_t *adj, u32 n_adj, u32 *adj_index_result)
Definition: adj.c:426
This packet matches an "incomplete adjacency" and packets need to be passed to ARP to find rewrite st...
Definition: lookup.h:72
u32 * if_address_pool_index_by_sw_if_index
Head of doubly linked list of interface addresses for each software interface.
Definition: lookup.h:354
unsigned int u32
Definition: types.h:88
enum vnet_link_t_ vnet_link_t
Link Type: A description of the protocol of packets on the link.
This packets follow a mid-chain adjacency.
Definition: lookup.h:88
u32 fib_result_n_words
Definition: lookup.h:364
u32 if_address_index
Interface address index for this local/arp adjacency.
Definition: lookup.h:188
void(* adj_midchain_fixup_t)(vlib_main_t *vm, struct ip_adjacency_t_ *adj, vlib_buffer_t *b0)
A function type for post-rewrite fixups on midchain adjacency.
Definition: lookup.h:167
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
struct ip_adjacency_t_::@164::@167 glean
IP_LOOKUP_NEXT_GLEAN.
u32 flow_hash_config_t
A flow hash configuration is a mask of the flow hash options.
Definition: lookup.h:157
u64 uword
Definition: types.h:112
u16 n_adj
Number of adjecencies in block.
Definition: lookup.h:179
u32 fib_table_id_find_fib_index(fib_protocol_t proto, u32 table_id)
Definition: lookup.c:316
unsigned short u16
Definition: types.h:57
format_function_t ** special_adjacency_format_functions
Special adjacency format functions.
Definition: lookup.h:375
unsigned char u8
Definition: types.h:56
ip_lookup_next_t lookup_next_index
Definition: lookup.h:183
u32 is_ip6
1 for ip6; 0 for ip4.
Definition: lookup.h:369
static void * mhash_key_to_mem(mhash_t *h, uword key)
Definition: mhash.h:90
A collection of combined counters.
Definition: counter.h:212
u16 saved_lookup_next_index
Highest possible perf subgraph arc interposition, e.g.
Definition: lookup.h:194
short i16
Definition: types.h:46
This packet is to be rewritten and forwarded to the next processing node.
Definition: lookup.h:82
ip_local_next_t
Definition: lookup.h:323
void ip_lookup_init(ip_lookup_main_t *lm, u32 ip_lookup_node_index)
Definition: lookup.c:165
ip4_lookup_next_t
Definition: lookup.h:96
static void * ip_interface_address_get_address(ip_lookup_main_t *lm, ip_interface_address_t *a)
Definition: lookup.h:431
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
vnet_declare_rewrite(VLIB_BUFFER_PRE_DATA_SIZE)
vlib buffer structure definition and a few select access methods.
vlib_combined_counter_main_t load_balance_counters
load-balance packet/byte counters indexed by LB index.
Definition: lookup.h:338
struct ip_adjacency_t_::@164::@166 midchain
IP_LOOKUP_NEXT_MIDCHAIN.
ip_multicast_rewrite_string_t * rewrite_strings
Definition: lookup.h:292
static ip_adjacency_t * ip_get_adjacency(ip_lookup_main_t *lm, u32 adj_index)
Definition: lookup.h:385
format_function_t * format_address_and_length
Either format_ip4_address_and_length or format_ip6_address_and_length.
Definition: lookup.h:372