From 3adfe9c3c8a6e2ce1cdc5bd1a673e428c18fa64b Mon Sep 17 00:00:00 2001 From: imarom Date: Sun, 6 Sep 2015 17:55:00 +0300 Subject: added more commands to RPC server --- src/rpc-server/commands/trex_rpc_cmd_stream.cpp | 128 ++++++++++++++++++++++-- src/rpc-server/commands/trex_rpc_cmds.h | 9 +- 2 files changed, 129 insertions(+), 8 deletions(-) (limited to 'src/rpc-server/commands') diff --git a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp index 6dfebe6d..16889413 100644 --- a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp +++ b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp @@ -34,6 +34,9 @@ using namespace std; trex_rpc_cmd_rc_e TrexRpcCmdAddStream::_run(const Json::Value ¶ms, Json::Value &result) { + uint8_t port_id = parse_int(params, "port_id", result); + uint32_t stream_id = parse_int(params, "stream_id", result); + const Json::Value §ion = parse_object(params, "stream", result); /* get the type of the stream */ @@ -41,7 +44,7 @@ TrexRpcCmdAddStream::_run(const Json::Value ¶ms, Json::Value &result) { string type = parse_string(mode, "type", result); /* allocate a new stream based on the type */ - TrexStream *stream = allocate_new_stream(section, result); + TrexStream *stream = allocate_new_stream(section, port_id, stream_id, result); /* some fields */ stream->m_enabled = parse_bool(section, "enabled", result); @@ -97,10 +100,7 @@ TrexRpcCmdAddStream::_run(const Json::Value ¶ms, Json::Value &result) { TrexStream * -TrexRpcCmdAddStream::allocate_new_stream(const Json::Value §ion, Json::Value &result) { - - uint8_t port_id = parse_int(section, "port_id", result); - uint32_t stream_id = parse_int(section, "stream_id", result); +TrexRpcCmdAddStream::allocate_new_stream(const Json::Value §ion, uint8_t port_id, uint32_t stream_id, Json::Value &result) { TrexStream *stream; @@ -200,6 +200,7 @@ TrexRpcCmdRemoveStream::_run(const Json::Value ¶ms, Json::Value &result) { } port->get_stream_table()->remove_stream(stream); + delete stream; result["result"] = "ACK"; @@ -231,7 +232,7 @@ TrexRpcCmdRemoveAllStreams::_run(const Json::Value ¶ms, Json::Value &result) /*************************** * get all streams configured - * on specific port + * on a specific port * **************************/ trex_rpc_cmd_rc_e @@ -261,3 +262,118 @@ TrexRpcCmdGetStreamList::_run(const Json::Value ¶ms, Json::Value &result) { return (TREX_RPC_CMD_OK); } +/*************************** + * get stream by id + * on a specific port + * + **************************/ +trex_rpc_cmd_rc_e +TrexRpcCmdGetStream::_run(const Json::Value ¶ms, Json::Value &result) { + uint8_t port_id = parse_byte(params, "port_id", result); + + uint32_t stream_id = parse_int(params, "stream_id", result); + + if (port_id >= TrexStateless::get_instance().get_port_count()) { + std::stringstream ss; + ss << "invalid port id - should be between 0 and " << (int)TrexStateless::get_instance().get_port_count() - 1; + generate_execute_err(result, ss.str()); + } + + TrexStatelessPort *port = TrexStateless::get_instance().get_port_by_id(port_id); + + TrexStream *stream = port->get_stream_table()->get_stream_by_id(stream_id); + + if (!stream) { + std::stringstream ss; + ss << "stream id " << stream_id << " on port " << (int)port_id << " does not exists"; + 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(stream)) { + stream_json["mode"]["type"] = "continuous"; + stream_json["mode"]["pps"] = cont->get_pps(); + + } + + result["result"]["stream"] = stream_json; + + return (TREX_RPC_CMD_OK); +} + +/*************************** + * start traffic on port + * + **************************/ +trex_rpc_cmd_rc_e +TrexRpcCmdStartTraffic::_run(const Json::Value ¶ms, Json::Value &result) { + uint8_t port_id = parse_byte(params, "port_id", result); + + if (port_id >= TrexStateless::get_instance().get_port_count()) { + std::stringstream ss; + ss << "invalid port id - should be between 0 and " << (int)TrexStateless::get_instance().get_port_count() - 1; + generate_execute_err(result, ss.str()); + } + + TrexStatelessPort *port = TrexStateless::get_instance().get_port_by_id(port_id); + + TrexStatelessPort::traffic_rc_e rc = port->start_traffic(); + + if (rc == TrexStatelessPort::TRAFFIC_OK) { + result["result"] = "ACK"; + } else { + std::stringstream ss; + switch (rc) { + case TrexStatelessPort::TRAFFIC_ERR_ALREADY_STARTED: + ss << "traffic has already started on that port"; + break; + case TrexStatelessPort::TRAFFIC_ERR_NO_STREAMS: + ss << "no active streams on that port"; + break; + default: + ss << "failed to start traffic"; + break; + } + + generate_execute_err(result, ss.str()); + } + + return (TREX_RPC_CMD_OK); +} + +/*************************** + * start traffic on port + * + **************************/ +trex_rpc_cmd_rc_e +TrexRpcCmdStopTraffic::_run(const Json::Value ¶ms, Json::Value &result) { + uint8_t port_id = parse_byte(params, "port_id", result); + + if (port_id >= TrexStateless::get_instance().get_port_count()) { + std::stringstream ss; + ss << "invalid port id - should be between 0 and " << (int)TrexStateless::get_instance().get_port_count() - 1; + generate_execute_err(result, ss.str()); + } + + TrexStatelessPort *port = TrexStateless::get_instance().get_port_by_id(port_id); + + port->stop_traffic(); + result["result"] = "ACK"; + + 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 e9666f21..428d48c1 100644 --- a/src/rpc-server/commands/trex_rpc_cmds.h +++ b/src/rpc-server/commands/trex_rpc_cmds.h @@ -65,10 +65,10 @@ TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStatus, "get_status", 0); TREX_RPC_CMD_DEFINE(TrexRpcCmdRemoveAllStreams, "remove_all_streams", 1); TREX_RPC_CMD_DEFINE(TrexRpcCmdRemoveStream, "remove_stream", 2); -TREX_RPC_CMD_DEFINE_EXTENED(TrexRpcCmdAddStream, "add_stream", 1, +TREX_RPC_CMD_DEFINE_EXTENED(TrexRpcCmdAddStream, "add_stream", 3, /* extended part */ -TrexStream * allocate_new_stream(const Json::Value §ion, Json::Value &result); +TrexStream * allocate_new_stream(const Json::Value §ion, uint8_t port_id, uint32_t stream_id, Json::Value &result); void validate_stream(const TrexStream *stream, Json::Value &result); ); @@ -76,4 +76,9 @@ void validate_stream(const TrexStream *stream, Json::Value &result); TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStreamList, "get_stream_list", 1); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStream, "get_stream", 2); + +TREX_RPC_CMD_DEFINE(TrexRpcCmdStartTraffic, "start_traffic", 1); +TREX_RPC_CMD_DEFINE(TrexRpcCmdStopTraffic, "stop_traffic", 1); + #endif /* __TREX_RPC_CMD_H__ */ -- cgit 1.2.3-korg