1b8d4481aSNeale Ranns/*
2b8d4481aSNeale Ranns *------------------------------------------------------------------
3b8d4481aSNeale Ranns * pg_api.c - vnet pg api
4b8d4481aSNeale Ranns *
5b8d4481aSNeale Ranns * Copyright (c) 2016 Cisco and/or its affiliates.
6b8d4481aSNeale Ranns * Licensed under the Apache License, Version 2.0 (the "License");
7b8d4481aSNeale Ranns * you may not use this file except in compliance with the License.
8b8d4481aSNeale Ranns * You may obtain a copy of the License at:
9b8d4481aSNeale Ranns *
10b8d4481aSNeale Ranns *     http://www.apache.org/licenses/LICENSE-2.0
11b8d4481aSNeale Ranns *
12b8d4481aSNeale Ranns * Unless required by applicable law or agreed to in writing, software
13b8d4481aSNeale Ranns * distributed under the License is distributed on an "AS IS" BASIS,
14b8d4481aSNeale Ranns * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15b8d4481aSNeale Ranns * See the License for the specific language governing permissions and
16b8d4481aSNeale Ranns * limitations under the License.
17b8d4481aSNeale Ranns *------------------------------------------------------------------
18b8d4481aSNeale Ranns */
19b8d4481aSNeale Ranns
20b8d4481aSNeale Ranns#include <vnet/vnet.h>
21b8d4481aSNeale Ranns#include <vlibmemory/api.h>
22b8d4481aSNeale Ranns
23b8d4481aSNeale Ranns#include <vnet/pg/pg.h>
24b8d4481aSNeale Ranns
25b8d4481aSNeale Ranns#include <vnet/vnet_msg_enum.h>
26b8d4481aSNeale Ranns
27b8d4481aSNeale Ranns#define vl_typedefs		/* define message structures */
28b8d4481aSNeale Ranns#include <vnet/vnet_all_api_h.h>
29b8d4481aSNeale Ranns#undef vl_typedefs
30b8d4481aSNeale Ranns
31b8d4481aSNeale Ranns#define vl_endianfun		/* define message structures */
32b8d4481aSNeale Ranns#include <vnet/vnet_all_api_h.h>
33b8d4481aSNeale Ranns#undef vl_endianfun
34b8d4481aSNeale Ranns
35b8d4481aSNeale Ranns/* instantiate all the print functions we know about */
36b8d4481aSNeale Ranns#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
37b8d4481aSNeale Ranns#define vl_printfun
38b8d4481aSNeale Ranns#include <vnet/vnet_all_api_h.h>
39b8d4481aSNeale Ranns#undef vl_printfun
40b8d4481aSNeale Ranns
41b8d4481aSNeale Ranns#include <vlibapi/api_helper_macros.h>
42b8d4481aSNeale Ranns
43b8d4481aSNeale Ranns
44b8d4481aSNeale Ranns#define foreach_pg_api_msg                                              \
45b8d4481aSNeale Ranns_(PG_CREATE_INTERFACE, pg_create_interface)                             \
46b8d4481aSNeale Ranns_(PG_CAPTURE, pg_capture)                                               \
47b8d4481aSNeale Ranns_(PG_ENABLE_DISABLE, pg_enable_disable)
48b8d4481aSNeale Ranns
49b8d4481aSNeale Rannsstatic void
50b8d4481aSNeale Rannsvl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
51b8d4481aSNeale Ranns{
52b8d4481aSNeale Ranns  vl_api_pg_create_interface_reply_t *rmp;
53b8d4481aSNeale Ranns  int rv = 0;
54b8d4481aSNeale Ranns
55b8d4481aSNeale Ranns  pg_main_t *pg = &pg_main;
5622e9cfd7SMohsin Kazmi  u32 pg_if_id = pg_interface_add_or_get (pg, ntohl (mp->interface_id),
5722e9cfd7SMohsin Kazmi					  mp->gso_enabled,
5822e9cfd7SMohsin Kazmi					  ntohl (mp->gso_size));
59b8d4481aSNeale Ranns  pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
60b8d4481aSNeale Ranns
61b8d4481aSNeale Ranns  /* *INDENT-OFF* */
63b8d4481aSNeale Ranns  ({
64b8d4481aSNeale Ranns    rmp->sw_if_index = ntohl(pi->sw_if_index);
65b8d4481aSNeale Ranns  }));
66b8d4481aSNeale Ranns  /* *INDENT-ON* */
67b8d4481aSNeale Ranns}
68b8d4481aSNeale Ranns
69b8d4481aSNeale Rannsstatic void
70b8d4481aSNeale Rannsvl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
71b8d4481aSNeale Ranns{
72b8d4481aSNeale Ranns  vl_api_pg_capture_reply_t *rmp;
73b8d4481aSNeale Ranns  int rv = 0;
74b8d4481aSNeale Ranns
75b8d4481aSNeale Ranns  vnet_main_t *vnm = vnet_get_main ();
76b8d4481aSNeale Ranns  vnet_interface_main_t *im = &vnm->interface_main;
77b8d4481aSNeale Ranns  vnet_hw_interface_t *hi = 0;
78b8d4481aSNeale Ranns
79b8d4481aSNeale Ranns  u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
80b8d4481aSNeale Ranns  vec_terminate_c_string (intf_name);
81b8d4481aSNeale Ranns  u32 hw_if_index = ~0;
82b8d4481aSNeale Ranns  uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
83b8d4481aSNeale Ranns  if (p)
84b8d4481aSNeale Ranns    hw_if_index = *p;
85b8d4481aSNeale Ranns  vec_free (intf_name);
86b8d4481aSNeale Ranns
87b8d4481aSNeale Ranns  if (hw_if_index != ~0)
88b8d4481aSNeale Ranns    {
89b8d4481aSNeale Ranns      pg_capture_args_t _a, *a = &_a;
90db86329aSJakub Grajciar      char *pcap_file_name =
91db86329aSJakub Grajciar	vl_api_from_api_to_new_c_string (&mp->pcap_file_name);
92b8d4481aSNeale Ranns
93b8d4481aSNeale Ranns      hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
94b8d4481aSNeale Ranns      a->hw_if_index = hw_if_index;
95b8d4481aSNeale Ranns      a->dev_instance = hi->dev_instance;
96b8d4481aSNeale Ranns      a->is_enabled = mp->is_enabled;
97b8d4481aSNeale Ranns      a->pcap_file_name = pcap_file_name;
98b8d4481aSNeale Ranns      a->count = ntohl (mp->count);
99b8d4481aSNeale Ranns
100b8d4481aSNeale Ranns      clib_error_t *e = pg_capture (a);
101b8d4481aSNeale Ranns      if (e)
102b8d4481aSNeale Ranns	{
103b8d4481aSNeale Ranns	  clib_error_report (e);
104b8d4481aSNeale Ranns	  rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
105b8d4481aSNeale Ranns	}
106b8d4481aSNeale Ranns
107b8d4481aSNeale Ranns      vec_free (pcap_file_name);
108b8d4481aSNeale Ranns    }
110b8d4481aSNeale Ranns}
111b8d4481aSNeale Ranns
112b8d4481aSNeale Rannsstatic void
113b8d4481aSNeale Rannsvl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
114b8d4481aSNeale Ranns{
115b8d4481aSNeale Ranns  vl_api_pg_enable_disable_reply_t *rmp;
116b8d4481aSNeale Ranns  int rv = 0;
117b8d4481aSNeale Ranns
118b8d4481aSNeale Ranns  pg_main_t *pg = &pg_main;
119b8d4481aSNeale Ranns  u32 stream_index = ~0;
120b8d4481aSNeale Ranns
121b8d4481aSNeale Ranns  int is_enable = mp->is_enabled != 0;
122b8d4481aSNeale Ranns
123db86329aSJakub Grajciar  if (vl_api_string_len (&mp->stream_name) > 0)
124b8d4481aSNeale Ranns    {
1257784140fSDave Barach      u8 *stream_name = vl_api_from_api_to_new_vec (mp, &mp->stream_name);
126b8d4481aSNeale Ranns      uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
127b8d4481aSNeale Ranns      if (p)
128b8d4481aSNeale Ranns	stream_index = *p;
129b8d4481aSNeale Ranns      vec_free (stream_name);
130b8d4481aSNeale Ranns    }
131b8d4481aSNeale Ranns
132b8d4481aSNeale Ranns  pg_enable_disable (stream_index, is_enable);
133b8d4481aSNeale Ranns
135b8d4481aSNeale Ranns}
136b8d4481aSNeale Ranns
137b8d4481aSNeale Ranns#define vl_msg_name_crc_list
138b8d4481aSNeale Ranns#include <vnet/pg/pg.api.h>
139b8d4481aSNeale Ranns#undef vl_msg_name_crc_list
140b8d4481aSNeale Ranns
141b8d4481aSNeale Rannsstatic void
142b8d4481aSNeale Rannssetup_message_id_table (api_main_t * am)
143b8d4481aSNeale Ranns{
144b8d4481aSNeale Ranns#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
145b8d4481aSNeale Ranns  foreach_vl_msg_name_crc_pg;
146b8d4481aSNeale Ranns#undef _
147b8d4481aSNeale Ranns}
148b8d4481aSNeale Ranns
149b8d4481aSNeale Rannsstatic clib_error_t *
150b8d4481aSNeale Rannspg_api_hookup (vlib_main_t * vm)
151b8d4481aSNeale Ranns{
15239d69112SDave Barach  api_main_t *am = vlibapi_get_main ();
153b8d4481aSNeale Ranns
154b8d4481aSNeale Ranns#define _(N,n)                                                  \
155b8d4481aSNeale Ranns    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
156b8d4481aSNeale Ranns                           vl_api_##n##_t_handler,              \
157b8d4481aSNeale Ranns                           vl_noop_handler,                     \
158b8d4481aSNeale Ranns                           vl_api_##n##_t_endian,               \
159b8d4481aSNeale Ranns                           vl_api_##n##_t_print,                \
160b8d4481aSNeale Ranns                           sizeof(vl_api_##n##_t), 1);
161b8d4481aSNeale Ranns  foreach_pg_api_msg;
162b8d4481aSNeale Ranns#undef _
163b8d4481aSNeale Ranns
164b8d4481aSNeale Ranns  /*
165b8d4481aSNeale Ranns   * Set up the (msg_name, crc, message-id) table
166b8d4481aSNeale Ranns   */
167b8d4481aSNeale Ranns  setup_message_id_table (am);
168b8d4481aSNeale Ranns
169b8d4481aSNeale Ranns  return 0;
170b8d4481aSNeale Ranns}
171b8d4481aSNeale Ranns
172b8d4481aSNeale RannsVLIB_API_INIT_FUNCTION (pg_api_hookup);
173b8d4481aSNeale Ranns
174b8d4481aSNeale Ranns/*
175b8d4481aSNeale Ranns * fd.io coding-style-patch-verification: ON
176b8d4481aSNeale Ranns *
177b8d4481aSNeale Ranns * Local Variables:
178b8d4481aSNeale Ranns * eval: (c-set-style "gnu")
179b8d4481aSNeale Ranns * End:
180b8d4481aSNeale Ranns */