Custom adjacency formatting fix (Revived)
[vpp.git] / vnet / 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 /** @file Definitions for all things IP (v4|v6) unicast and multicast lookup related.
41     - Adjacency definitions and registration
42     - callbacks on route add
43     - callbacks on interface address change
44  */
45 #ifndef included_ip_lookup_h
46 #define included_ip_lookup_h
47
48 #include <vnet/vnet.h>
49 #include <vlib/buffer.h>
50 #include <vnet/ip/ip4_packet.h>
51 #include <vnet/ip/ip6_packet.h>
52
53 /** @brief Common (IP4/IP6) next index stored in adjacency. */
54 typedef enum {
55   /** Packet does not match any route in table. */
56   IP_LOOKUP_NEXT_MISS,
57
58   /** Adjacency to drop this packet. */
59   IP_LOOKUP_NEXT_DROP,
60   /** Adjacency to punt this packet. */
61   IP_LOOKUP_NEXT_PUNT,
62
63   /** This packet is for one of our own IP addresses. */
64   IP_LOOKUP_NEXT_LOCAL,
65
66   /** This packet matches an "interface route" and packets
67      need to be passed to ARP to find rewrite string for
68      this destination. */
69   IP_LOOKUP_NEXT_ARP,
70
71   /** This packet is to be rewritten and forwarded to the next
72      processing node.  This is typically the output interface but
73      might be another node for further output processing. */
74   IP_LOOKUP_NEXT_REWRITE,
75
76   /** This packet needs to be classified */
77   IP_LOOKUP_NEXT_CLASSIFY,
78
79   /** This packet needs to go to MAP - RFC7596, RFC7597 */
80   IP_LOOKUP_NEXT_MAP,
81
82   /** This packet needs to go to MAP with Translation - RFC7599 */
83   IP_LOOKUP_NEXT_MAP_T,
84
85   /** This packets needs to go to indirect next hop */
86   IP_LOOKUP_NEXT_INDIRECT,
87
88   /** This packets needs to go to ICMP error */
89   IP_LOOKUP_NEXT_ICMP_ERROR,
90
91   IP_LOOKUP_N_NEXT,
92 } ip_lookup_next_t;
93
94 typedef enum {
95   IP4_LOOKUP_N_NEXT = IP_LOOKUP_N_NEXT,
96 } ip4_lookup_next_t;
97
98 typedef enum {
99   /** Hop-by-hop header handling */
100   IP6_LOOKUP_NEXT_HOP_BY_HOP = IP_LOOKUP_N_NEXT,
101   IP6_LOOKUP_NEXT_ADD_HOP_BY_HOP,
102   IP6_LOOKUP_NEXT_POP_HOP_BY_HOP,
103   IP6_LOOKUP_N_NEXT,
104 } ip6_lookup_next_t;
105
106 #define IP4_LOOKUP_NEXT_NODES {                                 \
107     [IP_LOOKUP_NEXT_MISS] = "ip4-miss",                         \
108     [IP_LOOKUP_NEXT_DROP] = "ip4-drop",                         \
109     [IP_LOOKUP_NEXT_PUNT] = "ip4-punt",                         \
110     [IP_LOOKUP_NEXT_LOCAL] = "ip4-local",                       \
111     [IP_LOOKUP_NEXT_ARP] = "ip4-arp",                           \
112     [IP_LOOKUP_NEXT_REWRITE] = "ip4-rewrite-transit",           \
113     [IP_LOOKUP_NEXT_CLASSIFY] = "ip4-classify",                 \
114     [IP_LOOKUP_NEXT_MAP] = "ip4-map",                           \
115     [IP_LOOKUP_NEXT_MAP_T] = "ip4-map-t",                       \
116     [IP_LOOKUP_NEXT_INDIRECT] = "ip4-indirect",                 \
117     [IP_LOOKUP_NEXT_ICMP_ERROR] = "ip4-icmp-error",             \
118 }
119
120 #define IP6_LOOKUP_NEXT_NODES {                                 \
121     [IP_LOOKUP_NEXT_MISS] = "ip6-miss",                         \
122     [IP_LOOKUP_NEXT_DROP] = "ip6-drop",                         \
123     [IP_LOOKUP_NEXT_PUNT] = "ip6-punt",                         \
124     [IP_LOOKUP_NEXT_LOCAL] = "ip6-local",                       \
125     [IP_LOOKUP_NEXT_ARP] = "ip6-discover-neighbor",             \
126     [IP_LOOKUP_NEXT_REWRITE] = "ip6-rewrite",                   \
127     [IP_LOOKUP_NEXT_CLASSIFY] = "ip6-classify",                 \
128     [IP_LOOKUP_NEXT_MAP] = "ip6-map",                           \
129     [IP_LOOKUP_NEXT_MAP_T] = "ip6-map-t",                       \
130     [IP_LOOKUP_NEXT_INDIRECT] = "ip6-indirect",                 \
131     [IP_LOOKUP_NEXT_ICMP_ERROR] = "ip6-icmp-error",             \
132     [IP6_LOOKUP_NEXT_HOP_BY_HOP] = "ip6-hop-by-hop",            \
133     [IP6_LOOKUP_NEXT_ADD_HOP_BY_HOP] = "ip6-add-hop-by-hop",    \
134     [IP6_LOOKUP_NEXT_POP_HOP_BY_HOP] = "ip6-pop-hop-by-hop",    \
135 }
136
137 /** Flow hash configuration */
138 #define IP_FLOW_HASH_SRC_ADDR (1<<0)
139 #define IP_FLOW_HASH_DST_ADDR (1<<1)
140 #define IP_FLOW_HASH_PROTO (1<<2)
141 #define IP_FLOW_HASH_SRC_PORT (1<<3)
142 #define IP_FLOW_HASH_DST_PORT (1<<4)
143 #define IP_FLOW_HASH_REVERSE_SRC_DST (1<<5)
144
145 /** Default: 5-tuple without the "reverse" bit */
146 #define IP_FLOW_HASH_DEFAULT (0x1F)
147
148 #define foreach_flow_hash_bit                   \
149 _(src, IP_FLOW_HASH_SRC_ADDR)                   \
150 _(dst, IP_FLOW_HASH_DST_ADDR)                   \
151 _(sport, IP_FLOW_HASH_SRC_PORT)                 \
152 _(dport, IP_FLOW_HASH_DST_PORT)                 \
153 _(proto, IP_FLOW_HASH_PROTO)                    \
154 _(reverse, IP_FLOW_HASH_REVERSE_SRC_DST)
155
156 #define IP_ADJACENCY_OPAQUE_SZ 16
157 /** @brief IP unicast adjacency.
158     @note cache aligned.
159 */
160 typedef struct {
161   CLIB_CACHE_LINE_ALIGN_MARK(cacheline0);
162   /** Handle for this adjacency in adjacency heap. */
163   u32 heap_handle;
164
165   STRUCT_MARK(signature_start);
166
167   /** Interface address index for this local/arp adjacency. */
168   u32 if_address_index;
169
170   /** Number of adjecencies in block.  Greater than 1 means multipath;
171      otherwise equal to 1. */
172   u16 n_adj;
173
174   /** Next hop after ip4-lookup. */
175   union {
176     ip_lookup_next_t lookup_next_index : 16;
177     u16 lookup_next_index_as_int;
178   };
179
180   /** Force re-lookup in a different FIB. ~0 => normal behavior */
181   i16 explicit_fib_index;
182   u16 mcast_group_index;  
183
184   /** Highest possible perf subgraph arc interposition, e.g. for ip6 ioam */
185   u16 saved_lookup_next_index;
186
187   union {
188     /** IP_LOOKUP_NEXT_ARP only */
189     struct {
190       ip46_address_t next_hop;
191     } arp;
192     /** IP_LOOKUP_NEXT_CLASSIFY only */
193     struct {
194       u16 table_index;
195     } classify;
196     /** IP_LOOKUP_NEXT_INDIRECT only */
197     struct {
198         ip46_address_t next_hop;
199     } indirect;
200     u8 opaque[IP_ADJACENCY_OPAQUE_SZ];
201   };
202
203   /** @brief Special format function for this adjacency.
204    * Specifically good for cases which use the entire rewrite
205    * for their own purposes. Can easily reduce to a u16 or a u8 if/when
206    * the first cache line reads "full" on the free space gas gauge.
207    */
208   u32 special_adjacency_format_function_index;  /* 0 is invalid */
209   STRUCT_MARK(signature_end);
210
211   /** Number of FIB entries sharing this adjacency */
212   u32 share_count;
213   /** Use this adjacency instead */
214   u32 next_adj_with_signature;
215
216   CLIB_CACHE_LINE_ALIGN_MARK(cacheline1);
217
218   /** Rewrite in second/third cache lines */
219   vnet_declare_rewrite (VLIB_BUFFER_PRE_DATA_SIZE);
220 } ip_adjacency_t;
221
222 static inline uword
223 vnet_ip_adjacency_signature (ip_adjacency_t * adj)
224 {
225   uword signature = 0xfeedfaceULL;
226
227   /* Skip heap handle, sum everything up to but not including share_count */
228   signature = hash_memory
229       (STRUCT_MARK_PTR(adj, signature_start),
230        STRUCT_OFFSET_OF(ip_adjacency_t, signature_end)
231        - STRUCT_OFFSET_OF(ip_adjacency_t, signature_start),
232        signature);
233
234   /* and the rewrite */
235   signature = hash_memory (&adj->rewrite_header, VLIB_BUFFER_PRE_DATA_SIZE,
236                              signature);
237   return signature;
238 }
239
240 static inline int
241 vnet_ip_adjacency_share_compare (ip_adjacency_t * a1, ip_adjacency_t *a2)
242 {
243   if (memcmp (STRUCT_MARK_PTR(a1, signature_start),
244               STRUCT_MARK_PTR(a2, signature_start),
245               STRUCT_OFFSET_OF(ip_adjacency_t, signature_end)
246               - STRUCT_OFFSET_OF(ip_adjacency_t, signature_start)))
247     return 0;
248   if (memcmp (&a1->rewrite_header, &a2->rewrite_header,
249               VLIB_BUFFER_PRE_DATA_SIZE))
250     return 0;
251   return 1;
252 }
253
254 /* Index into adjacency table. */
255 typedef u32 ip_adjacency_index_t;
256
257 typedef struct {
258   /* Directly connected next-hop adjacency index. */
259   u32 next_hop_adj_index;
260
261   /* Path weight for this adjacency. */
262   u32 weight;
263 } ip_multipath_next_hop_t;
264
265 typedef struct {
266   /* Adjacency index of first index in block. */
267   u32 adj_index;
268   
269   /* Power of 2 size of adjacency block. */
270   u32 n_adj_in_block;
271
272   /* Number of prefixes that point to this adjacency. */
273   u32 reference_count;
274
275   /* Normalized next hops are used as hash keys: they are sorted by weight
276      and weights are chosen so they add up to 1 << log2_n_adj_in_block (with
277      zero-weighted next hops being deleted).
278      Unnormalized next hops are saved so that control plane has a record of exactly
279      what the RIB told it. */
280   struct {
281     /* Number of hops in the multipath. */
282     u32 count;
283
284     /* Offset into next hop heap for this block. */
285     u32 heap_offset;
286
287     /* Heap handle used to for example free block when we're done with it. */
288     u32 heap_handle;
289   } normalized_next_hops, unnormalized_next_hops;
290 } ip_multipath_adjacency_t;
291
292 /* IP multicast adjacency. */
293 typedef struct {
294   /* Handle for this adjacency in adjacency heap. */
295   u32 heap_handle;
296
297   /* Number of adjecencies in block. */
298   u32 n_adj;
299
300   /* Rewrite string. */
301   vnet_declare_rewrite (64 - 2*sizeof(u32));
302 } ip_multicast_rewrite_t;
303
304 typedef struct {
305   /* ip4-multicast-rewrite next index. */
306   u32 next_index;
307
308   u8 n_rewrite_bytes;
309
310   u8 rewrite_string[64 - 1*sizeof(u32) - 1*sizeof(u8)];
311 } ip_multicast_rewrite_string_t;
312
313 typedef struct {
314   ip_multicast_rewrite_t * rewrite_heap;
315
316   ip_multicast_rewrite_string_t * rewrite_strings;
317
318   /* Negative rewrite string index; >= 0 sw_if_index.
319      Sorted.  Used to hash. */
320   i32 ** adjacency_id_vector;
321
322   uword * adjacency_by_id_vector;
323 } ip_multicast_lookup_main_t;
324
325 typedef struct {
326   /* Key for mhash; in fact, just a byte offset into mhash key vector. */
327   u32 address_key;
328
329   /* Interface which has this address. */
330   u32 sw_if_index;
331
332   /* Adjacency for neighbor probe (ARP) for this interface address. */
333   u32 neighbor_probe_adj_index;
334
335   /* Address (prefix) length for this interface. */
336   u16 address_length;
337
338   /* Will be used for something eventually.  Primary vs. secondary? */
339   u16 flags;
340
341   /* Next and previous pointers for doubly linked list of
342      addresses per software interface. */
343   u32 next_this_sw_interface;
344   u32 prev_this_sw_interface;
345 } ip_interface_address_t;
346
347 typedef enum {
348   IP_LOCAL_NEXT_DROP,
349   IP_LOCAL_NEXT_PUNT,
350   IP_LOCAL_NEXT_UDP_LOOKUP,
351   IP_LOCAL_NEXT_ICMP,
352   IP_LOCAL_N_NEXT,
353 } ip_local_next_t;
354
355 struct ip_lookup_main_t;
356
357 typedef void (* ip_add_del_adjacency_callback_t) (struct ip_lookup_main_t * lm,
358                                                   u32 adj_index,
359                                                   ip_adjacency_t * adj,
360                                                   u32 is_del);
361
362 typedef struct {
363   vnet_config_main_t config_main;
364
365   u32 * config_index_by_sw_if_index;
366 } ip_config_main_t;
367
368 /**
369  * This structure is used to dynamically register a custom adjacency
370  * for ip lookup.
371  * Typically used with
372  *  VNET_IP4_REGISTER_ADJACENCY or
373  *  VNET_IP6_REGISTER_ADJACENCY macros.
374  */
375 typedef struct ip_adj_register_struct {
376   /** Name of the node for this registered adjacency. */
377   char *node_name;
378
379   /** Formatting function for the adjacency.
380    * Variadic arguments given to the function are:
381    * - struct ip_lookup_main_t *
382    * - ip_adjacency_t *adj
383    */
384   format_function_t *fn;
385
386   /**
387    * When the adjacency is registered, the ip-lookup next index will
388    * be written where this pointer points.
389    */
390   u32 *next_index;
391
392   struct ip_adj_register_struct *next;
393 } ip_adj_register_t;
394
395 typedef struct ip_lookup_main_t {
396   /** Adjacency heap. */
397   ip_adjacency_t * adjacency_heap;
398
399   /** Adjacency packet/byte counters indexed by adjacency index. */
400   vlib_combined_counter_main_t adjacency_counters;
401
402   /** Heap of (next hop, weight) blocks.  Sorted by next hop. */
403   ip_multipath_next_hop_t * next_hop_heap;
404
405   /** Indexed by heap_handle from ip_adjacency_t. */
406   ip_multipath_adjacency_t * multipath_adjacencies;
407
408   /** Adjacency by signature hash */
409   uword * adj_index_by_signature;
410
411   /** Temporary vectors for looking up next hops in hash. */
412   ip_multipath_next_hop_t * next_hop_hash_lookup_key;
413   ip_multipath_next_hop_t * next_hop_hash_lookup_key_normalized;
414
415   /** Hash table mapping normalized next hops and weights
416      to multipath adjacency index. */
417   uword * multipath_adjacency_by_next_hops;
418
419   u32 * adjacency_remap_table;
420   u32 n_adjacency_remaps;
421
422   /** If average error per adjacency is less than this threshold adjacency block
423      size is accepted. */
424   f64 multipath_next_hop_error_tolerance;
425
426   /** Adjacency index for routing table misses, local punts, and drops. */
427   u32 miss_adj_index, drop_adj_index, local_adj_index;
428
429   /** Miss adjacency is always first in adjacency table. */
430 #define IP_LOOKUP_MISS_ADJ_INDEX 0
431
432   ip_add_del_adjacency_callback_t * add_del_adjacency_callbacks;
433
434   /** Pool of addresses that are assigned to interfaces. */
435   ip_interface_address_t * if_address_pool;
436
437   /** Hash table mapping address to index in interface address pool. */
438   mhash_t address_to_if_address_index;
439
440   /** Head of doubly linked list of interface addresses for each software interface.
441      ~0 means this interface has no address. */
442   u32 * if_address_pool_index_by_sw_if_index;
443
444   /** First table index to use for this interface, ~0 => none */
445   u32 * classify_table_index_by_sw_if_index;
446
447   /** rx/tx interface/feature configuration. */
448   ip_config_main_t rx_config_mains[VNET_N_CAST], tx_config_main;
449
450   /** Number of bytes in a fib result.  Must be at least
451      sizeof (uword).  First word is always adjacency index. */
452   u32 fib_result_n_bytes, fib_result_n_words;
453
454   format_function_t * format_fib_result;
455
456   /** 1 for ip6; 0 for ip4. */
457   u32 is_ip6;
458
459   /** Either format_ip4_address_and_length or format_ip6_address_and_length. */
460   format_function_t * format_address_and_length;
461
462   /** Special adjacency format functions */
463   format_function_t ** special_adjacency_format_functions;
464
465   /** Table mapping ip protocol to ip[46]-local node next index. */
466   u8 local_next_by_ip_protocol[256];
467
468   /** IP_BUILTIN_PROTOCOL_{TCP,UDP,ICMP,OTHER} by protocol in IP header. */
469   u8 builtin_protocol_by_ip_protocol[256];
470
471   /** Registered adjacencies */
472   ip_adj_register_t *registered_adjacencies;
473 } ip_lookup_main_t;
474
475 always_inline ip_adjacency_t *
476 ip_get_adjacency (ip_lookup_main_t * lm,
477                   u32 adj_index)
478 {
479   ip_adjacency_t * adj;
480
481   adj = vec_elt_at_index (lm->adjacency_heap, adj_index);
482
483   ASSERT (adj->heap_handle != ~0);
484
485   return adj;
486 }
487
488 #define ip_prefetch_adjacency(lm,adj_index,type)                \
489 do {                                                            \
490   ip_adjacency_t * _adj = (lm)->adjacency_heap + (adj_index);   \
491   CLIB_PREFETCH (_adj, sizeof (_adj[0]), type);                 \
492 } while (0)
493
494 /* Adds a next node to ip4 or ip6 lookup node which can be then used in adjacencies.
495  * @param vlib_main pointer
496  * @param lm ip4_main.lookup_main or ip6_main.lookup_main
497  * @param reg registration structure
498  * @param next_node_index Returned index to be used in adjacencies.
499  * @return 0 on success. -1 on failure.
500  */
501 int ip_register_adjacency(vlib_main_t *vm, u8 is_ip4,
502                           ip_adj_register_t *reg);
503
504 /*
505  * Construction helpers to add IP adjacency at init.
506  */
507 #define VNET_IP_REGISTER_ADJACENCY(ip,x,...)                     \
508   __VA_ARGS__ ip_adj_register_t ip##adj_##x;                     \
509 static void __vnet_##ip##_register_adjacency_##x (void)          \
510   __attribute__((__constructor__)) ;                             \
511 static void __vnet_##ip##_register_adjacency_##x (void)          \
512 {                                                                \
513   ip_lookup_main_t *lm = &ip##_main.lookup_main;                 \
514   ip##adj_##x.next = lm->registered_adjacencies;                 \
515   lm->registered_adjacencies = &ip##adj_##x;                     \
516 }                                                                \
517 __VA_ARGS__ ip_adj_register_t ip##adj_##x
518
519 #define VNET_IP4_REGISTER_ADJACENCY(x,...)                       \
520     VNET_IP_REGISTER_ADJACENCY(ip4, x, __VA_ARGS__)
521
522 #define VNET_IP6_REGISTER_ADJACENCY(x,...)                       \
523     VNET_IP_REGISTER_ADJACENCY(ip6, x, __VA_ARGS__)
524
525 static inline void
526 ip_register_add_del_adjacency_callback(ip_lookup_main_t * lm,
527                                        ip_add_del_adjacency_callback_t cb)
528 {
529   vec_add1(lm->add_del_adjacency_callbacks, cb);
530 }
531
532 always_inline void
533 ip_call_add_del_adjacency_callbacks (ip_lookup_main_t * lm, u32 adj_index, u32 is_del)
534 {
535   ip_adjacency_t * adj;
536   uword i;
537   adj = ip_get_adjacency (lm, adj_index);
538   for (i = 0; i < vec_len (lm->add_del_adjacency_callbacks); i++)
539     lm->add_del_adjacency_callbacks[i] (lm, adj_index, adj, is_del);
540 }
541
542 /* Create new block of given number of contiguous adjacencies. */
543 ip_adjacency_t *
544 ip_add_adjacency (ip_lookup_main_t * lm,
545                   ip_adjacency_t * adj,
546                   u32 n_adj,
547                   u32 * adj_index_result);
548
549 void ip_del_adjacency (ip_lookup_main_t * lm, u32 adj_index);
550 void
551 ip_update_adjacency (ip_lookup_main_t * lm,
552                      u32 adj_index,
553                      ip_adjacency_t * copy_adj);
554
555 static inline int
556 ip_adjacency_is_multipath(ip_lookup_main_t * lm, u32 adj_index)
557 {
558   if (!vec_len(lm->multipath_adjacencies))
559     return 0;
560
561   if (vec_len(lm->multipath_adjacencies) < adj_index - 1)
562     return 0;
563
564
565   return (lm->multipath_adjacencies[adj_index].adj_index == adj_index &&
566           lm->multipath_adjacencies[adj_index].n_adj_in_block > 0);
567 }
568
569 void
570 ip_multipath_adjacency_free (ip_lookup_main_t * lm,
571                              ip_multipath_adjacency_t * a);
572
573 u32
574 ip_multipath_adjacency_add_del_next_hop (ip_lookup_main_t * lm,
575                                          u32 is_del,
576                                          u32 old_mp_adj_index,
577                                          u32 next_hop_adj_index,
578                                          u32 next_hop_weight,
579                                          u32 * new_mp_adj_index);
580
581 clib_error_t *
582 ip_interface_address_add_del (ip_lookup_main_t * lm,
583                               u32 sw_if_index,
584                               void * address,
585                               u32 address_length,
586                               u32 is_del,
587                               u32 * result_index);
588
589 always_inline ip_interface_address_t *
590 ip_get_interface_address (ip_lookup_main_t * lm, void * addr_fib)
591 {
592   uword * p = mhash_get (&lm->address_to_if_address_index, addr_fib);
593   return p ? pool_elt_at_index (lm->if_address_pool, p[0]) : 0;
594 }
595
596 always_inline void *
597 ip_interface_address_get_address (ip_lookup_main_t * lm, ip_interface_address_t * a)
598 { return mhash_key_to_mem (&lm->address_to_if_address_index, a->address_key); }
599
600 always_inline ip_interface_address_t *
601 ip_interface_address_for_packet (ip_lookup_main_t * lm, vlib_buffer_t * b, u32 sw_if_index)
602 {
603   ip_adjacency_t * adj;
604   u32 if_address_index;
605
606   adj = ip_get_adjacency (lm, vnet_buffer (b)->ip.adj_index[VLIB_TX]);
607
608   ASSERT (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP
609           || adj->lookup_next_index == IP_LOOKUP_NEXT_LOCAL);
610   if_address_index = adj->if_address_index;
611   if_address_index = (if_address_index == ~0 ?
612                       vec_elt (lm->if_address_pool_index_by_sw_if_index, sw_if_index)
613                       : if_address_index);
614
615   return (if_address_index != ~0)?pool_elt_at_index (lm->if_address_pool, if_address_index):NULL;
616 }
617
618 #define foreach_ip_interface_address(lm,a,sw_if_index,loop,body)        \
619 do {                                                                    \
620     vnet_main_t *_vnm = vnet_get_main();                                     \
621     u32 _sw_if_index = sw_if_index;                                     \
622     vnet_sw_interface_t *_swif;                                         \
623     _swif = vnet_get_sw_interface (_vnm, _sw_if_index);                 \
624                                                                         \
625     /*                                                                  \
626      * Loop => honor unnumbered interface addressing.                   \
627      */                                                                 \
628     if (loop && _swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)       \
629       _sw_if_index = _swif->unnumbered_sw_if_index;                     \
630     u32 _ia =                                                           \
631       (vec_len((lm)->if_address_pool_index_by_sw_if_index)              \
632        > (_sw_if_index))                                                \
633         ? vec_elt ((lm)->if_address_pool_index_by_sw_if_index,          \
634                    (_sw_if_index)) : (u32)~0;                           \
635     ip_interface_address_t * _a;                                        \
636     while (_ia != ~0)                                                   \
637     {                                                                   \
638         _a = pool_elt_at_index ((lm)->if_address_pool, _ia);            \
639         _ia = _a->next_this_sw_interface;                               \
640         (a) = _a;                                                       \
641         body;                                                           \
642     }                                                                   \
643 } while (0)
644
645 void ip_lookup_init (ip_lookup_main_t * lm, u32 ip_lookup_node_index);
646 u32 vnet_register_special_adjacency_format_function 
647 (ip_lookup_main_t * lm, format_function_t * fp);
648
649 #endif /* included_ip_lookup_h */