Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-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: |
| 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 | #ifndef included_ip46_address_h |
| 17 | #define included_ip46_address_h |
| 18 | |
| 19 | #include <vnet/ip/ip6_packet.h> |
| 20 | #include <vnet/ip/ip4_packet.h> |
| 21 | |
| 22 | typedef enum |
| 23 | { |
| 24 | IP46_TYPE_ANY, |
| 25 | IP46_TYPE_BOTH = IP46_TYPE_ANY, |
| 26 | IP46_TYPE_IP4, |
| 27 | IP46_TYPE_IP6 |
| 28 | } ip46_type_t; |
| 29 | |
| 30 | #define IP46_N_TYPES (IP46_TYPE_IP6+2) |
| 31 | |
| 32 | #define FOREACH_IP46_TYPE(_type) \ |
| 33 | for (_type = IP46_TYPE_IP4; _type <= IP46_TYPE_IP6; _type++) |
| 34 | |
| 35 | extern u8 *format_ip46_type (u8 * s, va_list * args); |
| 36 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 37 | typedef CLIB_PACKED (union ip46_address_t_ { |
| 38 | struct { |
| 39 | u32 pad[3]; |
| 40 | ip4_address_t ip4; |
| 41 | }; |
| 42 | ip6_address_t ip6; |
| 43 | u8 as_u8[16]; |
| 44 | u64 as_u64[2]; |
| 45 | }) ip46_address_t; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | format_function_t format_ip46_address; |
| 49 | |
| 50 | #define ip46_address_initializer {{{ 0 }}} |
| 51 | |
| 52 | always_inline u8 |
| 53 | ip46_address_is_ip4 (const ip46_address_t * ip46) |
| 54 | { |
| 55 | return (((ip46)->pad[0] | (ip46)->pad[1] | (ip46)->pad[2]) == 0); |
| 56 | } |
| 57 | |
| 58 | always_inline void |
| 59 | ip46_address_mask_ip4 (ip46_address_t * ip46) |
| 60 | { |
| 61 | ((ip46)->pad[0] = (ip46)->pad[1] = (ip46)->pad[2] = 0); |
| 62 | } |
| 63 | |
| 64 | always_inline void |
| 65 | ip46_address_set_ip4 (ip46_address_t * ip46, const ip4_address_t * ip) |
| 66 | { |
| 67 | ip46_address_mask_ip4 (ip46); |
| 68 | ip46->ip4 = *ip; |
| 69 | } |
| 70 | |
| 71 | always_inline void |
| 72 | ip46_address_reset (ip46_address_t * ip46) |
| 73 | { |
| 74 | ip46->as_u64[0] = ip46->as_u64[1] = 0; |
| 75 | } |
| 76 | |
| 77 | always_inline int |
| 78 | ip46_address_cmp (const ip46_address_t * ip46_1, |
| 79 | const ip46_address_t * ip46_2) |
| 80 | { |
| 81 | return (memcmp (ip46_1, ip46_2, sizeof (*ip46_1))); |
| 82 | } |
| 83 | |
| 84 | always_inline u8 |
| 85 | ip46_address_is_zero (const ip46_address_t * ip46) |
| 86 | { |
| 87 | return (ip46->as_u64[0] == 0 && ip46->as_u64[1] == 0); |
| 88 | } |
| 89 | |
| 90 | always_inline u8 |
| 91 | ip46_address_is_equal (const ip46_address_t * ip46_1, |
| 92 | const ip46_address_t * ip46_2) |
| 93 | { |
| 94 | return ((ip46_1->as_u64[0] == ip46_2->as_u64[0]) && |
| 95 | (ip46_1->as_u64[1] == ip46_2->as_u64[1])); |
| 96 | } |
| 97 | |
| 98 | static_always_inline int |
| 99 | ip4_address_is_equal (const ip4_address_t * ip4_1, |
| 100 | const ip4_address_t * ip4_2) |
| 101 | { |
| 102 | return (ip4_1->as_u32 == ip4_2->as_u32); |
| 103 | } |
| 104 | |
| 105 | static_always_inline int |
| 106 | ip46_address_is_equal_v4 (const ip46_address_t * ip46, |
| 107 | const ip4_address_t * ip4) |
| 108 | { |
| 109 | return (ip46->ip4.as_u32 == ip4->as_u32); |
| 110 | } |
| 111 | |
| 112 | static_always_inline int |
| 113 | ip46_address_is_equal_v6 (const ip46_address_t * ip46, |
| 114 | const ip6_address_t * ip6) |
| 115 | { |
| 116 | return ((ip46->ip6.as_u64[0] == ip6->as_u64[0]) && |
| 117 | (ip46->ip6.as_u64[1] == ip6->as_u64[1])); |
| 118 | } |
| 119 | |
| 120 | static_always_inline void |
| 121 | ip46_address_copy (ip46_address_t * dst, const ip46_address_t * src) |
| 122 | { |
| 123 | dst->as_u64[0] = src->as_u64[0]; |
| 124 | dst->as_u64[1] = src->as_u64[1]; |
| 125 | } |
| 126 | |
| 127 | static_always_inline void |
| 128 | ip46_address_set_ip6 (ip46_address_t * dst, const ip6_address_t * src) |
| 129 | { |
| 130 | dst->as_u64[0] = src->as_u64[0]; |
| 131 | dst->as_u64[1] = src->as_u64[1]; |
| 132 | } |
| 133 | |
| 134 | always_inline ip46_address_t |
| 135 | to_ip46 (u32 is_ipv6, u8 * buf) |
| 136 | { |
| 137 | ip46_address_t ip; |
| 138 | if (is_ipv6) |
| 139 | ip.ip6 = *((ip6_address_t *) buf); |
| 140 | else |
| 141 | ip46_address_set_ip4 (&ip, (ip4_address_t *) buf); |
| 142 | return ip; |
| 143 | } |
| 144 | |
| 145 | always_inline ip46_type_t |
| 146 | ip46_address_get_type (const ip46_address_t * ip) |
| 147 | { |
| 148 | return (ip46_address_is_ip4 (ip) ? IP46_TYPE_IP4 : IP46_TYPE_IP6); |
| 149 | } |
| 150 | |
| 151 | always_inline uword |
| 152 | ip46_address_is_multicast (const ip46_address_t * a) |
| 153 | { |
| 154 | return ip46_address_is_ip4 (a) ? ip4_address_is_multicast (&a->ip4) : |
| 155 | ip6_address_is_multicast (&a->ip6); |
| 156 | } |
| 157 | |
| 158 | extern void ip4_address_increment (ip4_address_t * i); |
| 159 | extern void ip6_address_increment (ip6_address_t * i); |
| 160 | extern void ip46_address_increment (ip46_type_t type, ip46_address_t * ip); |
| 161 | |
| 162 | #endif /* included_ip46_address_h */ |
| 163 | |
| 164 | /* |
| 165 | * fd.io coding-style-patch-verification: ON |
| 166 | * |
| 167 | * Local Variables: |
| 168 | * eval: (c-set-style "gnu") |
| 169 | * End: |
| 170 | */ |