FD.io VPP  v16.09
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 
18 typedef struct
19 {
20  u8 mac_addr[6];
21 } mac_addr_t;
22 
23 static clib_error_t *
25  unformat_input_t * input,
26  vlib_cli_command_t * cmd)
27 {
28  unformat_input_t _line_input, *line_input = &_line_input;
29  vnet_main_t *vnm = vnet_get_main ();
30  ip4_main_t *im = &ip4_main;
31  ip_lookup_main_t *lm = &im->lookup_main;
32  ip4_address_t ip_addr, next_hop;
33  u8 mac_addr[6];
34  mac_addr_t *mac_addrs = 0;
35  u32 sw_if_index;
36  u32 i, f;
37 
38  /* Get a line of input. */
39  if (!unformat_user (input, unformat_line_input, line_input))
40  return 0;
41 
42  if (!unformat (line_input, "%U %U",
43  unformat_ip4_address, &ip_addr,
44  unformat_vnet_sw_interface, vnm, &sw_if_index))
45  goto barf;
46 
47  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
48  {
49  if (unformat (line_input, "mac %U",
50  unformat_ethernet_address, &mac_addr))
51  {
52  mac_addr_t *ma;
53  vec_add2 (mac_addrs, ma, 1);
54  clib_memcpy (ma, mac_addr, sizeof (mac_addr));
55  }
56  else
57  {
58  barf:
59  return clib_error_return (0, "unknown input `%U'",
60  format_unformat_error, input);
61  }
62  }
63  if (vec_len (mac_addrs) == 0)
64  goto barf;
65 
66  /* Create / delete special interface route /32's */
67  next_hop.as_u32 = 0;
68 
69  for (i = 0; i < vec_len (mac_addrs); i++)
70  {
71  ip_adjacency_t adj;
72  u32 adj_index;
73 
74  memset (&adj, 0, sizeof (adj));
76 
77  vnet_rewrite_for_sw_interface (vnm, VNET_L3_PACKET_TYPE_IP4, sw_if_index, ip4_rewrite_node.index, &mac_addrs[i], /* destination address */
78  &adj.rewrite_header,
79  sizeof (adj.rewrite_data));
80 
81  ip_add_adjacency (lm, &adj, 1 /* one adj */ ,
82  &adj_index);
83 
84  f =
85  (i + 1 < vec_len (mac_addrs)) ? IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP : 0;
87  32 /* insert /32's */ ,
88  &next_hop, sw_if_index, 1 /* weight */ ,
89  adj_index,
90  (u32) ~ 0 /* explicit fib index */ );
91  }
92 
93  vec_free (mac_addrs);
94 
95  return 0;
96 }
97 
98 /* *INDENT-OFF* */
99 VLIB_CLI_COMMAND (virtual_ip_cmd_fn_command, static) = {
100  .path = "ip virtual",
101  .short_help = "ip virtual <addr> <interface> [mac <Mi>]+",
102  .function = virtual_ip_cmd_fn_command_fn,
103 };
104 /* *INDENT-ON* */
105 
106 /*
107  * fd.io coding-style-patch-verification: ON
108  *
109  * Local Variables:
110  * eval: (c-set-style "gnu")
111  * End:
112  */
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
void ip4_add_del_route_next_hop(ip4_main_t *im, u32 flags, ip4_address_t *dst_address, u32 dst_address_length, ip4_address_t *next_hop, u32 next_hop_sw_if_index, u32 next_hop_weight, u32 adj_index, u32 explicit_fib_index)
Definition: ip4_forward.c:385
ip_lookup_next_t lookup_next_index
Definition: lookup.h:180
IP unicast adjacency.
Definition: lookup.h:164
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
ip_adjacency_t * ip_add_adjacency(ip_lookup_main_t *lm, ip_adjacency_t *copy_adj, u32 n_adj, u32 *adj_index_return)
Definition: lookup.c:167
#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
ip_lookup_main_t lookup_main
Definition: ip4.h:115
unformat_function_t unformat_vnet_sw_interface
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:977
unformat_function_t unformat_ip4_address
Definition: format.h:68
vlib_node_registration_t ip4_rewrite_node
(constructor) VLIB_REGISTER_NODE (ip4_rewrite_node)
Definition: ip4_forward.c:3088
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
void vnet_rewrite_for_sw_interface(vnet_main_t *vnm, vnet_l3_packet_type_t packet_type, u32 sw_if_index, u32 node_index, void *dst_address, vnet_rewrite_header_t *rw, u32 max_rewrite_bytes)
Definition: rewrite.c:190
#define clib_memcpy(a, b, c)
Definition: string.h:63
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:206
unsigned int u32
Definition: types.h:88
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
#define IP4_ROUTE_FLAG_ADD
Definition: ip4.h:308
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:24
IPv4 main type.
Definition: ip4.h:114
VLIB_CLI_COMMAND(set_interface_ip_source_and_port_range_check_command, static)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
#define IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP
Definition: ip4.h:315
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1578
This packet is to be rewritten and forwarded to the next processing node.
Definition: lookup.h:78
#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