FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
cli.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2016 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17 #include <stdint.h>
18 #include <net/if.h>
19 #include <sys/ioctl.h>
20 #include <inttypes.h>
21 
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ethernet/ethernet.h>
25 
26 #include <memif/memif.h>
27 
28 static clib_error_t *
30  vlib_cli_command_t * cmd)
31 {
32  unformat_input_t _line_input, *line_input = &_line_input;
33  int r;
34  u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
35  memif_create_if_args_t args = { 0 };
37 
38  /* Get a line of input. */
39  if (!unformat_user (input, unformat_line_input, line_input))
40  return 0;
41 
42  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
43  {
44  if (unformat (line_input, "key 0x%" PRIx64, &args.key))
45  ;
46  else if (unformat (line_input, "socket %s", &args.socket_filename))
47  ;
48  else if (unformat (line_input, "ring-size %u", &ring_size))
49  ;
50  else if (unformat (line_input, "buffer-size %u", &args.buffer_size))
51  ;
52  else if (unformat (line_input, "master"))
53  args.is_master = 1;
54  else if (unformat (line_input, "slave"))
55  args.is_master = 0;
56  else if (unformat (line_input, "hw-addr %U",
58  args.hw_addr_set = 1;
59  else
60  return clib_error_return (0, "unknown input `%U'",
61  format_unformat_error, input);
62  }
63  unformat_free (line_input);
64 
65  if (!is_pow2 (ring_size))
66  return clib_error_return (0, "ring size must be power of 2");
67 
68  args.log2_ring_size = min_log2 (ring_size);
69 
70  r = memif_create_if (vm, &args);
71 
72  if (r <= VNET_API_ERROR_SYSCALL_ERROR_1
73  && r >= VNET_API_ERROR_SYSCALL_ERROR_10)
74  return clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
75 
76  if (r == VNET_API_ERROR_INVALID_INTERFACE)
77  return clib_error_return (0, "Invalid interface name");
78 
79  if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
80  return clib_error_return (0, "Interface already exists");
81 
82  return 0;
83 }
84 
85 /* *INDENT-OFF* */
86 VLIB_CLI_COMMAND (memif_create_command, static) = {
87  .path = "create memif",
88  .short_help = "create memif [key <key>] [socket <path>] "
89  "[ring-size <size>] [buffer-size <size>] [hw-addr <mac-address>] "
90  "<master|slave>",
91  .function = memif_create_command_fn,
92 };
93 /* *INDENT-ON* */
94 
95 static clib_error_t *
97  vlib_cli_command_t * cmd)
98 {
99  unformat_input_t _line_input, *line_input = &_line_input;
100  u64 key = 0;
101  u8 key_defined = 0;
102 
103  /* Get a line of input. */
104  if (!unformat_user (input, unformat_line_input, line_input))
105  return 0;
106 
107  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
108  {
109  if (unformat (line_input, "key 0x%" PRIx64, &key))
110  key_defined = 1;
111  else
112  return clib_error_return (0, "unknown input `%U'",
113  format_unformat_error, input);
114  }
115  unformat_free (line_input);
116 
117  if (!key_defined)
118  return clib_error_return (0, "missing key");
119 
120  memif_delete_if (vm, key);
121 
122  return 0;
123 }
124 
125 /* *INDENT-OFF* */
126 VLIB_CLI_COMMAND (memif_delete_command, static) = {
127  .path = "delete memif",
128  .short_help = "delete memif key <key-value>",
129  .function = memif_delete_command_fn,
130 };
131 /* *INDENT-ON* */
132 
133 static clib_error_t *
135  vlib_cli_command_t * cmd)
136 {
137  memif_main_t *mm = &memif_main;
138  memif_if_t *mif;
139  vnet_main_t *vnm = vnet_get_main ();
140  int i;
141 
142  /* *INDENT-OFF* */
143  pool_foreach (mif, mm->interfaces,
144  ({
145  vlib_cli_output (vm, "interface %U", format_vnet_sw_if_index_name,
146  vnm, mif->sw_if_index);
147  vlib_cli_output (vm, " key 0x%" PRIx64 " file %s", mif->key,
148  mif->socket_filename);
149  vlib_cli_output (vm, " listener %d conn-fd %d int-fd %d", mif->listener_index,
150  mif->connection.fd, mif->interrupt_line.fd);
151  vlib_cli_output (vm, " ring-size %u num-c2s-rings %u num-s2c-rings %u buffer_size %u",
152  (1 << mif->log2_ring_size),
153  mif->num_s2m_rings,
154  mif->num_m2s_rings,
155  mif->buffer_size);
156  for (i=0; i < mif->num_s2m_rings; i++)
157  {
158  memif_ring_t * ring = memif_get_ring (mif, MEMIF_RING_S2M, i);
159  if (ring)
160  {
161  vlib_cli_output (vm, " slave-to-master ring %u:", i);
162  vlib_cli_output (vm, " head %u tail %u", ring->head, ring->tail);
163  }
164  }
165  for (i=0; i < mif->num_m2s_rings; i++)
166  {
167  memif_ring_t * ring = memif_get_ring (mif, MEMIF_RING_M2S, i);
168  if (ring)
169  {
170  vlib_cli_output (vm, " master-to-slave ring %u:", i);
171  vlib_cli_output (vm, " head %u tail %u", ring->head, ring->tail);
172  }
173  }
174  }));
175  /* *INDENT-ON* */
176 
177  return 0;
178 }
179 
180 /* *INDENT-OFF* */
181 VLIB_CLI_COMMAND (memif_show_command, static) = {
182  .path = "show memif",
183  .short_help = "show memif",
184  .function = memif_show_command_fn,
185 };
186 /* *INDENT-ON* */
187 
188 clib_error_t *
190 {
191  return 0;
192 }
193 
195 
196 /*
197  * fd.io coding-style-patch-verification: ON
198  *
199  * Local Variables:
200  * eval: (c-set-style "gnu")
201  * End:
202  */
memif_if_t * interfaces
Definition: memif.h:143
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static clib_error_t * memif_create_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:29
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:982
u16 head
Definition: memif.h:59
u8 num_m2s_rings
Definition: memif.h:125
static uword min_log2(uword x)
Definition: clib.h:189
int memif_delete_if(vlib_main_t *vm, u64 key)
Definition: memif.c:974
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:376
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
#define clib_error_return(e, args...)
Definition: error.h:111
unsigned long u64
Definition: types.h:89
#define MEMIF_DEFAULT_RING_SIZE
Definition: memif.h:32
unformat_function_t unformat_line_input
Definition: format.h:281
static clib_error_t * memif_delete_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:96
static clib_error_t * memif_show_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:134
struct _unformat_input_t unformat_input_t
int memif_create_if(vlib_main_t *vm, memif_create_if_args_t *args)
Definition: memif.c:786
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
vlib_main_t * vm
Definition: buffer.c:276
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:227
unsigned int u32
Definition: types.h:88
static uword is_pow2(uword x)
Definition: clib.h:272
unsigned char u8
Definition: types.h:56
clib_error_t * memif_cli_init(vlib_main_t *vm)
Definition: cli.c:189
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
static_always_inline memif_ring_t * memif_get_ring(memif_if_t *mif, memif_ring_type_t type, u16 ring_num)
Definition: memif.h:225
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
u16 tail
Definition: memif.h:60
memif_main_t memif_main
Definition: memif.c:47
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:577
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:971
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
#define MEMIF_DEFAULT_BUFFER_SIZE
Definition: memif.h:36