FD.io VPP  v17.01.1-3-gc6833f8
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 {
22  u8 mac_addr[6];
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;
34  fib_prefix_t prefix;
35  u8 mac_addr[6];
36  mac_addr_t *mac_addrs = 0;
37  u32 sw_if_index;
38  u32 i;
39 
40  next_hops = NULL;
41  rpaths = NULL;
42  prefix.fp_len = 32;
43  prefix.fp_proto = FIB_PROTOCOL_IP4;
44 
45  /* Get a line of input. */
46  if (!unformat_user (input, unformat_line_input, line_input))
47  return 0;
48 
49  if (!unformat (line_input, "%U %U",
50  unformat_ip4_address, &prefix.fp_addr.ip4,
51  unformat_vnet_sw_interface, vnm, &sw_if_index))
52  goto barf;
53 
54  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
55  {
56  if (unformat (line_input, "mac %U",
57  unformat_ethernet_address, &mac_addr))
58  {
59  mac_addr_t *ma;
60  vec_add2 (mac_addrs, ma, 1);
61  clib_memcpy (ma, mac_addr, sizeof (mac_addr));
62  }
63  else if (unformat (line_input, "next-hop %U",
64  unformat_ip4_address, &next_hop.ip4))
65  {
66  vec_add1 (next_hops, next_hop);
67  }
68  else
69  {
70  barf:
71  return clib_error_return (0, "unknown input `%U'",
72  format_unformat_error, input);
73  }
74  }
75  if (vec_len (mac_addrs) == 0 || vec_len (mac_addrs) != vec_len (next_hops))
76  goto barf;
77 
78  /* Create / delete special interface route /32's */
79 
80  for (i = 0; i < vec_len (mac_addrs); i++)
81  {
82  fib_route_path_t *rpath;
83 
86  &next_hops[i],
87  sw_if_index, mac_addrs[i].mac_addr);
88 
89  vec_add2 (rpaths, rpath, 1);
90 
91  rpath->frp_proto = FIB_PROTOCOL_IP4;
92  rpath->frp_addr = next_hops[i];
93  rpath->frp_sw_if_index = sw_if_index;
94  rpath->frp_fib_index = ~0;
95  rpath->frp_weight = 1;
96  rpath->frp_label_stack = NULL;
97  }
98 
99  fib_table_entry_path_add2 (0, // default FIB table
100  &prefix,
102 
103  vec_free (mac_addrs);
104  vec_free (next_hops);
105 
106  return 0;
107 }
108 
109 /* *INDENT-OFF* */
110 VLIB_CLI_COMMAND (virtual_ip_cmd_fn_command, static) = {
111  .path = "ip virtual",
112  .short_help = "ip virtual <addr> <interface> [mac <Mi>]+",
113  .function = virtual_ip_cmd_fn_command_fn,
114 };
115 /* *INDENT-ON* */
116 
117 /*
118  * fd.io coding-style-patch-verification: ON
119  *
120  * Local Variables:
121  * eval: (c-set-style "gnu")
122  * End:
123  */
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:154
fib_protocol_t frp_proto
The protocol of the address below.
Definition: fib_types.h:290
ip46_address_t frp_addr
The next-hop address.
Definition: fib_types.h:299
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
A representation of a path as described by a route producer.
Definition: fib_types.h:285
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
#define NULL
Definition: clib.h:55
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 *rpath)
Add n paths to an entry (aka route) in the FIB.
Definition: fib_table.c:521
From the CLI.
Definition: fib_entry.h:62
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:482
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:521
unformat_function_t unformat_vnet_sw_interface
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:215
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
u32 frp_sw_if_index
The interface.
Definition: fib_types.h:311
mpls_label_t * frp_label_stack
The outgoing MPLS label Stack.
Definition: fib_types.h:328
Aggregrate type for a prefix.
Definition: fib_types.h:145
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:977
u16 fp_len
The mask length.
Definition: fib_types.h:149
Definition: fib_entry.h:216
unformat_function_t unformat_ip4_address
Definition: format.h:76
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:168
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
u32 frp_weight
[un]equal cost path weight
Definition: fib_types.h:320
#define clib_memcpy(a, b, c)
Definition: string.h:69
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:245
unsigned int u32
Definition: types.h:88
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
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
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
#define clib_error_return(e, args...)
Definition: error.h:111
struct _unformat_input_t unformat_input_t
unformat_function_t unformat_line_input
Definition: format.h:281
u32 frp_fib_index
The FIB index to lookup the nexthop Only valid for recursive paths.
Definition: fib_types.h:316