Initial commit of vpp code.
[vpp.git] / vpp / app / vpe_cli.c
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     u8 mac_addr[6];
20 } mac_addr_t;
21
22 static clib_error_t *
23 virtual_ip_cmd_fn_command_fn (vlib_main_t * vm,
24                  unformat_input_t * input,
25                  vlib_cli_command_t * cmd)
26 {
27     unformat_input_t _line_input, * line_input = &_line_input;
28     vnet_main_t * vnm = vnet_get_main();
29     ip4_main_t * im = &ip4_main;
30     ip_lookup_main_t * lm = &im->lookup_main;
31     ip4_address_t ip_addr, next_hop;
32     u8 mac_addr[6];
33     mac_addr_t *mac_addrs = 0;
34     u32 sw_if_index;
35     u32 i, f;
36
37     /* Get a line of input. */
38     if (! unformat_user (input, unformat_line_input, line_input))
39         return 0;
40
41     if (!unformat(line_input, "%U %U", 
42                   unformat_ip4_address, &ip_addr,
43                   unformat_vnet_sw_interface, vnm, &sw_if_index))
44         goto barf;
45
46     while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
47         if (unformat (line_input, "mac %U", 
48                       unformat_ethernet_address, 
49                       &mac_addr))
50         {
51             mac_addr_t *ma;
52             vec_add2 (mac_addrs, ma, 1);
53             memcpy(ma, mac_addr, sizeof (mac_addr));
54         } else {
55         barf:
56             return clib_error_return (0, "unknown input `%U'",
57                                       format_unformat_error, input);
58         }
59     }
60     if (vec_len (mac_addrs) == 0)
61         goto barf;
62
63     /* Create / delete special interface route /32's */
64     next_hop.as_u32 = 0;
65
66     for (i = 0; i < vec_len(mac_addrs); i++) {
67         ip_adjacency_t adj;
68         u32 adj_index;
69         
70         adj.lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
71         
72         vnet_rewrite_for_sw_interface
73             (vnm,
74              VNET_L3_PACKET_TYPE_IP4,
75              sw_if_index,
76              ip4_rewrite_node.index,
77              &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 = (i + 1 < vec_len(mac_addrs)) ? IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP : 0;
85         ip4_add_del_route_next_hop (im,
86                                     IP4_ROUTE_FLAG_ADD | f,
87                                     &ip_addr,
88                                     32 /* insert /32's */,
89                                     &next_hop,
90                                     sw_if_index,
91                                     1 /* weight */, 
92                                     adj_index, 
93                                     (u32)~0 /* explicit fib index */);
94     }
95
96     vec_free (mac_addrs);
97
98     return 0;
99 }
100
101 VLIB_CLI_COMMAND (virtual_ip_cmd_fn_command, static) = {
102     .path = "ip virtual",
103     .short_help = "ip virtual <addr> <interface> [mac <Mi>]+",
104     .function = virtual_ip_cmd_fn_command_fn,
105 };