FD.io VPP  v16.09
Vector Packet Processing
ip46_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 /*
16  * ip/ip4_cli.c: ip4 commands
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39 
40 #include <vnet/ip/ip.h>
41 
43 { return clib_net_to_host_u32 (a1->data_u32) - clib_net_to_host_u32 (a2->data_u32); }
44 
46 {
47  int i;
48  for (i = 0; i < ARRAY_LEN (a1->as_u16); i++)
49  {
50  int cmp = clib_net_to_host_u16 (a1->as_u16[i]) - clib_net_to_host_u16 (a2->as_u16[i]);
51  if (cmp != 0)
52  return cmp;
53  }
54  return 0;
55 }
56 
57 VLIB_CLI_COMMAND (set_interface_ip_command, static) = {
58  .path = "set interface ip",
59  .short_help = "IP4/IP6 commands",
60 };
61 
63 {
64  ip4_main_t * im4 = &ip4_main;
65  ip4_address_t * ip4_addrs = 0;
66  u32 *ip4_masks = 0;
67  ip6_main_t * im6 = &ip6_main;
68  ip6_address_t * ip6_addrs = 0;
69  u32 *ip6_masks = 0;
71  int i;
72 
73  foreach_ip_interface_address (&im4->lookup_main, ia, sw_if_index,
74  0 /* honor unnumbered */,
75  ({
76  ip4_address_t * x = (ip4_address_t *)
77  ip_interface_address_get_address (&im4->lookup_main, ia);
78  vec_add1 (ip4_addrs, x[0]);
79  vec_add1 (ip4_masks, ia->address_length);
80  }));
81 
82  foreach_ip_interface_address (&im6->lookup_main, ia, sw_if_index,
83  0 /* honor unnumbered */,
84  ({
85  ip6_address_t * x = (ip6_address_t *)
86  ip_interface_address_get_address (&im6->lookup_main, ia);
87  vec_add1 (ip6_addrs, x[0]);
88  vec_add1 (ip6_masks, ia->address_length);
89  }));
90 
91  for (i = 0; i < vec_len (ip4_addrs); i++)
92  ip4_add_del_interface_address (vm, sw_if_index, &ip4_addrs[i],
93  ip4_masks[i], 1 /* is_del */);
94  for (i = 0; i < vec_len (ip6_addrs); i++)
95  ip6_add_del_interface_address (vm, sw_if_index, &ip6_addrs[i],
96  ip6_masks[i], 1 /* is_del */);
97 
98  vec_free (ip4_addrs);
99  vec_free (ip4_masks);
100  vec_free (ip6_addrs);
101  vec_free (ip6_masks);
102 }
103 
104 static clib_error_t *
105 ip_address_delete_cleanup (vnet_main_t * vnm, u32 hw_if_index, u32 is_create)
106 {
107  vlib_main_t * vm = vlib_get_main();
108  vnet_hw_interface_t * hw;
109 
110  if (is_create)
111  return 0;
112 
113  hw = vnet_get_hw_interface (vnm, hw_if_index);
114 
116  return 0;
117 }
118 
120 
121 static clib_error_t *
123  unformat_input_t * input,
124  vlib_cli_command_t * cmd)
125 {
126  vnet_main_t * vnm = vnet_get_main();
127  ip4_address_t a4;
128  ip6_address_t a6;
129  clib_error_t * error = 0;
130  u32 sw_if_index, length, is_del;
131 
132  sw_if_index = ~0;
133  is_del = 0;
134 
135  if (unformat (input, "del"))
136  is_del = 1;
137 
138  if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
139  {
140  error = clib_error_return (0, "unknown interface `%U'",
141  format_unformat_error, input);
142  goto done;
143  }
144 
145  if (is_del && unformat (input, "all"))
146  ip_del_all_interface_addresses (vm, sw_if_index);
147  else if (unformat (input, "%U/%d", unformat_ip4_address, &a4, &length))
148  error = ip4_add_del_interface_address (vm, sw_if_index, &a4, length,
149  is_del);
150  else if (unformat (input, "%U/%d", unformat_ip6_address, &a6, &length))
151  error = ip6_add_del_interface_address (vm, sw_if_index, &a6, length,
152  is_del);
153  else
154  {
155  error = clib_error_return (0, "expected IP4/IP6 address/length `%U'",
156  format_unformat_error, input);
157  goto done;
158  }
159 
160 
161  done:
162  return error;
163 }
164 /*?
165  * To set an interface ip address, use "set interface ip address":
166  *
167  * @cliexpar
168  * @cliexstart{set interface ip address}
169 
170  * vpp# set interface ip address GigabitEthernet2/0/0 db01::1/64
171  * Note that the debug CLI does not enforce classful mask-width / addressing constraints.
172  * Interfaces may have multiple ip4 and ip6 addresses.
173  * There is no concept of primary vs. secondary interface addresses; they're just addresses.
174  * To delete a specific interface ip address, use "set interface ip address del":
175  * vpp# set interface ip address del GigabitEthernet2/0/0 6.0.0.2/24
176  * To delete all interfaces addresses (ip4+ip6), use "set interface ip address del <intfc> all"
177  * vpp# set interface ip address del GigabitEthernet2/0/0 all
178  * @cliexend
179  ?*/
180 VLIB_CLI_COMMAND (set_interface_ip_address_command, static) = {
181  .path = "set interface ip address",
182  .function = add_del_ip_address,
183  .short_help = "Add/delete IP4/IP6 address for interface",
184 };
185 
186 /* Dummy init function to get us linked in. */
188 { return 0; }
189 
#define foreach_ip_interface_address(lm, a, sw_if_index, loop, body)
Definition: lookup.h:622
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
void ip_del_all_interface_addresses(vlib_main_t *vm, u32 sw_if_index)
Definition: ip46_cli.c:62
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
ip_lookup_main_t lookup_main
Definition: ip4.h:115
unformat_function_t unformat_vnet_sw_interface
static clib_error_t * ip4_cli_init(vlib_main_t *vm)
Definition: ip46_cli.c:187
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:977
VNET_HW_INTERFACE_ADD_DEL_FUNCTION(ip_address_delete_cleanup)
unformat_function_t unformat_ip4_address
Definition: format.h:68
clib_error_t * ip4_add_del_interface_address(vlib_main_t *vm, u32 sw_if_index, ip4_address_t *address, u32 address_length, u32 is_del)
Definition: ip4_forward.c:1355
static clib_error_t * ip_address_delete_cleanup(vnet_main_t *vnm, u32 hw_if_index, u32 is_create)
Definition: ip46_cli.c:105
int ip6_address_compare(ip6_address_t *a1, ip6_address_t *a2)
Definition: ip46_cli.c:45
unformat_function_t unformat_ip6_address
Definition: format.h:86
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
static clib_error_t * add_del_ip_address(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ip46_cli.c:122
#define ARRAY_LEN(x)
Definition: clib.h:59
unsigned int u32
Definition: types.h:88
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
ip6_main_t ip6_main
Definition: ip6_forward.c:2955
ip_lookup_main_t lookup_main
Definition: ip6.h:110
IPv4 main type.
Definition: ip4.h:114
int ip4_address_compare(ip4_address_t *a1, ip4_address_t *a2)
Definition: ip46_cli.c:42
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)
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1578
u16 as_u16[8]
Definition: ip6_packet.h:48
clib_error_t * ip6_add_del_interface_address(vlib_main_t *vm, u32 sw_if_index, ip6_address_t *address, u32 address_length, u32 is_del)
Definition: ip6_forward.c:1204
#define clib_error_return(e, args...)
Definition: error.h:111
struct _unformat_input_t unformat_input_t