1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/*
Itay Marom
Cisco Systems, Inc.
*/
/*
Copyright (c) 2015-2015 Cisco Systems, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "trex_rpc_cmds.h"
#include <trex_rpc_server_api.h>
#include <trex_stateless.h>
#include <trex_stateless_port.h>
#include <trex_rpc_cmds_table.h>
#include <fstream>
#include <iostream>
#include <unistd.h>
#ifndef TREX_RPC_MOCK_SERVER
#include <../linux_dpdk/version.h>
#endif
using namespace std;
/**
* ping command
*/
trex_rpc_cmd_rc_e
TrexRpcCmdPing::_run(const Json::Value ¶ms, Json::Value &result) {
result["result"] = "ACK";
return (TREX_RPC_CMD_OK);
}
/**
* query command
*/
trex_rpc_cmd_rc_e
TrexRpcCmdGetCmds::_run(const Json::Value ¶ms, Json::Value &result) {
vector<string> cmds;
TrexRpcCommandsTable::get_instance().query(cmds);
Json::Value test = Json::arrayValue;
for (auto cmd : cmds) {
test.append(cmd);
}
result["result"] = test;
return (TREX_RPC_CMD_OK);
}
/**
* get version
*
*/
trex_rpc_cmd_rc_e
TrexRpcCmdGetVersion::_run(const Json::Value ¶ms, Json::Value &result) {
Json::Value §ion = result["result"];
#ifndef TREX_RPC_MOCK_SERVER
section["version"] = VERSION_BUILD_NUM;
section["build_date"] = get_build_date();
section["build_time"] = get_build_time();
section["built_by"] = VERSION_USER;
#else
section["version"] = "v1.75";
section["build_date"] = __DATE__;
section["build_time"] = __TIME__;
section["built_by"] = "MOCK";
#endif
return (TREX_RPC_CMD_OK);
}
/**
* get the CPU model
*
*/
std::string
TrexRpcCmdGetSysInfo::get_cpu_model() {
static const string cpu_prefix = "model name";
std::ifstream cpuinfo("/proc/cpuinfo");
if (cpuinfo.is_open()) {
while (cpuinfo.good()) {
std::string line;
getline(cpuinfo, line);
int pos = line.find(cpu_prefix);
if (pos == string::npos) {
continue;
}
/* trim it */
int index = cpu_prefix.size() + 1;
while ( (line[index] == ' ') || (line[index] == ':') ) {
index++;
}
return line.substr(index);
}
}
return "unknown";
}
void
TrexRpcCmdGetSysInfo::get_hostname(string &hostname) {
char buffer[256];
buffer[0] = 0;
gethostname(buffer, sizeof(buffer));
/* write hostname */
hostname = buffer;
}
/**
* get system info
*
*/
trex_rpc_cmd_rc_e
TrexRpcCmdGetSysInfo::_run(const Json::Value ¶ms, Json::Value &result) {
string hostname;
TrexStateless & instance = TrexStateless::get_instance();
Json::Value §ion = result["result"];
get_hostname(hostname);
section["hostname"] = hostname;
section["uptime"] = TrexRpcServer::get_server_uptime();
/* FIXME: core count */
section["dp_core_count"] = instance.get_dp_core_count();
section["core_type"] = get_cpu_model();
/* ports */
section["port_count"] = instance.get_port_count();
section["ports"] = Json::arrayValue;
for (int i = 0; i < instance.get_port_count(); i++) {
string driver;
string speed;
TrexStatelessPort *port = instance.get_port_by_id(i);
port->get_properties(driver, speed);
section["ports"][i]["index"] = i;
section["ports"][i]["driver"] = driver;
section["ports"][i]["speed"] = speed;
section["ports"][i]["owner"] = port->get_owner();
section["ports"][i]["status"] = port->get_state_as_string();
}
return (TREX_RPC_CMD_OK);
}
/**
* returns the current owner of the device
*
* @author imarom (08-Sep-15)
*
* @param params
* @param result
*
* @return trex_rpc_cmd_rc_e
*/
trex_rpc_cmd_rc_e
TrexRpcCmdGetOwner::_run(const Json::Value ¶ms, Json::Value &result) {
Json::Value §ion = result["result"];
uint8_t port_id = parse_port(params, result);
TrexStatelessPort *port = TrexStateless::get_instance().get_port_by_id(port_id);
section["owner"] = port->get_owner();
return (TREX_RPC_CMD_OK);
}
/**
* acquire device
*
*/
trex_rpc_cmd_rc_e
TrexRpcCmdAcquire::_run(const Json::Value ¶ms, Json::Value &result) {
uint8_t port_id = parse_port(params, result);
const string &new_owner = parse_string(params, "user", result);
bool force = parse_bool(params, "force", result);
/* if not free and not you and not force - fail */
TrexStatelessPort *port = TrexStateless::get_instance().get_port_by_id(port_id);
if ( (!port->is_free_to_aquire()) && (port->get_owner() != new_owner) && (!force)) {
generate_execute_err(result, "port is already taken by '" + port->get_owner() + "'");
}
port->set_owner(new_owner);
result["result"] = port->get_owner_handler();
return (TREX_RPC_CMD_OK);
}
/**
* release device
*
*/
trex_rpc_cmd_rc_e
TrexRpcCmdRelease::_run(const Json::Value ¶ms, Json::Value &result) {
uint8_t port_id = parse_port(params, result);
TrexStatelessPort *port = TrexStateless::get_instance().get_port_by_id(port_id);
if (port->get_state() == TrexStatelessPort::PORT_STATE_TRANSMITTING) {
generate_execute_err(result, "cannot release a port during transmission");
}
port->clear_owner();
result["result"] = "ACK";
return (TREX_RPC_CMD_OK);
}
/**
* get port stats
*
*/
trex_rpc_cmd_rc_e
TrexRpcCmdGetPortStats::_run(const Json::Value ¶ms, Json::Value &result) {
uint8_t port_id = parse_port(params, result);
TrexStatelessPort *port = TrexStateless::get_instance().get_port_by_id(port_id);
if (port->get_state() == TrexStatelessPort::PORT_STATE_DOWN) {
generate_execute_err(result, "cannot get stats - port is down");
}
result["result"]["status"] = port->get_state_as_string();
port->encode_stats(result["result"]);
return (TREX_RPC_CMD_OK);
}
|