2 * Copyright (c) 2019 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __IP_TYPES_H__
17 #define __IP_TYPES_H__
19 #include <vnet/fib/fib_types.h>
21 typedef enum ip_address_family_t_
25 } __clib_packed ip_address_family_t;
27 #define N_AF (AF_IP6+1)
29 extern uword unformat_ip_address_family (unformat_input_t * input,
31 extern u8 *format_ip_address_family (u8 * s, va_list * args);
33 #define FOR_EACH_IP_ADDRESS_FAMILY(_af) \
34 for (_af = AF_IP4; _af <= AF_IP6; _af++)
36 extern vnet_link_t ip_address_family_to_link_type (ip_address_family_t af);
37 extern fib_protocol_t ip_address_family_to_fib_proto (ip_address_family_t af);
38 extern ip_address_family_t ip_address_family_from_fib_proto (fib_protocol_t
41 #define u8_ptr_add(ptr, index) (((u8 *)ptr) + index)
42 #define u16_net_add(u, val) clib_host_to_net_u16(clib_net_to_host_u16(u) + (val))
45 typedef struct ip_address
48 ip_address_family_t version;
49 } __clib_packed ip_address_t;
52 #define IP_ADDRESS_V4_ALL_0S {.ip.ip4.as_u32 = 0, .version = AF_IP4}
53 #define IP_ADDRESS_V6_ALL_0S {.ip.ip6.as_u64 = {0, 0}, .version = AF_IP6}
54 #define ip_address_initializer IP_ADDRESS_V6_ALL_0S
56 #define ip_addr_46(_a) (_a)->ip
57 #define ip_addr_v4(_a) (_a)->ip.ip4
58 #define ip_addr_v6(_a) (_a)->ip.ip6
59 #define ip_addr_version(_a) (_a)->version
61 extern u8 *ip_addr_bytes (ip_address_t * ip);
63 extern bool ip_address_is_zero (const ip_address_t * ip);
64 extern int ip_address_cmp (const ip_address_t * ip1,
65 const ip_address_t * ip2);
66 extern void ip_address_copy (ip_address_t * dst, const ip_address_t * src);
67 extern void ip_address_copy_addr (void *dst, const ip_address_t * src);
68 extern void ip_address_set (ip_address_t * dst, const void *src, u8 version);
69 extern u16 ip_address_size (const ip_address_t * a);
70 extern u16 ip_version_to_size (ip_address_family_t af);
71 extern u8 *format_ip_address (u8 * s, va_list * args);
72 extern uword unformat_ip_address (unformat_input_t * input, va_list * args);
73 extern fib_protocol_t ip_address_to_46 (const ip_address_t * addr,
75 extern void ip_address_from_46 (const ip46_address_t * a,
76 fib_protocol_t fproto, ip_address_t * addr);
77 extern void ip_address_increment (ip_address_t * ip);
78 extern void ip_address_reset (ip_address_t * ip);
81 typedef struct ip_prefix
85 } __clib_packed ip_prefix_t;
88 #define ip_prefix_addr(_a) (_a)->addr
89 #define ip_prefix_version(_a) ip_addr_version(&ip_prefix_addr(_a))
90 #define ip_prefix_len(_a) (_a)->len
91 #define ip_prefix_v4(_a) ip_addr_v4(&ip_prefix_addr(_a))
92 #define ip_prefix_v6(_a) ip_addr_v6(&ip_prefix_addr(_a))
94 extern int ip_prefix_cmp (ip_prefix_t * p1, ip_prefix_t * p2);
95 extern void ip_prefix_normalize (ip_prefix_t * a);
97 extern void ip_address_to_fib_prefix (const ip_address_t * addr,
98 fib_prefix_t * prefix);
99 extern void ip_prefix_to_fib_prefix (const ip_prefix_t * ipp,
100 fib_prefix_t * fibp);
101 extern u8 *format_ip_prefix (u8 * s, va_list * args);
102 extern uword unformat_ip_prefix (unformat_input_t * input, va_list * args);
104 extern bool ip_prefix_validate (const ip_prefix_t * ip);
105 extern void ip4_address_normalize (ip4_address_t * ip4, u8 preflen);
106 extern void ip6_address_normalize (ip6_address_t * ip6, u8 preflen);
107 extern void ip4_preflen_to_mask (u8 pref_len, ip4_address_t * ip);
108 extern u32 ip4_mask_to_preflen (ip4_address_t * mask);
109 extern void ip4_prefix_max_address_host_order (ip4_address_t * ip, u8 plen,
110 ip4_address_t * res);
111 extern void ip6_prefix_max_address_host_order (ip6_address_t * ip, u8 plen,
112 ip6_address_t * res);
113 extern void ip6_preflen_to_mask (u8 pref_len, ip6_address_t * mask);
114 extern u32 ip6_mask_to_preflen (ip6_address_t * mask);
116 #endif /* __IP_TYPES_H__ */
119 * fd.io coding-style-patch-verification: ON
122 * eval: (c-set-style "gnu")