blob: e5d0a08a527b3f5e771295f37fe6ff05af62576d [file] [log] [blame]
Neale Rannsb8d44812017-11-10 06:53:54 -08001/*
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>
Neale Rannsb8d44812017-11-10 06:53:54 -080022#include <vnet/pg/pg.h>
23
Filip Tehlar83e5a112021-06-21 12:11:28 +000024#include <vnet/format_fns.h>
25#include <vnet/pg/pg.api_enum.h>
26#include <vnet/pg/pg.api_types.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080027
Filip Tehlar83e5a112021-06-21 12:11:28 +000028#define REPLY_MSG_ID_BASE pg->msg_id_base
Neale Rannsb8d44812017-11-10 06:53:54 -080029#include <vlibapi/api_helper_macros.h>
30
Neale Rannsb8d44812017-11-10 06:53:54 -080031static void
32vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
33{
34 vl_api_pg_create_interface_reply_t *rmp;
35 int rv = 0;
36
37 pg_main_t *pg = &pg_main;
Neale Ranns6197cb72021-06-03 14:43:21 +000038 u32 pg_if_id =
39 pg_interface_add_or_get (pg, ntohl (mp->interface_id), mp->gso_enabled,
40 ntohl (mp->gso_size), 0, PG_MODE_ETHERNET);
Neale Rannsb8d44812017-11-10 06:53:54 -080041 pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
42
Neale Rannsb8d44812017-11-10 06:53:54 -080043 REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
44 ({
45 rmp->sw_if_index = ntohl(pi->sw_if_index);
46 }));
Neale Rannsb8d44812017-11-10 06:53:54 -080047}
48
49static void
Neale Ranns6197cb72021-06-03 14:43:21 +000050vl_api_pg_create_interface_v2_t_handler (vl_api_pg_create_interface_v2_t *mp)
51{
52 vl_api_pg_create_interface_v2_reply_t *rmp;
53 int rv = 0;
54
55 pg_main_t *pg = &pg_main;
56 u32 pg_if_id =
57 pg_interface_add_or_get (pg, ntohl (mp->interface_id), mp->gso_enabled,
58 ntohl (mp->gso_size), 0, (u8) mp->mode);
59 pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
60
61 REPLY_MACRO2 (VL_API_PG_CREATE_INTERFACE_V2_REPLY,
62 ({ rmp->sw_if_index = ntohl (pi->sw_if_index); }));
63}
64
65static void
Mohsin Kazmif382b062020-08-11 15:00:44 +020066 vl_api_pg_interface_enable_disable_coalesce_t_handler
67 (vl_api_pg_interface_enable_disable_coalesce_t * mp)
68{
69 vl_api_pg_interface_enable_disable_coalesce_reply_t *rmp;
70 int rv = 0;
71
72 VALIDATE_SW_IF_INDEX (mp);
73
74 u32 sw_if_index = ntohl (mp->sw_if_index);
75
76 pg_main_t *pg = &pg_main;
77 vnet_main_t *vnm = vnet_get_main ();
78 vnet_hw_interface_t *hw =
79 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
80
81 if (hw)
82 {
83 pg_interface_t *pi =
84 pool_elt_at_index (pg->interfaces, hw->dev_instance);
85 if (pi->gso_enabled)
86 pg_interface_enable_disable_coalesce (pi, mp->coalesce_enabled,
87 hw->tx_node_index);
88 else
89 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
90 }
91 else
92 {
93 rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
94 }
95
96 BAD_SW_IF_INDEX_LABEL;
97 REPLY_MACRO (VL_API_PG_INTERFACE_ENABLE_DISABLE_COALESCE_REPLY);
98}
99
100static void
Neale Rannsb8d44812017-11-10 06:53:54 -0800101vl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
102{
Filip Tehlar83e5a112021-06-21 12:11:28 +0000103 pg_main_t *pg = &pg_main;
Neale Rannsb8d44812017-11-10 06:53:54 -0800104 vl_api_pg_capture_reply_t *rmp;
105 int rv = 0;
106
107 vnet_main_t *vnm = vnet_get_main ();
108 vnet_interface_main_t *im = &vnm->interface_main;
109 vnet_hw_interface_t *hi = 0;
110
111 u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
112 vec_terminate_c_string (intf_name);
113 u32 hw_if_index = ~0;
114 uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
115 if (p)
116 hw_if_index = *p;
117 vec_free (intf_name);
118
119 if (hw_if_index != ~0)
120 {
121 pg_capture_args_t _a, *a = &_a;
Jakub Grajciardb863292020-01-30 14:14:15 +0100122 char *pcap_file_name =
123 vl_api_from_api_to_new_c_string (&mp->pcap_file_name);
Neale Rannsb8d44812017-11-10 06:53:54 -0800124
125 hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
126 a->hw_if_index = hw_if_index;
127 a->dev_instance = hi->dev_instance;
128 a->is_enabled = mp->is_enabled;
129 a->pcap_file_name = pcap_file_name;
130 a->count = ntohl (mp->count);
131
132 clib_error_t *e = pg_capture (a);
133 if (e)
134 {
135 clib_error_report (e);
136 rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
137 }
138
139 vec_free (pcap_file_name);
140 }
141 REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
142}
143
144static void
145vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
146{
147 vl_api_pg_enable_disable_reply_t *rmp;
148 int rv = 0;
149
150 pg_main_t *pg = &pg_main;
151 u32 stream_index = ~0;
152
153 int is_enable = mp->is_enabled != 0;
Neale Rannsb8d44812017-11-10 06:53:54 -0800154
Jakub Grajciardb863292020-01-30 14:14:15 +0100155 if (vl_api_string_len (&mp->stream_name) > 0)
Neale Rannsb8d44812017-11-10 06:53:54 -0800156 {
Dave Barach77841402020-04-29 17:04:10 -0400157 u8 *stream_name = vl_api_from_api_to_new_vec (mp, &mp->stream_name);
Neale Rannsb8d44812017-11-10 06:53:54 -0800158 uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
159 if (p)
160 stream_index = *p;
161 vec_free (stream_name);
162 }
163
164 pg_enable_disable (stream_index, is_enable);
165
166 REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
167}
168
Filip Tehlar83e5a112021-06-21 12:11:28 +0000169#include <vnet/pg/pg.api.c>
Neale Rannsb8d44812017-11-10 06:53:54 -0800170static clib_error_t *
171pg_api_hookup (vlib_main_t * vm)
172{
Filip Tehlar83e5a112021-06-21 12:11:28 +0000173 pg_main_t *pg = &pg_main;
Neale Rannsb8d44812017-11-10 06:53:54 -0800174 /*
175 * Set up the (msg_name, crc, message-id) table
176 */
Filip Tehlar83e5a112021-06-21 12:11:28 +0000177 pg->msg_id_base = setup_message_id_table ();
Neale Rannsb8d44812017-11-10 06:53:54 -0800178
179 return 0;
180}
181
182VLIB_API_INIT_FUNCTION (pg_api_hookup);
183
184/*
185 * fd.io coding-style-patch-verification: ON
186 *
187 * Local Variables:
188 * eval: (c-set-style "gnu")
189 * End:
190 */