summaryrefslogtreecommitdiffstats
path: root/src/rpc-server
diff options
context:
space:
mode:
Diffstat (limited to 'src/rpc-server')
-rw-r--r--src/rpc-server/commands/trex_rpc_cmd_general.cpp161
-rw-r--r--src/rpc-server/commands/trex_rpc_cmd_stream.cpp40
-rw-r--r--src/rpc-server/commands/trex_rpc_cmd_test.cpp30
-rw-r--r--src/rpc-server/commands/trex_rpc_cmds.h21
-rw-r--r--src/rpc-server/trex_rpc_cmds_table.cpp5
-rw-r--r--src/rpc-server/trex_rpc_jsonrpc_v2_parser.cpp8
-rw-r--r--src/rpc-server/trex_rpc_jsonrpc_v2_parser.h9
-rw-r--r--src/rpc-server/trex_rpc_req_resp_server.cpp27
-rw-r--r--src/rpc-server/trex_rpc_req_resp_server.h4
9 files changed, 224 insertions, 81 deletions
diff --git a/src/rpc-server/commands/trex_rpc_cmd_general.cpp b/src/rpc-server/commands/trex_rpc_cmd_general.cpp
index d5aa3a90..afa15973 100644
--- a/src/rpc-server/commands/trex_rpc_cmd_general.cpp
+++ b/src/rpc-server/commands/trex_rpc_cmd_general.cpp
@@ -18,9 +18,17 @@ 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_api.h>
+#include <trex_rpc_cmds_table.h>
+
+#include <fstream>
+#include <iostream>
+#include <unistd.h>
+
+//#include <netdb.h>
#ifndef TREX_RPC_MOCK_SERVER
#include <../linux_dpdk/version.h>
@@ -29,36 +37,161 @@ limitations under the License.
using namespace std;
/**
- * get status
+ * ping command
+ */
+trex_rpc_cmd_rc_e
+TrexRpcCmdPing::_run(const Json::Value &params, Json::Value &result) {
+
+ result["result"] = "ACK";
+ return (TREX_RPC_CMD_OK);
+}
+
+/**
+ * query command
+ */
+trex_rpc_cmd_rc_e
+TrexRpcCmdGetCmds::_run(const Json::Value &params, 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
-TrexRpcCmdGetStatus::_run(const Json::Value &params, Json::Value &result) {
+TrexRpcCmdGetVersion::_run(const Json::Value &params, Json::Value &result) {
Json::Value &section = result["result"];
#ifndef TREX_RPC_MOCK_SERVER
- section["general"]["version"] = VERSION_BUILD_NUM;
- section["general"]["build_date"] = get_build_date();
- section["general"]["build_time"] = get_build_time();
- section["general"]["built_by"] = VERSION_USER;
+ section["version"] = VERSION_BUILD_NUM;
+ section["build_date"] = get_build_date();
+ section["build_time"] = get_build_time();
+ section["built_by"] = VERSION_USER;
#else
- section["general"]["version"] = "v0.0";
- section["general"]["build_date"] = __DATE__;
- section["general"]["build_time"] = __TIME__;
- section["general"]["version_user"] = "MOCK";
+ section["version"] = "v0.0";
+ section["build_date"] = __DATE__;
+ section["build_time"] = __TIME__;
+ section["built_by"] = "MOCK";
#endif
- section["general"]["uptime"] = TrexRpcServer::get_server_uptime();
- section["general"]["owner"] = TrexRpcServer::get_owner();
+ 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;
+ }
- // ports
+ /* trim it */
+ int index = cpu_prefix.size() + 1;
+ while ( (line[index] == ' ') || (line[index] == ':') ) {
+ index++;
+ }
- section["ports"]["count"] = TrexStateless::get_instance().get_port_count();
+ 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 &params, Json::Value &result) {
+ string hostname;
+
+ TrexStateless & instance = TrexStateless::get_instance();
+
+ Json::Value &section = result["result"];
+
+ get_hostname(hostname);
+ section["hostname"] = hostname;
+
+ section["uptime"] = TrexRpcServer::get_server_uptime();
+
+ /* FIXME: core count */
+ section["dp_core_count"] = 1;
+ 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;
+
+ switch (port->get_state()) {
+ case TrexStatelessPort::PORT_STATE_DOWN:
+ section["ports"][i]["status"] = "down";
+ break;
+
+ case TrexStatelessPort::PORT_STATE_UP_IDLE:
+ section["ports"][i]["status"] = "idle";
+ break;
+
+ case TrexStatelessPort::PORT_STATE_TRANSMITTING:
+ section["ports"][i]["status"] = "transmitting";
+ break;
+ }
+
+ }
return (TREX_RPC_CMD_OK);
}
diff --git a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp
index b62d213b..41567509 100644
--- a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp
+++ b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp
@@ -63,6 +63,9 @@ TrexRpcCmdAddStream::_run(const Json::Value &params, Json::Value &result) {
/* allocate a new stream based on the type */
TrexStream *stream = allocate_new_stream(section, port_id, stream_id, result);
+ /* save this for future queries */
+ stream->store_stream_json(section);
+
/* some fields */
stream->m_enabled = parse_bool(section, "enabled", result);
stream->m_self_start = parse_bool(section, "self_start", result);
@@ -130,7 +133,7 @@ TrexRpcCmdAddStream::allocate_new_stream(const Json::Value &section, uint8_t por
if (type == "continuous") {
- uint32_t pps = parse_int(mode, "pps", result);
+ double pps = parse_double(mode, "pps", result);
stream = new TrexStreamContinuous(port_id, stream_id, pps);
} else if (type == "single_burst") {
@@ -413,30 +416,11 @@ TrexRpcCmdGetStream::_run(const Json::Value &params, Json::Value &result) {
generate_execute_err(result, ss.str());
}
- Json::Value stream_json;
-
- stream_json["enabled"] = stream->m_enabled;
- stream_json["self_start"] = stream->m_self_start;
-
- stream_json["isg"] = stream->m_isg_usec;
- stream_json["next_stream_id"] = stream->m_next_stream_id;
-
- stream_json["packet"]["binary"] = Json::arrayValue;
- for (int i = 0; i < stream->m_pkt.len; i++) {
- stream_json["packet"]["binary"].append(stream->m_pkt.binary[i]);
- }
-
- stream_json["packet"]["meta"] = stream->m_pkt.meta;
-
- if (TrexStreamContinuous *cont = dynamic_cast<TrexStreamContinuous *>(stream)) {
- stream_json["mode"]["type"] = "continuous";
- stream_json["mode"]["pps"] = cont->get_pps();
-
- }
-
- result["result"]["stream"] = stream_json;
+ /* return the stored stream json (instead of decoding it all over again) */
+ result["result"]["stream"] = stream->get_stream_json();
return (TREX_RPC_CMD_OK);
+
}
/***************************
@@ -455,17 +439,17 @@ TrexRpcCmdStartTraffic::_run(const Json::Value &params, Json::Value &result) {
TrexStatelessPort *port = TrexStateless::get_instance().get_port_by_id(port_id);
- TrexStatelessPort::traffic_rc_e rc = port->start_traffic();
+ TrexStatelessPort::rc_e rc = port->start_traffic();
- if (rc == TrexStatelessPort::TRAFFIC_OK) {
+ if (rc == TrexStatelessPort::RC_OK) {
result["result"] = "ACK";
} else {
std::stringstream ss;
switch (rc) {
- case TrexStatelessPort::TRAFFIC_ERR_ALREADY_STARTED:
- ss << "traffic has already started on that port";
+ case TrexStatelessPort::RC_ERR_BAD_STATE_FOR_OP:
+ ss << "bad state for operations: port is either transmitting traffic or down";
break;
- case TrexStatelessPort::TRAFFIC_ERR_NO_STREAMS:
+ case TrexStatelessPort::RC_ERR_NO_STREAMS:
ss << "no active streams on that port";
break;
default:
diff --git a/src/rpc-server/commands/trex_rpc_cmd_test.cpp b/src/rpc-server/commands/trex_rpc_cmd_test.cpp
index 3153317e..3cdddd31 100644
--- a/src/rpc-server/commands/trex_rpc_cmd_test.cpp
+++ b/src/rpc-server/commands/trex_rpc_cmd_test.cpp
@@ -21,7 +21,6 @@ limitations under the License.
#include "trex_rpc_cmds.h"
#include <iostream>
#include <sstream>
-#include <trex_rpc_cmds_table.h>
using namespace std;
@@ -50,32 +49,3 @@ TrexRpcCmdTestSub::_run(const Json::Value &params, Json::Value &result) {
return (TREX_RPC_CMD_OK);
}
-/**
- * ping command
- */
-trex_rpc_cmd_rc_e
-TrexRpcCmdPing::_run(const Json::Value &params, Json::Value &result) {
-
- result["result"] = "ACK";
- return (TREX_RPC_CMD_OK);
-}
-
-/**
- * query command
- */
-trex_rpc_cmd_rc_e
-TrexRpcCmdGetReg::_run(const Json::Value &params, 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);
-}
-
diff --git a/src/rpc-server/commands/trex_rpc_cmds.h b/src/rpc-server/commands/trex_rpc_cmds.h
index 32138195..643aa22d 100644
--- a/src/rpc-server/commands/trex_rpc_cmds.h
+++ b/src/rpc-server/commands/trex_rpc_cmds.h
@@ -35,7 +35,7 @@ class TrexStream;
* syntactic sugar for creating a simple command
*/
-#define TREX_RPC_CMD_DEFINE_EXTENED(class_name, cmd_name, param_count, needs_ownership, ext) \
+#define TREX_RPC_CMD_DEFINE_EXTENDED(class_name, cmd_name, param_count, needs_ownership, ext) \
class class_name : public TrexRpcCommand { \
public: \
class_name () : TrexRpcCommand(cmd_name, param_count, needs_ownership) {} \
@@ -44,7 +44,7 @@ class TrexStream;
ext \
}
-#define TREX_RPC_CMD_DEFINE(class_name, cmd_name, param_count, needs_ownership) TREX_RPC_CMD_DEFINE_EXTENED(class_name, cmd_name, param_count, needs_ownership, ;)
+#define TREX_RPC_CMD_DEFINE(class_name, cmd_name, param_count, needs_ownership) TREX_RPC_CMD_DEFINE_EXTENDED(class_name, cmd_name, param_count, needs_ownership, ;)
/**
* test cmds
@@ -55,9 +55,16 @@ TREX_RPC_CMD_DEFINE(TrexRpcCmdTestSub, "test_sub", 2, false);
/**
* general cmds
*/
-TREX_RPC_CMD_DEFINE(TrexRpcCmdPing, "ping", 0, false);
-TREX_RPC_CMD_DEFINE(TrexRpcCmdGetReg, "get_reg_cmds", 0, false);
-TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStatus, "get_status", 0, false);
+TREX_RPC_CMD_DEFINE(TrexRpcCmdPing, "ping", 0, false);
+TREX_RPC_CMD_DEFINE(TrexRpcCmdGetCmds, "get_supported_cmds", 0, false);
+TREX_RPC_CMD_DEFINE(TrexRpcCmdGetVersion, "get_version", 0, false);
+
+TREX_RPC_CMD_DEFINE_EXTENDED(TrexRpcCmdGetSysInfo, "get_system_info", 0, false,
+
+std::string get_cpu_model();
+void get_hostname(std::string &hostname);
+
+);
/**
* ownership
@@ -73,7 +80,7 @@ TREX_RPC_CMD_DEFINE(TrexRpcCmdRelease, "release", 0, true);
TREX_RPC_CMD_DEFINE(TrexRpcCmdRemoveAllStreams, "remove_all_streams", 1, true);
TREX_RPC_CMD_DEFINE(TrexRpcCmdRemoveStream, "remove_stream", 2, true);
-TREX_RPC_CMD_DEFINE_EXTENED(TrexRpcCmdAddStream, "add_stream", 3, true,
+TREX_RPC_CMD_DEFINE_EXTENDED(TrexRpcCmdAddStream, "add_stream", 3, true,
/* extended part */
TrexStream * allocate_new_stream(const Json::Value &section, uint8_t port_id, uint32_t stream_id, Json::Value &result);
@@ -92,4 +99,6 @@ TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStream, "get_stream", 2, true);
TREX_RPC_CMD_DEFINE(TrexRpcCmdStartTraffic, "start_traffic", 1, true);
TREX_RPC_CMD_DEFINE(TrexRpcCmdStopTraffic, "stop_traffic", 1, true);
+
+
#endif /* __TREX_RPC_CMD_H__ */
diff --git a/src/rpc-server/trex_rpc_cmds_table.cpp b/src/rpc-server/trex_rpc_cmds_table.cpp
index f968bb74..170f0de1 100644
--- a/src/rpc-server/trex_rpc_cmds_table.cpp
+++ b/src/rpc-server/trex_rpc_cmds_table.cpp
@@ -34,8 +34,9 @@ TrexRpcCommandsTable::TrexRpcCommandsTable() {
/* general */
register_command(new TrexRpcCmdPing());
- register_command(new TrexRpcCmdGetReg());
- register_command(new TrexRpcCmdGetStatus());
+ register_command(new TrexRpcCmdGetCmds());
+ register_command(new TrexRpcCmdGetVersion());
+ register_command(new TrexRpcCmdGetSysInfo());
register_command(new TrexRpcCmdGetOwner());
register_command(new TrexRpcCmdAcquire());
register_command(new TrexRpcCmdRelease());
diff --git a/src/rpc-server/trex_rpc_jsonrpc_v2_parser.cpp b/src/rpc-server/trex_rpc_jsonrpc_v2_parser.cpp
index 928baca6..9d9de53a 100644
--- a/src/rpc-server/trex_rpc_jsonrpc_v2_parser.cpp
+++ b/src/rpc-server/trex_rpc_jsonrpc_v2_parser.cpp
@@ -225,3 +225,11 @@ std::string TrexJsonRpcV2Parser::pretty_json_str(const std::string &json_str) {
return writer.write(value);
}
+void
+TrexJsonRpcV2Parser::generate_common_error(Json::Value &json, const std::string &specific_err) {
+ JsonRpcError err(Json::Value::null, JSONRPC_V2_ERR_INTERNAL_ERROR, specific_err, true);
+
+ err.execute(json);
+
+}
+
diff --git a/src/rpc-server/trex_rpc_jsonrpc_v2_parser.h b/src/rpc-server/trex_rpc_jsonrpc_v2_parser.h
index ebffaeb7..0563f21d 100644
--- a/src/rpc-server/trex_rpc_jsonrpc_v2_parser.h
+++ b/src/rpc-server/trex_rpc_jsonrpc_v2_parser.h
@@ -80,6 +80,15 @@ public:
void parse(std::vector<TrexJsonRpcV2ParsedObject *> &commands);
/**
+ * will generate a valid JSON RPC v2 error message with
+ * generic error code and message
+ *
+ * @author imarom (16-Sep-15)
+ *
+ */
+ static void generate_common_error(Json::Value &json, const std::string &specific_err);
+
+ /**
* *tries* to generate a pretty string from JSON
* if json_str is not a valid JSON string
* it will duplicate the source
diff --git a/src/rpc-server/trex_rpc_req_resp_server.cpp b/src/rpc-server/trex_rpc_req_resp_server.cpp
index c4d9dfdb..3d52686c 100644
--- a/src/rpc-server/trex_rpc_req_resp_server.cpp
+++ b/src/rpc-server/trex_rpc_req_resp_server.cpp
@@ -82,6 +82,13 @@ void TrexRpcServerReqRes::_rpc_thread_cb() {
}
}
+ if (msg_size >= sizeof(m_msg_buffer)) {
+ std::stringstream ss;
+ ss << "RPC request of '" << msg_size << "' exceeds maximum message size which is '" << sizeof(m_msg_buffer) << "'";
+ handle_server_error(ss.str());
+ continue;
+ }
+
/* transform it to a string */
std::string request((const char *)m_msg_buffer, msg_size);
@@ -145,3 +152,23 @@ void TrexRpcServerReqRes::handle_request(const std::string &request) {
zmq_send(m_socket, response_str.c_str(), response_str.size(), 0);
}
+
+/**
+ * handles a server error
+ *
+ */
+void
+TrexRpcServerReqRes::handle_server_error(const std::string &specific_err) {
+ Json::FastWriter writer;
+ Json::Value response;
+
+ /* generate error */
+ TrexJsonRpcV2Parser::generate_common_error(response, specific_err);
+
+ /* write the JSON to string and sever on ZMQ */
+ std::string response_str = writer.write(response);
+
+ verbose_json("Server Replied: ", response_str);
+
+ zmq_send(m_socket, response_str.c_str(), response_str.size(), 0);
+}
diff --git a/src/rpc-server/trex_rpc_req_resp_server.h b/src/rpc-server/trex_rpc_req_resp_server.h
index f12d0540..7c1d66d1 100644
--- a/src/rpc-server/trex_rpc_req_resp_server.h
+++ b/src/rpc-server/trex_rpc_req_resp_server.h
@@ -39,9 +39,11 @@ protected:
void _stop_rpc_thread();
private:
+
void handle_request(const std::string &request);
+ void handle_server_error(const std::string &specific_err);
- static const int RPC_MAX_MSG_SIZE = 2048;
+ static const int RPC_MAX_MSG_SIZE = (20 * 1024);
void *m_context;
void *m_socket;
uint8_t m_msg_buffer[RPC_MAX_MSG_SIZE];