2 * Copyright (c) 2017 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 #include <vnet/ip/ip.h>
17 #include <vnet/fib/fib_table.h>
19 u32 ip_flow_hash_router_id;
22 ip_address_family_to_ether_type (ip_address_family_t af)
27 return (ETHERNET_TYPE_IP4);
29 return (ETHERNET_TYPE_IP6);
32 return (ETHERNET_TYPE_IP4);
36 ip_is_zero (ip46_address_t * ip46_address, u8 is_ip4)
39 return (ip46_address->ip4.as_u32 == 0);
41 return (ip46_address->as_u64[0] == 0 && ip46_address->as_u64[1] == 0);
45 ip_is_local_host (ip46_address_t * ip46_address, u8 is_ip4)
48 return (ip46_address->ip4.as_u8[0] == 127);
50 return (ip46_address->as_u64[0] == 0 &&
51 clib_net_to_host_u64 (ip46_address->as_u64[1]) == 1);
55 ip4_is_local_host (ip4_address_t * ip4_address)
57 return (ip4_address->as_u8[0] == 127);
61 ip6_is_local_host (ip6_address_t * ip6_address)
63 return (ip6_address->as_u64[0] == 0 &&
64 clib_net_to_host_u64 (ip6_address->as_u64[1]) == 1);
68 * Checks that an ip is local to the requested fib
71 ip_is_local (u32 fib_index, ip46_address_t * ip46_address, u8 is_ip4)
74 fib_entry_flag_t flags;
77 /* Check if requester is local */
81 prefix.fp_proto = FIB_PROTOCOL_IP4;
86 prefix.fp_proto = FIB_PROTOCOL_IP6;
89 clib_memcpy_fast (&prefix.fp_addr, ip46_address, sizeof (ip46_address_t));
90 fei = fib_table_lookup (fib_index, &prefix);
91 flags = fib_entry_get_flags (fei);
93 return (flags & FIB_ENTRY_FLAG_LOCAL);
97 ip_copy (ip46_address_t * dst, ip46_address_t * src, u8 is_ip4)
101 ip46_address_mask_ip4 (dst);
102 dst->ip4.as_u32 = src->ip4.as_u32;
105 clib_memcpy_fast (&dst->ip6, &src->ip6, sizeof (ip6_address_t));
109 ip_set (ip46_address_t * dst, void *src, u8 is_ip4)
113 ip46_address_mask_ip4 (dst);
114 dst->ip4.as_u32 = ((ip4_address_t *) src)->as_u32;
117 clib_memcpy_fast (&dst->ip6, (ip6_address_t *) src,
118 sizeof (ip6_address_t));
121 static const char *ip_arc_names[N_IP_FEATURE_LOCATIONS][N_AF][N_SAFI] = {
122 [IP_FEATURE_INPUT] = {
124 [SAFI_UNICAST] = "ip4-unicast",
125 [SAFI_MULTICAST] = "ip4-multicast",
128 [SAFI_UNICAST] = "ip6-unicast",
129 [SAFI_MULTICAST] = "ip6-multicast",
132 [IP_FEATURE_OUTPUT] = {
134 [SAFI_UNICAST] = "ip4-output",
135 [SAFI_MULTICAST] = "ip4-output",
138 [SAFI_UNICAST] = "ip6-output",
139 [SAFI_MULTICAST] = "ip6-output",
142 [IP_FEATURE_LOCAL] = {
144 [SAFI_UNICAST] = "ip4-local",
145 [SAFI_MULTICAST] = "ip4-local",
148 [SAFI_UNICAST] = "ip6-local",
149 [SAFI_MULTICAST] = "ip6-local",
152 [IP_FEATURE_PUNT] = {
154 [SAFI_UNICAST] = "ip4-punt",
155 [SAFI_MULTICAST] = "ip4-punt",
158 [SAFI_UNICAST] = "ip6-punt",
159 [SAFI_MULTICAST] = "ip6-punt",
162 [IP_FEATURE_DROP] = {
164 [SAFI_UNICAST] = "ip4-drop",
165 [SAFI_MULTICAST] = "ip4-drop",
168 [SAFI_UNICAST] = "ip6-drop",
169 [SAFI_MULTICAST] = "ip6-drop",
175 ip_feature_enable_disable (ip_address_family_t af,
176 ip_sub_address_family_t safi,
177 ip_feature_location_t loc,
178 const char *feature_name,
179 u32 sw_if_index, int enable,
180 void *feature_config, u32 n_feature_config_bytes)
182 if (IP_FEATURE_INPUT == loc)
185 FOR_EACH_IP_ADDRESS_SUB_FAMILY (safi)
186 vnet_feature_enable_disable (ip_arc_names[loc][af][safi],
187 feature_name, sw_if_index,
188 enable, feature_config,
189 n_feature_config_bytes);
191 vnet_feature_enable_disable (ip_arc_names[loc][af][safi],
192 feature_name, sw_if_index,
193 enable, feature_config,
194 n_feature_config_bytes);
197 vnet_feature_enable_disable (ip_arc_names[loc][af][SAFI_UNICAST],
198 feature_name, sw_if_index,
199 enable, feature_config,
200 n_feature_config_bytes);
204 ip_flow_hash_set (ip_address_family_t af, u32 table_id, u32 flow_hash_config)
206 fib_protocol_t fproto;
209 fproto = ip_address_family_to_fib_proto (af);
210 fib_index = fib_table_find (fproto, table_id);
213 return VNET_API_ERROR_NO_SUCH_FIB;
215 fib_table_set_flow_hash_config (fib_index, fproto, flow_hash_config);
221 ip_flow_hash_router_id_set (u32 router_id)
223 ip_flow_hash_router_id = router_id;
227 format_ip_address_family (u8 * s, va_list * args)
229 ip_address_family_t af = va_arg (*args, int); // int promo ip_address_family_t);
234 return (format (s, "ip4"));
236 return (format (s, "ip6"));
239 return (format (s, "unknown"));
243 unformat_ip_address_family (unformat_input_t * input, va_list * args)
245 ip_address_family_t *af = va_arg (*args, ip_address_family_t *);
247 if (unformat (input, "ip4") || unformat (input, "ipv4") ||
248 unformat (input, "IP4") || unformat (input, "IPv4"))
253 else if (unformat (input, "ip6") || unformat (input, "ipv6") ||
254 unformat (input, "IP6") || unformat (input, "IPv6"))
263 format_ip_sub_address_family (u8 * s, va_list * args)
265 ip_sub_address_family_t safi = va_arg (*args, int); // int promo ip_sub_address_family_t);
270 return (format (s, "unicast"));
272 return (format (s, "multicast"));
275 return (format (s, "unknown"));
279 unformat_ip_sub_address_family (unformat_input_t * input, va_list * args)
281 ip_sub_address_family_t *safi = va_arg (*args, ip_sub_address_family_t *);
283 if (unformat (input, "unicast") || unformat (input, "uni"))
285 *safi = SAFI_UNICAST;
288 else if (unformat (input, "multicast") || unformat (input, "multi"))
290 *safi = SAFI_MULTICAST;
297 format_ip_dscp (u8 * s, va_list * va)
299 ip_dscp_t dscp = va_arg (*va, u32); // int promotion of u8
305 return (format (s, "%s", #v));
310 return (format (s, "unknown"));
314 unformat_ip_dscp (unformat_input_t * input, va_list * args)
316 ip_dscp_t *dscp = va_arg (*args, ip_dscp_t *);
321 else if (unformat (input, #v)) \
332 format_ip_ecn (u8 * s, va_list * va)
334 ip_ecn_t ecn = va_arg (*va, u32); // int promotion of u8
340 return (format (s, "%s", #v));
345 return (format (s, "unknown"));
349 * fd.io coding-style-patch-verification: ON
352 * eval: (c-set-style "gnu")