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.
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>
26 virtual_ip_cmd_fn_command_fn (vlib_main_t * vm,
27 unformat_input_t * input,
28 vlib_cli_command_t * cmd)
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;
36 mac_addr_t *mac_addrs = 0;
43 prefix.fp_proto = FIB_PROTOCOL_IP4;
45 /* Get a line of input. */
46 if (!unformat_user (input, unformat_line_input, line_input))
49 if (!unformat (line_input, "%U %U",
50 unformat_ip4_address, &prefix.fp_addr.ip4,
51 unformat_vnet_sw_interface, vnm, &sw_if_index))
54 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
56 if (unformat (line_input, "mac %U",
57 unformat_ethernet_address, &mac_addr))
60 vec_add2 (mac_addrs, ma, 1);
61 clib_memcpy (ma, mac_addr, sizeof (mac_addr));
63 else if (unformat (line_input, "next-hop %U",
64 unformat_ip4_address, &next_hop.ip4))
66 vec_add1 (next_hops, next_hop);
71 return clib_error_return (0, "unknown input `%U'",
72 format_unformat_error, input);
75 if (vec_len (mac_addrs) == 0 || vec_len (mac_addrs) != vec_len (next_hops))
78 /* Create / delete special interface route /32's */
80 for (i = 0; i < vec_len (mac_addrs); i++)
82 fib_route_path_t *rpath;
84 adj_nbr_add_or_lock_w_rewrite (FIB_PROTOCOL_IP4,
87 sw_if_index, mac_addrs[i].mac_addr);
89 vec_add2 (rpaths, rpath, 1);
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;
99 fib_table_entry_path_add2 (0, // default FIB table
101 FIB_SOURCE_CLI, FIB_ENTRY_FLAG_NONE, rpaths);
103 vec_free (mac_addrs);
104 vec_free (next_hops);
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,
118 * fd.io coding-style-patch-verification: ON
121 * eval: (c-set-style "gnu")