FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
pg_api.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * pg_api.c - vnet pg api
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19 
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22 
23 #include <vnet/pg/pg.h>
24 
25 #include <vnet/vnet_msg_enum.h>
26 
27 #define vl_typedefs /* define message structures */
28 #include <vnet/vnet_all_api_h.h>
29 #undef vl_typedefs
30 
31 #define vl_endianfun /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
33 #undef vl_endianfun
34 
35 /* instantiate all the print functions we know about */
36 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
37 #define vl_printfun
38 #include <vnet/vnet_all_api_h.h>
39 #undef vl_printfun
40 
42 
43 
44 #define foreach_pg_api_msg \
45 _(PG_CREATE_INTERFACE, pg_create_interface) \
46 _(PG_CAPTURE, pg_capture) \
47 _(PG_ENABLE_DISABLE, pg_enable_disable) \
48 _(PG_INTERFACE_ENABLE_DISABLE_COALESCE, pg_interface_enable_disable_coalesce)
49 
50 static void
52 {
54  int rv = 0;
55 
56  pg_main_t *pg = &pg_main;
57  u32 pg_if_id = pg_interface_add_or_get (pg, ntohl (mp->interface_id),
58  mp->gso_enabled,
59  ntohl (mp->gso_size), 0);
60  pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
61 
62  /* *INDENT-OFF* */
63  REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
64  ({
65  rmp->sw_if_index = ntohl(pi->sw_if_index);
66  }));
67  /* *INDENT-ON* */
68 }
69 
70 static void
73 {
74  vl_api_pg_interface_enable_disable_coalesce_reply_t *rmp;
75  int rv = 0;
76 
78 
79  u32 sw_if_index = ntohl (mp->sw_if_index);
80 
81  pg_main_t *pg = &pg_main;
82  vnet_main_t *vnm = vnet_get_main ();
85 
86  if (hw)
87  {
88  pg_interface_t *pi =
90  if (pi->gso_enabled)
92  hw->tx_node_index);
93  else
94  rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
95  }
96  else
97  {
98  rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
99  }
100 
102  REPLY_MACRO (VL_API_PG_INTERFACE_ENABLE_DISABLE_COALESCE_REPLY);
103 }
104 
105 static void
107 {
108  vl_api_pg_capture_reply_t *rmp;
109  int rv = 0;
110 
111  vnet_main_t *vnm = vnet_get_main ();
113  vnet_hw_interface_t *hi = 0;
114 
115  u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
116  vec_terminate_c_string (intf_name);
117  u32 hw_if_index = ~0;
118  uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
119  if (p)
120  hw_if_index = *p;
121  vec_free (intf_name);
122 
123  if (hw_if_index != ~0)
124  {
125  pg_capture_args_t _a, *a = &_a;
126  char *pcap_file_name =
128 
129  hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
130  a->hw_if_index = hw_if_index;
131  a->dev_instance = hi->dev_instance;
132  a->is_enabled = mp->is_enabled;
133  a->pcap_file_name = pcap_file_name;
134  a->count = ntohl (mp->count);
135 
136  clib_error_t *e = pg_capture (a);
137  if (e)
138  {
139  clib_error_report (e);
140  rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
141  }
142 
143  vec_free (pcap_file_name);
144  }
145  REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
146 }
147 
148 static void
150 {
151  vl_api_pg_enable_disable_reply_t *rmp;
152  int rv = 0;
153 
154  pg_main_t *pg = &pg_main;
155  u32 stream_index = ~0;
156 
157  int is_enable = mp->is_enabled != 0;
158 
159  if (vl_api_string_len (&mp->stream_name) > 0)
160  {
161  u8 *stream_name = vl_api_from_api_to_new_vec (mp, &mp->stream_name);
162  uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
163  if (p)
164  stream_index = *p;
165  vec_free (stream_name);
166  }
167 
168  pg_enable_disable (stream_index, is_enable);
169 
170  REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
171 }
172 
173 #define vl_msg_name_crc_list
174 #include <vnet/pg/pg.api.h>
175 #undef vl_msg_name_crc_list
176 
177 static void
179 {
180 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
181  foreach_vl_msg_name_crc_pg;
182 #undef _
183 }
184 
185 static clib_error_t *
187 {
188  api_main_t *am = vlibapi_get_main ();
189 
190 #define _(N,n) \
191  vl_msg_api_set_handlers(VL_API_##N, #n, \
192  vl_api_##n##_t_handler, \
193  vl_noop_handler, \
194  vl_api_##n##_t_endian, \
195  vl_api_##n##_t_print, \
196  sizeof(vl_api_##n##_t), 1);
198 #undef _
199 
200  /*
201  * Set up the (msg_name, crc, message-id) table
202  */
204 
205  return 0;
206 }
207 
209 
210 /*
211  * fd.io coding-style-patch-verification: ON
212  *
213  * Local Variables:
214  * eval: (c-set-style "gnu")
215  * End:
216  */
u32 sw_if_index
Definition: pg.h:304
#define foreach_pg_api_msg
Definition: pg_api.c:44
u32 pg_interface_add_or_get(pg_main_t *pg, uword stream_index, u8 gso_enabled, u32 gso_size, u8 coalesce_enabled)
Definition: stream.c:199
vl_api_interface_index_t sw_if_index
Definition: pg.api:49
void pg_interface_enable_disable_coalesce(pg_interface_t *pi, u8 enable, u32 tx_node_index)
Definition: stream.c:182
a
Definition: bitmap.h:538
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
Definition: pg.h:324
vnet_interface_main_t interface_main
Definition: vnet.h:59
#define REPLY_MACRO2(t, body)
char * pcap_file_name
Definition: pg.h:393
vl_api_interface_index_t sw_if_index
Definition: pg.api:62
uword * stream_index_by_name
Definition: pg.h:333
#define vec_terminate_c_string(V)
(If necessary) NULL terminate a vector containing a c-string.
Definition: vec.h:1090
VLIB_API_INIT_FUNCTION(pg_api_hookup)
u32 hw_if_index
Definition: pg.h:390
vlib_main_t * vm
Definition: in2out_ed.c:1582
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
string stream_name[]
Definition: pg.api:96
Enable / disable packet generator request.
Definition: pg.api:91
static void vl_api_pg_enable_disable_t_handler(vl_api_pg_enable_disable_t *mp)
Definition: pg_api.c:149
unsigned char u8
Definition: types.h:56
PacketGenerator capture packets on given interface request.
Definition: pg.api:75
unsigned int u32
Definition: types.h:88
vl_api_interface_index_t interface_id
Definition: pg.api:36
static clib_error_t * pg_api_hookup(vlib_main_t *vm)
Definition: pg_api.c:186
uword * hw_interface_by_name
Definition: interface.h:861
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
PacketGenerator interface enable/disable packet coalesce.
Definition: pg.api:58
#define REPLY_MACRO(t)
clib_error_t * pg_capture(pg_capture_args_t *a)
Definition: cli.c:81
u32 vl_api_string_len(vl_api_string_t *astr)
Definition: api_shared.c:1170
string pcap_file_name[]
Definition: pg.api:82
vl_api_interface_index_t interface_id
Definition: pg.api:79
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:227
#define BAD_SW_IF_INDEX_LABEL
PacketGenerator create interface request.
Definition: pg.api:32
u8 gso_enabled
Definition: pg.h:311
bool is_enabled[default=true]
Definition: pg.api:80
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
static vnet_hw_interface_t * vnet_get_sup_hw_interface_api_visible_or_null(vnet_main_t *vnm, u32 sw_if_index)
static void setup_message_id_table(api_main_t *am)
Definition: pg_api.c:178
char * vl_api_from_api_to_new_c_string(vl_api_string_t *astr)
Definition: api_shared.c:1204
bool is_enabled[default=true]
Definition: pg.api:95
static void vl_api_pg_create_interface_t_handler(vl_api_pg_create_interface_t *mp)
Definition: pg_api.c:51
#define clib_error_report(e)
Definition: error.h:113
vl_api_ip4_address_t hi
Definition: arp.api:37
u64 uword
Definition: types.h:112
PacketGenerator create interface response.
Definition: pg.api:45
static void vl_api_pg_capture_t_handler(vl_api_pg_capture_t *mp)
Definition: pg_api.c:106
u32 dev_instance
Definition: pg.h:391
#define hash_get_mem(h, key)
Definition: hash.h:269
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:389
pg_main_t pg_main
Definition: init.c:44
u8 is_enabled
Definition: pg.h:392
void pg_enable_disable(u32 stream_index, int is_enable)
Definition: cli.c:58
u8 * vl_api_from_api_to_new_vec(void *mp, vl_api_string_t *astr)
Definition: api_shared.c:1188
static void vl_api_pg_interface_enable_disable_coalesce_t_handler(vl_api_pg_interface_enable_disable_coalesce_t *mp)
Definition: pg_api.c:72
pg_interface_t * interfaces
Definition: pg.h:336
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:33
#define VALIDATE_SW_IF_INDEX(mp)