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:
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 * ip/ip_lookup.c: ip4/6 adjacency and lookup table management
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vnet/ip/ip_container_proxy.h>
41 #include <vnet/ip/format.h>
42 #include <vnet/fib/fib_table.h>
43 #include <vnet/dpo/l3_proxy_dpo.h>
44 #include <vnet/dpo/load_balance.h>
47 vnet_ip_container_proxy_add_del (vnet_ip_container_proxy_args_t * args)
51 if (!vnet_sw_interface_is_api_valid (vnet_get_main (), args->sw_if_index))
52 return clib_error_return_code (0, VNET_API_ERROR_INVALID_INTERFACE, 0,
53 "invalid sw_if_index");
55 fib_index = fib_table_get_table_id_for_sw_if_index (args->prefix.fp_proto,
59 dpo_id_t proxy_dpo = DPO_INVALID;
60 l3_proxy_dpo_add_or_lock (fib_proto_to_dpo (args->prefix.fp_proto),
61 args->sw_if_index, &proxy_dpo);
62 fib_table_entry_special_dpo_add (fib_index,
65 FIB_ENTRY_FLAG_EXCLUSIVE, &proxy_dpo);
66 dpo_reset (&proxy_dpo);
70 fib_table_entry_special_remove (fib_index, &args->prefix,
77 ip_container_proxy_is_set (fib_prefix_t * pfx, u32 sw_if_index)
85 fib_index = fib_table_get_table_id_for_sw_if_index (pfx->fp_proto,
90 fei = fib_table_lookup_exact_match (fib_index, pfx);
91 if (fei == FIB_NODE_INDEX_INVALID)
94 dpo = fib_entry_contribute_ip_forwarding (fei);
95 lb0 = load_balance_get (dpo->dpoi_index);
96 dpo = load_balance_get_bucket_i (lb0, 0);
97 if (dpo->dpoi_type != DPO_L3_PROXY)
100 l3p = l3_proxy_dpo_get (dpo->dpoi_index);
101 return (l3p->l3p_sw_if_index == sw_if_index);
104 typedef struct ip_container_proxy_walk_ctx_t_
106 ip_container_proxy_cb_t cb;
108 } ip_container_proxy_walk_ctx_t;
110 static fib_table_walk_rc_t
111 ip_container_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
113 ip_container_proxy_walk_ctx_t *ctx = arg;
114 const fib_prefix_t *pfx;
119 pfx = fib_entry_get_prefix (fei);
120 if (fib_entry_is_sourced (fei, FIB_SOURCE_PROXY))
122 dpo = fib_entry_contribute_ip_forwarding (fei);
123 lb = load_balance_get (dpo->dpoi_index);
124 dpo = load_balance_get_bucket_i (lb, 0);
125 l3p = l3_proxy_dpo_get (dpo->dpoi_index);
126 ctx->cb (pfx, l3p->l3p_sw_if_index, ctx->ctx);
129 return FIB_TABLE_WALK_CONTINUE;
133 ip_container_proxy_walk (ip_container_proxy_cb_t cb, void *ctx)
135 fib_table_t *fib_table;
136 ip_container_proxy_walk_ctx_t wctx = {
142 pool_foreach (fib_table, ip4_main.fibs)
144 fib_table_walk(fib_table->ft_index,
146 ip_container_proxy_fib_table_walk,
149 pool_foreach (fib_table, ip6_main.fibs)
151 fib_table_walk(fib_table->ft_index,
153 ip_container_proxy_fib_table_walk,
160 ip_container_cmd (vlib_main_t * vm,
161 unformat_input_t * main_input, vlib_cli_command_t * cmd)
163 unformat_input_t _line_input, *line_input = &_line_input;
165 u32 is_del, addr_set = 0;
169 vnm = vnet_get_main ();
172 clib_memset (&pfx, 0, sizeof (pfx));
174 /* Get a line of input. */
175 if (!unformat_user (main_input, unformat_line_input, line_input))
178 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
180 if (unformat (line_input, "%U", unformat_ip4_address, &pfx.fp_addr.ip4))
182 pfx.fp_proto = FIB_PROTOCOL_IP4;
186 else if (unformat (line_input, "%U",
187 unformat_ip6_address, &pfx.fp_addr.ip6))
189 pfx.fp_proto = FIB_PROTOCOL_IP6;
193 else if (unformat (line_input, "%U",
194 unformat_vnet_sw_interface, vnm, &sw_if_index))
196 else if (unformat (line_input, "del"))
200 unformat_free (line_input);
201 return (clib_error_return (0, "unknown input '%U'",
202 format_unformat_error, line_input));
206 if (~0 == sw_if_index || !addr_set)
208 unformat_free (line_input);
209 vlib_cli_output (vm, "interface and address must be set");
213 vnet_ip_container_proxy_args_t args = {
215 .sw_if_index = sw_if_index,
218 vnet_ip_container_proxy_add_del (&args);
219 unformat_free (line_input);
224 VLIB_CLI_COMMAND (ip_container_command_node, static) = {
225 .path = "ip container",
226 .function = ip_container_cmd,
227 .short_help = "ip container <address> <interface>",
233 show_ip_container_cmd_fn (vlib_main_t * vm, unformat_input_t * main_input,
234 vlib_cli_command_t * cmd)
236 unformat_input_t _line_input, *line_input = &_line_input;
237 vnet_main_t *vnm = vnet_get_main ();
239 u32 sw_if_index = ~0;
242 if (!unformat_user (main_input, unformat_line_input, line_input))
244 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
246 if (unformat (line_input, "%U", unformat_ip4_address, &pfx.fp_addr.ip4))
248 pfx.fp_proto = FIB_PROTOCOL_IP4;
251 else if (unformat (line_input, "%U",
252 unformat_ip6_address, &pfx.fp_addr.ip6))
254 pfx.fp_proto = FIB_PROTOCOL_IP6;
257 else if (unformat (line_input, "%U",
258 unformat_vnet_sw_interface, vnm, &sw_if_index))
262 unformat_free (line_input);
263 return (clib_error_return (0, "unknown input '%U'",
264 format_unformat_error, line_input));
268 if (~0 == sw_if_index)
270 unformat_free (line_input);
271 vlib_cli_output (vm, "no interface");
272 return (clib_error_return (0, "no interface"));
275 has_proxy = ip_container_proxy_is_set (&pfx, sw_if_index);
276 vlib_cli_output (vm, "ip container proxy is: %s", has_proxy ? "on" : "off");
278 unformat_free (line_input);
283 VLIB_CLI_COMMAND (show_ip_container_command, static) = {
284 .path = "show ip container",
285 .function = show_ip_container_cmd_fn,
286 .short_help = "show ip container <address> <interface>",
292 * fd.io coding-style-patch-verification: ON
295 * eval: (c-set-style "gnu")