FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
vpe_cli.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 #include <vnet/ip/ip.h>
16 #include <vnet/ethernet/ethernet.h>
17 #include <vnet/adj/adj.h>
18 #include <vnet/fib/fib_table.h>
19 
20 typedef struct
21 {
23 } mac_addr_t;
24 
25 static clib_error_t *
27  unformat_input_t * input,
28  vlib_cli_command_t * cmd)
29 {
30  unformat_input_t _line_input, *line_input = &_line_input;
31  vnet_main_t *vnm = vnet_get_main ();
32  ip46_address_t next_hop, *next_hops;
33  fib_route_path_t *rpaths;
35  u8 mac_addr[6];
36  mac_addr_t *mac_addrs = 0;
38  u32 i;
39  clib_error_t *error = NULL;
40 
41  next_hops = NULL;
42  rpaths = NULL;
43  prefix.fp_len = 32;
44  prefix.fp_proto = FIB_PROTOCOL_IP4;
45 
46  /* Get a line of input. */
47  if (!unformat_user (input, unformat_line_input, line_input))
48  return 0;
49 
50  if (!unformat (line_input, "%U %U",
51  unformat_ip4_address, &prefix.fp_addr.ip4,
53  {
54  error = clib_error_return (0, "unknown input `%U'",
55  format_unformat_error, line_input);
56  goto done;
57  }
58 
59  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
60  {
61  if (unformat (line_input, "mac %U",
63  {
64  mac_addr_t *ma;
65  vec_add2 (mac_addrs, ma, 1);
66  clib_memcpy (ma, mac_addr, sizeof (mac_addr));
67  }
68  else if (unformat (line_input, "next-hop %U",
69  unformat_ip4_address, &next_hop.ip4))
70  {
71  vec_add1 (next_hops, next_hop);
72  }
73  else
74  {
75  error = clib_error_return (0, "unknown input `%U'",
76  format_unformat_error, line_input);
77  goto done;
78  }
79  }
80 
81  if (vec_len (mac_addrs) == 0 || vec_len (mac_addrs) != vec_len (next_hops))
82  {
83  error = clib_error_return (0, "unknown input `%U'",
84  format_unformat_error, line_input);
85  goto done;
86  }
87 
88  /* Create / delete special interface route /32's */
89 
90  for (i = 0; i < vec_len (mac_addrs); i++)
91  {
92  fib_route_path_t *rpath;
93 
96  &next_hops[i],
97  sw_if_index, mac_addrs[i].mac_addr);
98 
99  vec_add2 (rpaths, rpath, 1);
100 
101  rpath->frp_proto = DPO_PROTO_IP4;
102  rpath->frp_addr = next_hops[i];
103  rpath->frp_sw_if_index = sw_if_index;
104  rpath->frp_fib_index = ~0;
105  rpath->frp_weight = 1;
106  rpath->frp_label_stack = NULL;
107  }
108 
109  fib_table_entry_path_add2 (0, // default FIB table
110  &prefix,
112 
113 done:
114  vec_free (mac_addrs);
115  vec_free (next_hops);
116  vec_free (rpaths);
117  unformat_free (line_input);
118 
119  return error;
120 }
121 
122 /* *INDENT-OFF* */
124  .path = "ip virtual",
125  .short_help = "ip virtual <addr> <interface> [mac <Mi>]+ [next-hop <ip4_address>]+",
126  .function = virtual_ip_cmd_fn_command_fn,
127 };
128 /* *INDENT-ON* */
129 
130 /*
131  * fd.io coding-style-patch-verification: ON
132  *
133  * Local Variables:
134  * eval: (c-set-style "gnu")
135  * End:
136  */
fib_route_path_t_::frp_fib_index
u32 frp_fib_index
The FIB index to lookup the nexthop Only valid for recursive paths.
Definition: fib_types.h:556
adj.h
unformat_ethernet_address
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:233
unformat_user
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
fib_route_path_t_::frp_weight
u8 frp_weight
[un]equal cost path weight
Definition: fib_types.h:599
unformat_line_input
unformat_function_t unformat_line_input
Definition: format.h:275
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_cli_command_t::path
char * path
Definition: cli.h:96
mac_addr_t
Definition: vpe_cli.c:20
fib_table.h
fib_route_path_t_::frp_addr
ip46_address_t frp_addr
The next-hop address.
Definition: fib_types.h:516
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
fib_route_path_t_::frp_sw_if_index
u32 frp_sw_if_index
The interface.
Definition: fib_types.h:545
unformat_input_t
struct _unformat_input_t unformat_input_t
fib_route_path_t_::frp_proto
dpo_proto_t frp_proto
The protocol of the address below.
Definition: fib_types.h:505
ethernet.h
error
Definition: cJSON.c:88
fib_route_path_t_::frp_label_stack
fib_mpls_label_t * frp_label_stack
The outgoing MPLS label Stack.
Definition: fib_types.h:560
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
unformat_free
static void unformat_free(unformat_input_t *i)
Definition: format.h:155
VNET_LINK_IP4
@ VNET_LINK_IP4
Definition: interface.h:344
vec_add2
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:644
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
adj_nbr_add_or_lock_w_rewrite
adj_index_t adj_nbr_add_or_lock_w_rewrite(fib_protocol_t nh_proto, vnet_link_t link_type, const ip46_address_t *nh_addr, u32 sw_if_index, u8 *rewrite)
Add (and lock) a new or lock an existing neighbour adjacency.
Definition: adj_nbr.c:303
FIB_ENTRY_FLAG_NONE
@ FIB_ENTRY_FLAG_NONE
Definition: fib_entry.h:112
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
virtual_ip_cmd_fn_command
static vlib_cli_command_t virtual_ip_cmd_fn_command
(constructor) VLIB_CLI_COMMAND (virtual_ip_cmd_fn_command)
Definition: vpe_cli.c:123
format_unformat_error
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
FIB_PROTOCOL_IP4
@ FIB_PROTOCOL_IP4
Definition: fib_types.h:36
mac_addr
vl_api_mac_address_t mac_addr
Definition: l2.api:210
vnet_main_t
Definition: vnet.h:76
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
fib_table_entry_path_add2
fib_node_index_t fib_table_entry_path_add2(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source, fib_entry_flag_t flags, fib_route_path_t *rpaths)
Add n paths to an entry (aka route) in the FIB.
Definition: fib_table.c:600
unformat_vnet_sw_interface
unformat_function_t unformat_vnet_sw_interface
Definition: interface_funcs.h:459
ip.h
u32
unsigned int u32
Definition: types.h:88
fib_route_path_t_
A representation of a path as described by a route producer.
Definition: fib_types.h:500
FIB_SOURCE_CLI
@ FIB_SOURCE_CLI
From the CLI.
Definition: fib_source.h:79
vlib_main_t
Definition: main.h:102
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
DPO_PROTO_IP4
@ DPO_PROTO_IP4
Definition: dpo.h:64
unformat_ip4_address
unformat_function_t unformat_ip4_address
Definition: format.h:68
vlib_cli_command_t
Definition: cli.h:92
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
virtual_ip_cmd_fn_command_fn
static clib_error_t * virtual_ip_cmd_fn_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: vpe_cli.c:26
fib_prefix_t_
Aggregate type for a prefix.
Definition: fib_types.h:202
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
prefix
vl_api_prefix_t prefix
Definition: ip.api:146