FD.io VPP  v16.06
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 *
106  unformat_input_t * input,
107  vlib_cli_command_t * cmd)
108 {
109  vnet_main_t * vnm = vnet_get_main();
110  ip4_address_t a4;
111  ip6_address_t a6;
112  clib_error_t * error = 0;
113  u32 sw_if_index, length, is_del;
114 
115  sw_if_index = ~0;
116  is_del = 0;
117 
118  if (unformat (input, "del"))
119  is_del = 1;
120 
121  if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
122  {
123  error = clib_error_return (0, "unknown interface `%U'",
124  format_unformat_error, input);
125  goto done;
126  }
127 
128  if (is_del && unformat (input, "all"))
129  ip_del_all_interface_addresses (vm, sw_if_index);
130  else if (unformat (input, "%U/%d", unformat_ip4_address, &a4, &length))
131  error = ip4_add_del_interface_address (vm, sw_if_index, &a4, length,
132  is_del);
133  else if (unformat (input, "%U/%d", unformat_ip6_address, &a6, &length))
134  error = ip6_add_del_interface_address (vm, sw_if_index, &a6, length,
135  is_del);
136  else
137  {
138  error = clib_error_return (0, "expected IP4/IP6 address/length `%U'",
139  format_unformat_error, input);
140  goto done;
141  }
142 
143 
144  done:
145  return error;
146 }
147 
148 VLIB_CLI_COMMAND (set_interface_ip_address_command, static) = {
149  .path = "set interface ip address",
150  .function = add_del_ip_address,
151  .short_help = "Add/delete IP4/IP6 address for interface",
152 };
153 
154 /* Dummy init function to get us linked in. */
156 { return 0; }
157 
#define foreach_ip_interface_address(lm, a, sw_if_index, loop, body)
Definition: lookup.h:534
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
void ip_del_all_interface_addresses(vlib_main_t *vm, u32 sw_if_index)
Definition: ip46_cli.c:62
ip_lookup_main_t lookup_main
Definition: ip4.h:129
unformat_function_t unformat_vnet_sw_interface
static clib_error_t * ip4_cli_init(vlib_main_t *vm)
Definition: ip46_cli.c:155
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:953
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:1239
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:298
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:105
#define ARRAY_LEN(x)
Definition: clib.h:59
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:150
unsigned int u32
Definition: types.h:88
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:87
ip6_main_t ip6_main
Definition: ip6_forward.c:2490
ip_lookup_main_t lookup_main
Definition: ip6.h:135
int ip4_address_compare(ip4_address_t *a1, ip4_address_t *a2)
Definition: ip46_cli.c:42
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
ip4_main_t ip4_main
Definition: ip4_forward.c:1394
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:1155
#define clib_error_return(e, args...)
Definition: error.h:112
struct _unformat_input_t unformat_input_t