FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
ip_container_proxy.c
Go to the documentation of this file.
1 /*
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:
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  * ip/ip_lookup.c: ip4/6 adjacency and lookup table management
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
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:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
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.
38  */
39 
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>
45 
48 {
49  u32 fib_index;
50 
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");
54 
55  fib_index = fib_table_get_table_id_for_sw_if_index (args->prefix.fp_proto,
56  args->sw_if_index);
57  if (args->is_add)
58  {
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);
63  &args->prefix,
65  FIB_ENTRY_FLAG_EXCLUSIVE, &proxy_dpo);
66  dpo_reset (&proxy_dpo);
67  }
68  else
69  {
70  fib_table_entry_special_remove (fib_index, &args->prefix,
72  }
73  return 0;
74 }
75 
76 u8
78 {
79  u32 fib_index;
80  fib_node_index_t fei;
81  const dpo_id_t *dpo;
82  l3_proxy_dpo_t *l3p;
83  load_balance_t *lb0;
84 
86  sw_if_index);
87  if (fib_index == ~0)
88  return 0;
89 
90  fei = fib_table_lookup_exact_match (fib_index, pfx);
91  if (fei == FIB_NODE_INDEX_INVALID)
92  return 0;
93 
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)
98  return 0;
99 
100  l3p = l3_proxy_dpo_get (dpo->dpoi_index);
101  return (l3p->l3p_sw_if_index == sw_if_index);
102 }
103 
105 {
107  void *ctx;
109 
110 static fib_table_walk_rc_t
112 {
114  const fib_prefix_t *pfx;
115  const dpo_id_t *dpo;
116  load_balance_t *lb;
117  l3_proxy_dpo_t *l3p;
118 
119  pfx = fib_entry_get_prefix (fei);
121  {
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);
127  }
128 
130 }
131 
132 void
134 {
135  fib_table_t *fib_table;
137  .cb = cb,
138  .ctx = ctx,
139  };
140 
141  /* *INDENT-OFF* */
142  pool_foreach (fib_table, ip4_main.fibs)
143  {
144  fib_table_walk(fib_table->ft_index,
147  &wctx);
148  }
149  pool_foreach (fib_table, ip6_main.fibs)
150  {
151  fib_table_walk(fib_table->ft_index,
154  &wctx);
155  }
156  /* *INDENT-ON* */
157 }
158 
159 clib_error_t *
161  unformat_input_t * main_input, vlib_cli_command_t * cmd)
162 {
163  unformat_input_t _line_input, *line_input = &_line_input;
164  fib_prefix_t pfx;
165  u32 is_del, addr_set = 0;
166  vnet_main_t *vnm;
168 
169  vnm = vnet_get_main ();
170  is_del = 0;
171  sw_if_index = ~0;
172  clib_memset (&pfx, 0, sizeof (pfx));
173 
174  /* Get a line of input. */
175  if (!unformat_user (main_input, unformat_line_input, line_input))
176  return 0;
177 
178  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
179  {
180  if (unformat (line_input, "%U", unformat_ip4_address, &pfx.fp_addr.ip4))
181  {
183  pfx.fp_len = 32;
184  addr_set = 1;
185  }
186  else if (unformat (line_input, "%U",
187  unformat_ip6_address, &pfx.fp_addr.ip6))
188  {
190  pfx.fp_len = 128;
191  addr_set = 1;
192  }
193  else if (unformat (line_input, "%U",
195  ;
196  else if (unformat (line_input, "del"))
197  is_del = 1;
198  else
199  {
200  unformat_free (line_input);
201  return (clib_error_return (0, "unknown input '%U'",
202  format_unformat_error, line_input));
203  }
204  }
205 
206  if (~0 == sw_if_index || !addr_set)
207  {
208  unformat_free (line_input);
209  vlib_cli_output (vm, "interface and address must be set");
210  return 0;
211  }
212 
214  .prefix = pfx,
215  .sw_if_index = sw_if_index,
216  .is_add = !is_del,
217  };
219  unformat_free (line_input);
220  return (NULL);
221 }
222 
223 /* *INDENT-OFF* */
225  .path = "ip container",
226  .function = ip_container_cmd,
227  .short_help = "ip container <address> <interface>",
228  .is_mp_safe = 1,
229 };
230 /* *INDENT-ON* */
231 
232 clib_error_t *
234  vlib_cli_command_t * cmd)
235 {
236  unformat_input_t _line_input, *line_input = &_line_input;
237  vnet_main_t *vnm = vnet_get_main ();
238  fib_prefix_t pfx;
239  u32 sw_if_index = ~0;
240  u8 has_proxy;
241 
242  if (!unformat_user (main_input, unformat_line_input, line_input))
243  return 0;
244  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
245  {
246  if (unformat (line_input, "%U", unformat_ip4_address, &pfx.fp_addr.ip4))
247  {
249  pfx.fp_len = 32;
250  }
251  else if (unformat (line_input, "%U",
252  unformat_ip6_address, &pfx.fp_addr.ip6))
253  {
255  pfx.fp_len = 128;
256  }
257  else if (unformat (line_input, "%U",
259  ;
260  else
261  {
262  unformat_free (line_input);
263  return (clib_error_return (0, "unknown input '%U'",
264  format_unformat_error, line_input));
265  }
266  }
267 
268  if (~0 == sw_if_index)
269  {
270  unformat_free (line_input);
271  vlib_cli_output (vm, "no interface");
272  return (clib_error_return (0, "no interface"));
273  }
274 
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");
277 
278  unformat_free (line_input);
279  return 0;
280 }
281 
282 /* *INDENT-OFF* */
284  .path = "show ip container",
285  .function = show_ip_container_cmd_fn,
286  .short_help = "show ip container <address> <interface>",
287  .is_mp_safe = 1,
288 };
289 /* *INDENT-ON* */
290 
291 /*
292  * fd.io coding-style-patch-verification: ON
293  *
294  * Local Variables:
295  * eval: (c-set-style "gnu")
296  * End:
297  */
load_balance.h
dpo_id_t_::dpoi_index
index_t dpoi_index
the index of objects of that type
Definition: dpo.h:190
DPO_INVALID
#define DPO_INVALID
An initialiser for DPOs declared on the stack.
Definition: dpo.h:204
unformat_user
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
fib_table_entry_special_dpo_add
fib_node_index_t fib_table_entry_special_dpo_add(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source, fib_entry_flag_t flags, const dpo_id_t *dpo)
Add a 'special' entry to the FIB that links to the DPO passed A special entry is an entry that the FI...
Definition: fib_table.c:324
fib_entry_get_prefix
const fib_prefix_t * fib_entry_get_prefix(fib_node_index_t fib_entry_index)
Definition: fib_entry.c:1728
ip4_main
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1105
l3_proxy_dpo.h
dpo_id_t_::dpoi_type
dpo_type_t dpoi_type
the type
Definition: dpo.h:178
unformat_line_input
unformat_function_t unformat_line_input
Definition: format.h:275
ip_container_proxy_walk
void ip_container_proxy_walk(ip_container_proxy_cb_t cb, void *ctx)
Definition: ip_container_proxy.c:133
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_cli_command_t::path
char * path
Definition: cli.h:96
ip_container_proxy_walk_ctx_t
struct ip_container_proxy_walk_ctx_t_ ip_container_proxy_walk_ctx_t
FIB_NODE_INDEX_INVALID
#define FIB_NODE_INDEX_INVALID
Definition: fib_types.h:30
fib_table.h
ip_container_proxy_walk_ctx_t_::ctx
void * ctx
Definition: ip_container_proxy.c:107
fib_prefix_t_::fp_len
u16 fp_len
The mask length.
Definition: fib_types.h:206
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
ip_container_proxy_cb_t
int(* ip_container_proxy_cb_t)(const fib_prefix_t *pfx, u32 sw_if_index, void *ctx)
Definition: ip_container_proxy.h:31
fib_table_walk
void fib_table_walk(u32 fib_index, fib_protocol_t proto, fib_table_walk_fn_t fn, void *ctx)
Walk all entries in a FIB table N.B: This is NOT safe to deletes.
Definition: fib_table.c:1239
FIB_ENTRY_FLAG_EXCLUSIVE
@ FIB_ENTRY_FLAG_EXCLUSIVE
Definition: fib_entry.h:116
unformat_input_t
struct _unformat_input_t unformat_input_t
ip_container_proxy.h
FIB_SOURCE_PROXY
@ FIB_SOURCE_PROXY
A route the is being 'proxied' on behalf of another device.
Definition: fib_source.h:53
vnet_sw_interface_is_api_valid
static uword vnet_sw_interface_is_api_valid(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface_funcs.h:285
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
fib_table_t_
A protocol Independent FIB table.
Definition: fib_table.h:71
fib_table_entry_special_remove
void fib_table_entry_special_remove(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source)
Remove a 'special' entry from the FIB.
Definition: fib_table.c:424
pool_foreach
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
show_ip_container_cmd_fn
clib_error_t * show_ip_container_cmd_fn(vlib_main_t *vm, unformat_input_t *main_input, vlib_cli_command_t *cmd)
Definition: ip_container_proxy.c:233
clib_error_return_code
#define clib_error_return_code(e, code, flags, args...)
Definition: error.h:93
fib_table_get_table_id_for_sw_if_index
u32 fib_table_get_table_id_for_sw_if_index(fib_protocol_t proto, u32 sw_if_index)
Get the Table-ID of the FIB bound to the interface.
Definition: fib_table.c:1082
unformat_free
static void unformat_free(unformat_input_t *i)
Definition: format.h:155
fib_table_t_::ft_index
fib_node_index_t ft_index
Index into FIB vector.
Definition: fib_table.h:97
fib_entry_contribute_ip_forwarding
const dpo_id_t * fib_entry_contribute_ip_forwarding(fib_node_index_t fib_entry_index)
Definition: fib_entry.c:506
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
format.h
l3_proxy_dpo_get
static l3_proxy_dpo_t * l3_proxy_dpo_get(index_t index)
Definition: l3_proxy_dpo.h:57
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
fib_node_index_t
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:29
load_balance_get_bucket_i
static const dpo_id_t * load_balance_get_bucket_i(const load_balance_t *lb, u32 bucket)
Definition: load_balance.h:229
l3_proxy_dpo_t_
The data-path object representing L3 proxy.
Definition: l3_proxy_dpo.h:26
format_unformat_error
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
ip6_main_t::fibs
struct fib_table_t_ * fibs
Definition: ip6.h:115
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
ip_container_proxy_walk_ctx_t_::cb
ip_container_proxy_cb_t cb
Definition: ip_container_proxy.c:106
FIB_PROTOCOL_IP4
@ FIB_PROTOCOL_IP4
Definition: fib_types.h:36
vlib_cli_output
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
fib_proto_to_dpo
dpo_proto_t fib_proto_to_dpo(fib_protocol_t fib_proto)
Definition: fib_types.c:343
fib_prefix_t_::fp_addr
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:225
ip6_main
ip6_main_t ip6_main
Definition: ip6_forward.c:2787
fib_table_lookup_exact_match
fib_node_index_t fib_table_lookup_exact_match(u32 fib_index, const fib_prefix_t *prefix)
Perfom an exact match in the non-forwarding table.
Definition: fib_table.c:97
DPO_L3_PROXY
@ DPO_L3_PROXY
Definition: dpo.h:122
vnet_main_t
Definition: vnet.h:76
ip_container_proxy_fib_table_walk
static fib_table_walk_rc_t ip_container_proxy_fib_table_walk(fib_node_index_t fei, void *arg)
Definition: ip_container_proxy.c:111
l3_proxy_dpo_t_::l3p_sw_if_index
u32 l3p_sw_if_index
The Software interface index on which traffic is l3_proxyd.
Definition: l3_proxy_dpo.h:37
unformat_vnet_sw_interface
unformat_function_t unformat_vnet_sw_interface
Definition: interface_funcs.h:459
fib_entry_is_sourced
int fib_entry_is_sourced(fib_node_index_t fib_entry_index, fib_source_t source)
Definition: fib_entry_src.c:139
l3_proxy_dpo_add_or_lock
void l3_proxy_dpo_add_or_lock(dpo_proto_t proto, u32 sw_if_index, dpo_id_t *dpo)
Definition: l3_proxy_dpo.c:56
u32
unsigned int u32
Definition: types.h:88
fib_table_walk_rc_t
enum fib_table_walk_rc_t_ fib_table_walk_rc_t
return code controlling how a table walk proceeds
ip4_main_t::fibs
struct fib_table_t_ * fibs
Vector of FIBs.
Definition: ip4.h:112
FIB_PROTOCOL_IP6
@ FIB_PROTOCOL_IP6
Definition: fib_types.h:37
ctx
long ctx[MAX_CONNS]
Definition: main.c:144
ip_container_command_node
static vlib_cli_command_t ip_container_command_node
(constructor) VLIB_CLI_COMMAND (ip_container_command_node)
Definition: ip_container_proxy.c:224
load_balance_get
static load_balance_t * load_balance_get(index_t lbi)
Definition: load_balance.h:220
vnet_ip_container_proxy_add_del
clib_error_t * vnet_ip_container_proxy_add_del(vnet_ip_container_proxy_args_t *args)
Definition: ip_container_proxy.c:47
fib_prefix_t_::fp_proto
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:211
FIB_TABLE_WALK_CONTINUE
@ FIB_TABLE_WALK_CONTINUE
Continue on to the next entry.
Definition: fib_table.h:916
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
show_ip_container_command
static vlib_cli_command_t show_ip_container_command
(constructor) VLIB_CLI_COMMAND (show_ip_container_command)
Definition: ip_container_proxy.c:283
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
ip_container_proxy_is_set
u8 ip_container_proxy_is_set(fib_prefix_t *pfx, u32 sw_if_index)
Definition: ip_container_proxy.c:77
dpo_id_t_
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:172
unformat_ip6_address
unformat_function_t unformat_ip6_address
Definition: format.h:89
unformat_ip4_address
unformat_function_t unformat_ip4_address
Definition: format.h:68
load_balance_t_
The FIB DPO provieds;.
Definition: load_balance.h:106
vlib_cli_command_t
Definition: cli.h:92
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
ip_container_cmd
clib_error_t * ip_container_cmd(vlib_main_t *vm, unformat_input_t *main_input, vlib_cli_command_t *cmd)
Definition: ip_container_proxy.c:160
dpo_reset
void dpo_reset(dpo_id_t *dpo)
reset a DPO ID The DPO will be unlocked.
Definition: dpo.c:234
fib_prefix_t_
Aggregate type for a prefix.
Definition: fib_types.h:202
ip_container_proxy_walk_ctx_t_
Definition: ip_container_proxy.c:104
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
vnet_ip_container_proxy_args_t
struct _vnet_ip_container_proxy_args vnet_ip_container_proxy_args_t