ip: Replace Sematics for Interface IP addresses
[vpp.git] / src / vnet / ip / ip_interface.h
1 /*
2  * Copyright (c) 2020 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 /**
17  * @file
18  * @brief IP prefix management on interfaces
19  */
20
21 #ifndef included_ip_interface_h
22 #define included_ip_interface_h
23
24 #include <vnet/ip/lookup.h>
25
26 clib_error_t *ip_interface_address_add (ip_lookup_main_t * lm,
27                                         u32 sw_if_index,
28                                         void *address,
29                                         u32 address_length,
30                                         u32 * result_index);
31 void ip_interface_address_del (ip_lookup_main_t * lm,
32                                u32 addr_index, void *address);
33 void *ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4);
34 void ip_interface_address_mark (void);
35 void ip_interface_address_sweep (void);
36 u32 ip_interface_address_find (ip_lookup_main_t * lm,
37                                void *addr_fib, u32 address_length);
38 u8 ip_interface_has_address (u32 sw_if_index, ip46_address_t * ip, u8 is_ip4);
39
40 always_inline void *
41 ip_interface_address_get_address (ip_lookup_main_t * lm,
42                                   ip_interface_address_t * a)
43 {
44   return mhash_key_to_mem (&lm->address_to_if_address_index, a->address_key);
45 }
46
47 always_inline ip_interface_prefix_t *
48 ip_get_interface_prefix (ip_lookup_main_t * lm, ip_interface_prefix_key_t * k)
49 {
50   uword *p = mhash_get (&lm->prefix_to_if_prefix_index, k);
51   return p ? pool_elt_at_index (lm->if_prefix_pool, p[0]) : 0;
52 }
53
54 /* *INDENT-OFF* */
55 #define foreach_ip_interface_address(lm,a,sw_if_index,loop,body)        \
56 do {                                                                    \
57     vnet_main_t *_vnm = vnet_get_main();                                \
58     u32 _sw_if_index = sw_if_index;                                     \
59     vnet_sw_interface_t *_swif;                                         \
60     _swif = vnet_get_sw_interface (_vnm, _sw_if_index);                 \
61                                                                         \
62     /*                                                                  \
63      * Loop => honor unnumbered interface addressing.                   \
64      */                                                                 \
65     if (_swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)               \
66       {                                                                 \
67         if (loop)                                                       \
68           _sw_if_index = _swif->unnumbered_sw_if_index;                 \
69         else                                                            \
70           /* the interface is unnumbered, by the caller does not want   \
71            * unnumbered interfaces considered/honoured */               \
72           break;                                                        \
73       }                                                                 \
74     u32 _ia = ((vec_len((lm)->if_address_pool_index_by_sw_if_index)     \
75                 > (_sw_if_index)) ?                                     \
76                vec_elt ((lm)->if_address_pool_index_by_sw_if_index,     \
77                         (_sw_if_index)) :                               \
78                (u32)~0);                                                \
79     ip_interface_address_t * _a;                                        \
80     while (_ia != ~0)                                                   \
81     {                                                                   \
82         _a = pool_elt_at_index ((lm)->if_address_pool, _ia);            \
83         _ia = _a->next_this_sw_interface;                               \
84         (a) = _a;                                                       \
85         body;                                                           \
86     }                                                                   \
87 } while (0)
88 /* *INDENT-ON* */
89
90 #endif /* included_ip_interface_h */
91
92 /*
93  * fd.io coding-style-patch-verification: ON
94  *
95  * Local Variables:
96  * eval: (c-set-style "gnu")
97  * End:
98  */