Add memif - packet memory interface for intra-host communication
[vpp.git] / src / plugins / memif / cli.c
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 *
29 memif_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
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 };
36   args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
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",
57                          unformat_ethernet_address, args.hw_addr))
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 *
96 memif_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
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 *
134 memif_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
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 *
189 memif_cli_init (vlib_main_t * vm)
190 {
191   return 0;
192 }
193
194 VLIB_INIT_FUNCTION (memif_cli_init);
195
196 /*
197  * fd.io coding-style-patch-verification: ON
198  *
199  * Local Variables:
200  * eval: (c-set-style "gnu")
201  * End:
202  */