FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
nhrp_cli.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 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 #include <vnet/nhrp/nhrp.h>
17 
18 static clib_error_t *
20  unformat_input_t * input, vlib_cli_command_t * cmd)
21 {
22  unformat_input_t _line_input, *line_input = &_line_input;
23  ip46_address_t peer = ip46_address_initializer;
24  ip46_address_t nh = ip46_address_initializer;
26  clib_error_t *error = NULL;
27  int rv;
28 
29  sw_if_index = ~0;
30  nh_table_id = 0;
31 
32  /* Get a line of input. */
33  if (!unformat_user (input, unformat_line_input, line_input))
34  return 0;
35 
36  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
37  {
38  if (unformat (line_input, "%U", unformat_vnet_sw_interface,
39  vnet_get_main (), &sw_if_index))
40  ;
41  else if (unformat (line_input, "peer %U", unformat_ip46_address, &peer))
42  ;
43  else if (unformat (line_input, "nh %U", unformat_ip46_address, &nh))
44  ;
45  else if (unformat (line_input, "nh-table-id %d", &nh_table_id))
46  ;
47  else
48  {
49  error = clib_error_return (0, "unknown input `%U'",
50  format_unformat_error, line_input);
51  goto done;
52  }
53  }
54 
55  if (~0 == sw_if_index)
56  {
57  error = clib_error_return (0, "interface required'",
58  format_unformat_error, line_input);
59  goto done;
60  }
61  if (ip46_address_is_zero (&peer))
62  {
63  error = clib_error_return (0, "peer required'",
64  format_unformat_error, line_input);
65  goto done;
66  }
67  if (ip46_address_is_zero (&nh))
68  {
69  error = clib_error_return (0, "next-hop required'",
70  format_unformat_error, line_input);
71  goto done;
72  }
73 
74  rv = nhrp_entry_add (sw_if_index, &peer, nh_table_id, &nh);
75 
76  if (rv)
77  {
78  error = clib_error_return_code (NULL, rv, 0,
79  "NRHP error",
80  format_unformat_error, line_input);
81  }
82 
83 done:
84  unformat_free (line_input);
85 
86  return error;
87 }
88 
89 /* *INDENT-OFF* */
90 VLIB_CLI_COMMAND (nhrp_create_command, static) = {
91  .path = "create nhrp",
92  .short_help = "create nhrp <interface> peer <addr> nh <addr> [nh-table-id <ID>]",
93  .function = nhrp_add,
94 };
95 /* *INDENT-ON* */
96 
97 static clib_error_t *
99  unformat_input_t * input, vlib_cli_command_t * cmd)
100 {
101  unformat_input_t _line_input, *line_input = &_line_input;
102  ip46_address_t peer = ip46_address_initializer;
103  clib_error_t *error = NULL;
105  int rv;
106 
107  sw_if_index = ~0;
108 
109  /* Get a line of input. */
110  if (!unformat_user (input, unformat_line_input, line_input))
111  return 0;
112 
113  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
114  {
115  if (unformat (line_input, "%U", unformat_vnet_sw_interface,
116  vnet_get_main (), &sw_if_index))
117  ;
118  else if (unformat (line_input, "peer %U", unformat_ip46_address, &peer))
119  ;
120  else
121  {
122  error = clib_error_return (0, "unknown input `%U'",
123  format_unformat_error, line_input);
124  goto done;
125  }
126  }
127 
128  if (~0 == sw_if_index)
129  {
130  error = clib_error_return (0, "interface required'",
131  format_unformat_error, line_input);
132  }
133  if (ip46_address_is_zero (&peer))
134  {
135  error = clib_error_return (0, "peer required'",
136  format_unformat_error, line_input);
137  goto done;
138  }
139 
140  rv = nhrp_entry_del (sw_if_index, &peer);
141 
142  if (rv)
143  {
144  error = clib_error_return_code (NULL, rv, 0,
145  "NRHP error",
146  format_unformat_error, line_input);
147  }
148 
149 done:
150  unformat_free (line_input);
151 
152  return error;
153 }
154 
155 /* *INDENT-OFF* */
156 VLIB_CLI_COMMAND (nhrp_delete_command, static) = {
157  .path = "delete nhrp",
158  .short_help = "delete nhrp <interface> peer <addr>",
159  .function = nhrp_del,
160 };
161 /* *INDENT-ON* */
162 
163 static walk_rc_t
165 {
166  vlib_cli_output (ctx, "%U", format_nhrp_entry, nei);
167 
168  return (WALK_CONTINUE);
169 }
170 
171 
172 static clib_error_t *
174  unformat_input_t * input, vlib_cli_command_t * cmd)
175 {
176  nhrp_walk (nhrp_show_one, vm);
177  return (NULL);
178 }
179 
180 /* *INDENT-OFF* */
181 VLIB_CLI_COMMAND (nhrp_show_command, static) = {
182  .path = "show nhrp",
183  .short_help = "show nhrp",
184  .function = nhrp_show,
185 };
186 /* *INDENT-ON* */
187 
188 /*
189  * fd.io coding-style-patch-verification: ON
190  *
191  * Local Variables:
192  * eval: (c-set-style "gnu")
193  * End:
194  */
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
vl_api_fib_path_nh_t nh
Definition: fib_types.api:126
#define NULL
Definition: clib.h:58
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:41
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
unformat_function_t unformat_vnet_sw_interface
void nhrp_walk(nhrp_walk_cb_t fn, void *ctx)
Definition: nhrp.c:220
u8 * format_nhrp_entry(u8 *s, va_list *args)
Definition: nhrp.c:199
int nhrp_entry_add(u32 sw_if_index, const ip46_address_t *peer, u32 nh_table_id, const ip46_address_t *nh)
Create a new NHRP entry.
Definition: nhrp.c:119
enum walk_rc_t_ walk_rc_t
Walk return code.
static walk_rc_t nhrp_show_one(index_t nei, void *ctx)
Definition: nhrp_cli.c:164
vl_api_interface_index_t sw_if_index
Definition: gre.api:59
#define clib_error_return(e, args...)
Definition: error.h:99
unsigned int u32
Definition: types.h:88
vl_api_address_t peer
Definition: nhrp.api:28
unformat_function_t unformat_line_input
Definition: format.h:283
static clib_error_t * nhrp_add(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: nhrp_cli.c:19
static u8 ip46_address_is_zero(const ip46_address_t *ip46)
Definition: ip46_address.h:87
long ctx[MAX_CONNS]
Definition: main.c:144
struct _unformat_input_t unformat_input_t
vlib_main_t * vm
Definition: in2out_ed.c:1810
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
int nhrp_entry_del(u32 sw_if_index, const ip46_address_t *peer)
Definition: nhrp.c:173
static clib_error_t * nhrp_show(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: nhrp_cli.c:173
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:152
unformat_function_t unformat_ip46_address
Definition: format.h:63
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
#define clib_error_return_code(e, code, flags, args...)
Definition: error.h:93
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:689
#define ip46_address_initializer
Definition: ip46_address.h:52
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static clib_error_t * nhrp_del(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: nhrp_cli.c:98
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
u32 nh_table_id
Definition: nhrp.api:30