diff options
author | 2016-04-03 18:19:20 +0300 | |
---|---|---|
committer | 2016-04-04 09:49:54 +0300 | |
commit | 4eacb570cf24927de536d23671f50609f1a9ffa5 (patch) | |
tree | 83b8dcd86994c7668a054413d0ba0449a1cf2816 /src | |
parent | 0eb15b2e851b5f50669633678143c5a1d3a7d95b (diff) |
API classes (versions)
Diffstat (limited to 'src')
-rwxr-xr-x | src/common/basic_utils.cpp | 23 | ||||
-rwxr-xr-x | src/common/basic_utils.h | 4 | ||||
-rw-r--r-- | src/rpc-server/commands/trex_rpc_cmd_general.cpp | 44 | ||||
-rw-r--r-- | src/rpc-server/commands/trex_rpc_cmds.h | 77 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_cmd.cpp | 46 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_cmd_api.h | 39 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_cmds_table.cpp | 1 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_exception_api.h | 10 | ||||
-rw-r--r-- | src/sim/trex_sim_stateless.cpp | 1 | ||||
-rw-r--r-- | src/stateless/cp/trex_api_class.h | 110 | ||||
-rw-r--r-- | src/stateless/cp/trex_exception.h | 41 | ||||
-rw-r--r-- | src/stateless/cp/trex_stateless.cpp | 3 | ||||
-rw-r--r-- | src/stateless/cp/trex_stateless.h | 38 | ||||
-rw-r--r-- | src/stateless/cp/trex_stateless_port.cpp | 21 | ||||
-rw-r--r-- | src/stateless/cp/trex_stateless_port.h | 4 |
15 files changed, 364 insertions, 98 deletions
diff --git a/src/common/basic_utils.cpp b/src/common/basic_utils.cpp index 34c37755..4f5578a6 100755 --- a/src/common/basic_utils.cpp +++ b/src/common/basic_utils.cpp @@ -17,6 +17,7 @@ limitations under the License. #include <ctype.h> #include <stdio.h> #include <string> +#include <sstream> bool utl_is_file_exists (const std::string& name) { if (FILE *file = fopen(name.c_str(), "r")) { @@ -175,3 +176,25 @@ void utl_macaddr_to_str(const uint8_t *macaddr, std::string &output) { } } + +/** + * generate a random connection handler + * + */ +std::string +utl_generate_random_str(unsigned int &seed, int len) { + std::stringstream ss; + + static const char alphanum[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + + /* generate 8 bytes of random handler */ + for (int i = 0; i < len; ++i) { + ss << alphanum[rand_r(&seed) % (sizeof(alphanum) - 1)]; + } + + return (ss.str()); +} + diff --git a/src/common/basic_utils.h b/src/common/basic_utils.h index 77282eea..63e858ab 100755 --- a/src/common/basic_utils.h +++ b/src/common/basic_utils.h @@ -21,8 +21,6 @@ limitations under the License. #include <stdio.h> #include <string> - - /** * the round must be power 2 e.g 2,4,8... * @@ -87,6 +85,8 @@ bool utl_is_file_exists (const std::string& name) ; void utl_macaddr_to_str(const uint8_t *macaddr, std::string &output); +std::string utl_generate_random_str(unsigned int &seed, int len); + #endif diff --git a/src/rpc-server/commands/trex_rpc_cmd_general.cpp b/src/rpc-server/commands/trex_rpc_cmd_general.cpp index f054c0ed..f7a23188 100644 --- a/src/rpc-server/commands/trex_rpc_cmd_general.cpp +++ b/src/rpc-server/commands/trex_rpc_cmd_general.cpp @@ -38,6 +38,50 @@ limitations under the License. using namespace std; /** + * API sync + */ +trex_rpc_cmd_rc_e +TrexRpcCmdAPISync::_run(const Json::Value ¶ms, Json::Value &result) { + const Json::Value &api_vers = parse_array(params, "api_vers", result); + + Json::Value api_ver_rc = Json::arrayValue; + + /* for every element in the list - generate the appropirate API handler */ + for (const auto api_ver : api_vers) { + Json::Value single_rc; + + /* only those are supported */ + const std::string type = parse_choice(api_ver, "type", {"core"}, result); + + int major = parse_int(api_ver, "major", result); + int minor = parse_int(api_ver, "minor", result); + APIClass::type_e api_type; + + /* decode type of API */ + if (type == "core") { + api_type = APIClass::API_CLASS_TYPE_CORE; + } + + single_rc["type"] = type; + + /* this section might throw exception in case versions do not match */ + try { + single_rc["api_h"] = get_stateless_obj()->verify_api(api_type, major, minor); + + } catch (const TrexAPIException &e) { + generate_execute_err(result, e.what()); + } + + /* add to the response */ + api_ver_rc.append(single_rc); + } + + result["result"]["api_vers"] = api_ver_rc; + + return (TREX_RPC_CMD_OK); +} + +/** * ping command */ trex_rpc_cmd_rc_e diff --git a/src/rpc-server/commands/trex_rpc_cmds.h b/src/rpc-server/commands/trex_rpc_cmds.h index dbf90fef..428bdd7b 100644 --- a/src/rpc-server/commands/trex_rpc_cmds.h +++ b/src/rpc-server/commands/trex_rpc_cmds.h @@ -36,33 +36,39 @@ class TrexStream; * syntactic sugar for creating a simple command */ -#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) {} \ - protected: \ - virtual trex_rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result); \ - ext \ +#define TREX_RPC_CMD_DEFINE_EXTENDED(class_name, cmd_name, param_count, needs_ownership, api_type, ext) \ + class class_name : public TrexRpcCommand { \ + public: \ + class_name () : TrexRpcCommand(cmd_name, param_count, needs_ownership, api_type) {} \ + protected: \ + virtual trex_rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result); \ + ext \ } -#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, ;) +#define TREX_RPC_CMD_DEFINE(class_name, cmd_name, param_count, needs_ownership, api_type) TREX_RPC_CMD_DEFINE_EXTENDED(class_name, cmd_name, param_count, needs_ownership, api_type, ;) /** * test cmds */ -TREX_RPC_CMD_DEFINE(TrexRpcCmdTestAdd, "test_add", 2, false); -TREX_RPC_CMD_DEFINE(TrexRpcCmdTestSub, "test_sub", 2, false); +TREX_RPC_CMD_DEFINE(TrexRpcCmdTestAdd, "test_add", 2, false, APIClass::API_CLASS_TYPE_NO_API); +TREX_RPC_CMD_DEFINE(TrexRpcCmdTestSub, "test_sub", 2, false, APIClass::API_CLASS_TYPE_NO_API); + +/** + * api_sync command always present and valid and also ping.... + */ +TREX_RPC_CMD_DEFINE(TrexRpcCmdAPISync, "api_sync", 1, false, APIClass::API_CLASS_TYPE_NO_API); +TREX_RPC_CMD_DEFINE(TrexRpcCmdPing, "ping", 0, false, APIClass::API_CLASS_TYPE_NO_API); /** * general cmds */ -TREX_RPC_CMD_DEFINE(TrexRpcCmdPing, "ping", 0, false); -TREX_RPC_CMD_DEFINE(TrexRpcPublishNow, "publish_now", 2, false); -TREX_RPC_CMD_DEFINE(TrexRpcCmdGetCmds, "get_supported_cmds", 0, false); -TREX_RPC_CMD_DEFINE(TrexRpcCmdGetVersion, "get_version", 0, false); -TREX_RPC_CMD_DEFINE(TrexRpcCmdGetActivePGIds, "get_active_pgids",0, false); -TREX_RPC_CMD_DEFINE_EXTENDED(TrexRpcCmdGetSysInfo, "get_system_info", 0, false, +TREX_RPC_CMD_DEFINE(TrexRpcPublishNow, "publish_now", 2, false, APIClass::API_CLASS_TYPE_CORE); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetCmds, "get_supported_cmds", 0, false, APIClass::API_CLASS_TYPE_CORE); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetVersion, "get_version", 0, false, APIClass::API_CLASS_TYPE_CORE); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetActivePGIds, "get_active_pgids", 0, false, APIClass::API_CLASS_TYPE_CORE); + +TREX_RPC_CMD_DEFINE_EXTENDED(TrexRpcCmdGetSysInfo, "get_system_info", 0, false, APIClass::API_CLASS_TYPE_CORE, std::string get_cpu_model(); void get_hostname(std::string &hostname); @@ -72,25 +78,25 @@ void get_hostname(std::string &hostname); /** * ownership */ -TREX_RPC_CMD_DEFINE(TrexRpcCmdGetOwner, "get_owner", 1, false); -TREX_RPC_CMD_DEFINE(TrexRpcCmdAcquire, "acquire", 4, false); -TREX_RPC_CMD_DEFINE(TrexRpcCmdRelease, "release", 1, true); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetOwner, "get_owner", 1, false, APIClass::API_CLASS_TYPE_CORE); +TREX_RPC_CMD_DEFINE(TrexRpcCmdAcquire, "acquire", 4, false, APIClass::API_CLASS_TYPE_CORE); +TREX_RPC_CMD_DEFINE(TrexRpcCmdRelease, "release", 1, true, APIClass::API_CLASS_TYPE_CORE); /** * port commands */ -TREX_RPC_CMD_DEFINE(TrexRpcCmdGetPortStats, "get_port_stats", 1, false); -TREX_RPC_CMD_DEFINE(TrexRpcCmdGetPortStatus, "get_port_status", 1, false); -TREX_RPC_CMD_DEFINE(TrexRpcCmdSetPortAttr, "set_port_attr", 3, false); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetPortStats, "get_port_stats", 1, false, APIClass::API_CLASS_TYPE_CORE); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetPortStatus, "get_port_status", 1, false, APIClass::API_CLASS_TYPE_CORE); +TREX_RPC_CMD_DEFINE(TrexRpcCmdSetPortAttr, "set_port_attr", 3, false, APIClass::API_CLASS_TYPE_CORE); /** * stream cmds */ -TREX_RPC_CMD_DEFINE(TrexRpcCmdRemoveAllStreams, "remove_all_streams", 1, true); -TREX_RPC_CMD_DEFINE(TrexRpcCmdRemoveStream, "remove_stream", 2, true); +TREX_RPC_CMD_DEFINE(TrexRpcCmdRemoveAllStreams, "remove_all_streams", 1, true, APIClass::API_CLASS_TYPE_CORE); +TREX_RPC_CMD_DEFINE(TrexRpcCmdRemoveStream, "remove_stream", 2, true, APIClass::API_CLASS_TYPE_CORE); -TREX_RPC_CMD_DEFINE_EXTENDED(TrexRpcCmdAddStream, "add_stream", 3, true, +TREX_RPC_CMD_DEFINE_EXTENDED(TrexRpcCmdAddStream, "add_stream", 3, true, APIClass::API_CLASS_TYPE_CORE, /* extended part */ std::unique_ptr<TrexStream> allocate_new_stream(const Json::Value §ion, uint8_t port_id, uint32_t stream_id, Json::Value &result); @@ -107,21 +113,22 @@ void parse_vm_instr_write_mask_flow_var(const Json::Value &inst, std::unique_ptr ); -TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStreamList, "get_stream_list", 1, false); -TREX_RPC_CMD_DEFINE(TrexRpcCmdGetAllStreams, "get_all_streams", 1, false); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStreamList, "get_stream_list", 1, false, APIClass::API_CLASS_TYPE_CORE); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetAllStreams, "get_all_streams", 1, false, APIClass::API_CLASS_TYPE_CORE); -TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStream, "get_stream", 3, false); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStream, "get_stream", 3, false, APIClass::API_CLASS_TYPE_CORE); -TREX_RPC_CMD_DEFINE(TrexRpcCmdStartTraffic, "start_traffic", 4, true); -TREX_RPC_CMD_DEFINE(TrexRpcCmdStopTraffic, "stop_traffic", 1, true); -TREX_RPC_CMD_DEFINE(TrexRpcCmdRemoveRXFilters, "remove_rx_filters", 1, true); -TREX_RPC_CMD_DEFINE(TrexRpcCmdPauseTraffic, "pause_traffic", 1, true); -TREX_RPC_CMD_DEFINE(TrexRpcCmdResumeTraffic, "resume_traffic", 1, true); +TREX_RPC_CMD_DEFINE(TrexRpcCmdStartTraffic, "start_traffic", 4, true, APIClass::API_CLASS_TYPE_CORE); +TREX_RPC_CMD_DEFINE(TrexRpcCmdStopTraffic, "stop_traffic", 1, true, APIClass::API_CLASS_TYPE_CORE); +TREX_RPC_CMD_DEFINE(TrexRpcCmdRemoveRXFilters, "remove_rx_filters", 1, true, APIClass::API_CLASS_TYPE_CORE); +TREX_RPC_CMD_DEFINE(TrexRpcCmdPauseTraffic, "pause_traffic", 1, true, APIClass::API_CLASS_TYPE_CORE); +TREX_RPC_CMD_DEFINE(TrexRpcCmdResumeTraffic, "resume_traffic", 1, true, APIClass::API_CLASS_TYPE_CORE); -TREX_RPC_CMD_DEFINE(TrexRpcCmdUpdateTraffic, "update_traffic", 3, true); +TREX_RPC_CMD_DEFINE(TrexRpcCmdUpdateTraffic, "update_traffic", 3, true, APIClass::API_CLASS_TYPE_CORE); -TREX_RPC_CMD_DEFINE(TrexRpcCmdValidate, "validate", 2, false); +TREX_RPC_CMD_DEFINE(TrexRpcCmdValidate, "validate", 2, false, APIClass::API_CLASS_TYPE_CORE); #endif /* __TREX_RPC_CMD_H__ */ + diff --git a/src/rpc-server/trex_rpc_cmd.cpp b/src/rpc-server/trex_rpc_cmd.cpp index caf161e3..902e63c7 100644 --- a/src/rpc-server/trex_rpc_cmd.cpp +++ b/src/rpc-server/trex_rpc_cmd.cpp @@ -23,6 +23,32 @@ limitations under the License. #include <trex_stateless.h> #include <trex_stateless_port.h> +/** + * method name and params + * + */ +TrexRpcCommand::TrexRpcCommand(const std::string &method_name, + int param_count, + bool needs_ownership, + APIClass::type_e type) : m_name(method_name), + m_param_count(param_count), + m_needs_ownership(needs_ownership) { + + /* if needs ownership - another field is needed (handler) */ + if (m_needs_ownership) { + m_param_count++; + } + + /* API verification */ + m_api_type = type; + + if (type != APIClass::API_CLASS_TYPE_NO_API) { + m_api_handler = get_stateless_obj()->get_api_handler(type); + m_param_count++; + } + +} + trex_rpc_cmd_rc_e TrexRpcCommand::run(const Json::Value ¶ms, Json::Value &result) { trex_rpc_cmd_rc_e rc; @@ -30,12 +56,18 @@ TrexRpcCommand::run(const Json::Value ¶ms, Json::Value &result) { /* the internal run can throw a parser error / other error */ try { - check_param_count(params, m_param_count, result); + /* verify API handler is correct (version mismatch) */ + if ( (m_api_type != APIClass::API_CLASS_TYPE_NO_API) && !g_test_override_api ) { + verify_api_handler(params, result); + } + /* verify ownership */ if (m_needs_ownership && !g_test_override_ownership) { verify_ownership(params, result); } + check_param_count(params, m_param_count, result); + /* run the command itself*/ rc = _run(params, result); @@ -72,6 +104,17 @@ TrexRpcCommand::verify_ownership(const Json::Value ¶ms, Json::Value &result) } } +void +TrexRpcCommand::verify_api_handler(const Json::Value ¶ms, Json::Value &result) { + std::string api_handler = parse_string(params, "api_h", result); + + if (m_api_handler != api_handler) { + std::stringstream ss; + ss << "API verification failed - API handler provided mismatch for class: '" << APIClass::type_to_name(m_api_type) << "'"; + generate_execute_err(result, ss.str()); + } +} + uint8_t TrexRpcCommand::parse_port(const Json::Value ¶ms, Json::Value &result) { uint8_t port_id = parse_byte(params, "port_id", result); @@ -281,3 +324,4 @@ TrexRpcCommand::generate_execute_err(Json::Value &result, const std::string &msg * by default this is off */ bool TrexRpcCommand::g_test_override_ownership = false; +bool TrexRpcCommand::g_test_override_api = false; diff --git a/src/rpc-server/trex_rpc_cmd_api.h b/src/rpc-server/trex_rpc_cmd_api.h index 7e694768..25920c6c 100644 --- a/src/rpc-server/trex_rpc_cmd_api.h +++ b/src/rpc-server/trex_rpc_cmd_api.h @@ -27,6 +27,8 @@ limitations under the License. #include <json/json.h> #include <trex_rpc_exception_api.h> +#include "trex_api_class.h" + /** * describe different types of rc for run() */ @@ -68,16 +70,10 @@ public: /** * method name and params */ - TrexRpcCommand(const std::string &method_name, int param_count, bool needs_ownership) : - m_name(method_name), - m_param_count(param_count), - m_needs_ownership(needs_ownership) { - - /* if needs ownership - another field is needed (handler) */ - if (m_needs_ownership) { - m_param_count++; - } - } + TrexRpcCommand(const std::string &method_name, + int param_count, + bool needs_ownership, + APIClass::type_e type); /** * entry point for executing RPC command @@ -99,6 +95,10 @@ public: g_test_override_ownership = enable; } + static void test_set_override_api(bool enable) { + g_test_override_api = enable; + } + virtual ~TrexRpcCommand() {} protected: @@ -131,11 +131,18 @@ protected: void check_param_count(const Json::Value ¶ms, int expected, Json::Value &result); /** + * verify API handler + * + */ + void verify_api_handler(const Json::Value ¶ms, Json::Value &result); + + /** * verify ownership * */ void verify_ownership(const Json::Value ¶ms, Json::Value &result); + /** * validate port id * @@ -360,11 +367,13 @@ protected: const char * json_type_to_name(const Json::Value &value); /* RPC command name */ - std::string m_name; - int m_param_count; - bool m_needs_ownership; - - static bool g_test_override_ownership; + std::string m_name; + int m_param_count; + bool m_needs_ownership; + std::string m_api_handler; + APIClass::type_e m_api_type; + static bool g_test_override_ownership; + static bool g_test_override_api; }; #endif /* __TREX_RPC_CMD_API_H__ */ diff --git a/src/rpc-server/trex_rpc_cmds_table.cpp b/src/rpc-server/trex_rpc_cmds_table.cpp index c9b41595..924503f2 100644 --- a/src/rpc-server/trex_rpc_cmds_table.cpp +++ b/src/rpc-server/trex_rpc_cmds_table.cpp @@ -33,6 +33,7 @@ TrexRpcCommandsTable::TrexRpcCommandsTable() { /* general */ + register_command(new TrexRpcCmdAPISync()); register_command(new TrexRpcCmdPing()); register_command(new TrexRpcPublishNow()); register_command(new TrexRpcCmdGetCmds()); diff --git a/src/rpc-server/trex_rpc_exception_api.h b/src/rpc-server/trex_rpc_exception_api.h index e349b980..ebc9b411 100644 --- a/src/rpc-server/trex_rpc_exception_api.h +++ b/src/rpc-server/trex_rpc_exception_api.h @@ -25,17 +25,19 @@ limitations under the License. #include <string> #include <stdexcept> +#include "trex_exception.h" + /** * generic exception for RPC errors * */ -class TrexRpcException : public std::runtime_error -{ +class TrexRpcException : public TrexException { + public: - TrexRpcException() : std::runtime_error("") { + TrexRpcException() : TrexException("") { } - TrexRpcException(const std::string &what) : std::runtime_error(what) { + TrexRpcException(const std::string &what) : TrexException(what) { } }; diff --git a/src/sim/trex_sim_stateless.cpp b/src/sim/trex_sim_stateless.cpp index ffe377f4..fa13401d 100644 --- a/src/sim/trex_sim_stateless.cpp +++ b/src/sim/trex_sim_stateless.cpp @@ -117,6 +117,7 @@ SimStateless::SimStateless() { /* override ownership checks */ TrexRpcCommand::test_set_override_ownership(true); + TrexRpcCommand::test_set_override_api(true); } diff --git a/src/stateless/cp/trex_api_class.h b/src/stateless/cp/trex_api_class.h new file mode 100644 index 00000000..78933d23 --- /dev/null +++ b/src/stateless/cp/trex_api_class.h @@ -0,0 +1,110 @@ +/* + 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. +*/ +#ifndef __TREX_API_CLASS_H__ +#define __TREX_API_CLASS_H__ + +#include <assert.h> + +#include "common/basic_utils.h" +#include "trex_exception.h" + +/** + * API exception + * + * @author imarom (03-Apr-16) + */ +class TrexAPIException : public TrexException { +public: + TrexAPIException(const std::string &what) : TrexException(what) { + } +}; + +/** + * define an API class + * + * @author imarom (03-Apr-16) + */ +class APIClass { +public: + + enum type_e { + API_CLASS_TYPE_CORE = 0, + API_CLASS_TYPE_MAX, + + API_CLASS_TYPE_NO_API + }; + + static const char * type_to_name(type_e type) { + switch (type) { + case API_CLASS_TYPE_CORE: + return "core"; + default: + assert(0); + } + } + + APIClass() { + /* invalid */ + m_type = API_CLASS_TYPE_MAX; + } + + void init(type_e type, int major, int minor) { + m_type = type; + m_major = major; + m_minor = minor; + + unsigned int seed = time(NULL); + m_handler = utl_generate_random_str(seed, 8); + } + + std::string & verify_api(int major, int minor) { + std::stringstream ss; + ss << "API type '" << type_to_name(m_type) << "': "; + + assert(m_type < API_CLASS_TYPE_MAX); + + /* for now a simple major check */ + if (major < m_major) { + ss << "server has a major newer API version - server: '" << m_major << "', client: '" << major << "'"; + throw TrexAPIException(ss.str()); + } + + if (major > m_major) { + ss << "server has an older API version - server: '" << m_major << "', client: '" << major << "'"; + throw TrexAPIException(ss.str()); + } + + return get_api_handler(); + } + + std::string & get_api_handler() { + return m_handler; + } + +private: + type_e m_type; + int m_major; + int m_minor; + std::string m_handler; + +}; + +#endif /* __TREX_API_CLASS_H__ */ diff --git a/src/stateless/cp/trex_exception.h b/src/stateless/cp/trex_exception.h new file mode 100644 index 00000000..b9e20761 --- /dev/null +++ b/src/stateless/cp/trex_exception.h @@ -0,0 +1,41 @@ +/* + 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. +*/ +#ifndef __TREX_EXCEPTION_H__ +#define __TREX_EXCEPTION_H__ + +#include <stdexcept> +#include <string> + +/** + * generic exception for errors + * TODO: move this to a better place + */ +class TrexException : public std::runtime_error +{ +public: + TrexException() : std::runtime_error("") { + + } + TrexException(const std::string &what) : std::runtime_error(what) { + } +}; + +#endif /* __TREX_EXCEPTION_H__ */ diff --git a/src/stateless/cp/trex_stateless.cpp b/src/stateless/cp/trex_stateless.cpp index 9df57a50..f6f81b96 100644 --- a/src/stateless/cp/trex_stateless.cpp +++ b/src/stateless/cp/trex_stateless.cpp @@ -53,6 +53,8 @@ TrexStateless::TrexStateless(const TrexStatelessCfg &cfg) { m_platform_api = cfg.m_platform_api; m_publisher = cfg.m_publisher; + /* API core version */ + m_api_classes[APIClass::API_CLASS_TYPE_CORE].init(APIClass::API_CLASS_TYPE_CORE, 1, 0); } /** @@ -175,3 +177,4 @@ TrexStateless::generate_publish_snapshot(std::string &snapshot) { snapshot = writer.write(root); } + diff --git a/src/stateless/cp/trex_stateless.h b/src/stateless/cp/trex_stateless.h index 7db86174..b506da61 100644 --- a/src/stateless/cp/trex_stateless.h +++ b/src/stateless/cp/trex_stateless.h @@ -27,27 +27,18 @@ limitations under the License. #include <mutex> -#include <trex_stream.h> -#include <trex_stateless_port.h> -#include <trex_rpc_server_api.h> -#include <publisher/trex_publisher.h> +#include "trex_stream.h" +#include "trex_stateless_port.h" +#include "trex_rpc_server_api.h" -#include <flow_stat.h> -#include <internal_api/trex_platform_api.h> +#include "publisher/trex_publisher.h" +#include "internal_api/trex_platform_api.h" -/** - * generic exception for errors - * TODO: move this to a better place - */ -class TrexException : public std::runtime_error -{ -public: - TrexException() : std::runtime_error("") { +#include "flow_stat.h" - } - TrexException(const std::string &what) : std::runtime_error(what) { - } -}; + +#include "trex_exception.h" +#include "trex_api_class.h" class TrexStatelessPort; @@ -81,6 +72,7 @@ public: } m_stats; }; + /** * config object for stateless object * @@ -167,6 +159,14 @@ public: return m_rpc_server; } + const std::string & verify_api(APIClass::type_e type, int major, int minor) { + return m_api_classes[type].verify_api(major, minor); + } + + const std::string & get_api_handler(APIClass::type_e type) { + return m_api_classes[type].get_api_handler(); + } + CFlowStatRuleMgr m_rx_flow_stat; protected: @@ -187,6 +187,8 @@ protected: TrexPublisher *m_publisher; + /* API */ + APIClass m_api_classes[APIClass::API_CLASS_TYPE_MAX]; }; /** diff --git a/src/stateless/cp/trex_stateless_port.cpp b/src/stateless/cp/trex_stateless_port.cpp index 6a33fcee..2239f3f6 100644 --- a/src/stateless/cp/trex_stateless_port.cpp +++ b/src/stateless/cp/trex_stateless_port.cpp @@ -781,26 +781,5 @@ TrexPortOwner::TrexPortOwner() { m_seed = time(NULL); } -/** - * generate a random connection handler - * - */ -std::string -TrexPortOwner::generate_handler() { - std::stringstream ss; - - static const char alphanum[] = - "0123456789" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz"; - - /* generate 8 bytes of random handler */ - for (int i = 0; i < 8; ++i) { - ss << alphanum[rand_r(&m_seed) % (sizeof(alphanum) - 1)]; - } - - return (ss.str()); -} - const std::string TrexPortOwner::g_unowned_name = "<FREE>"; const std::string TrexPortOwner::g_unowned_handler = ""; diff --git a/src/stateless/cp/trex_stateless_port.h b/src/stateless/cp/trex_stateless_port.h index 7aa3bfa8..2167e735 100644 --- a/src/stateless/cp/trex_stateless_port.h +++ b/src/stateless/cp/trex_stateless_port.h @@ -21,6 +21,7 @@ limitations under the License. #ifndef __TREX_STATELESS_PORT_H__ #define __TREX_STATELESS_PORT_H__ +#include "common/basic_utils.h" #include "internal_api/trex_platform_api.h" #include "trex_dp_port_events.h" #include "trex_stream.h" @@ -65,7 +66,7 @@ public: m_owner_name = owner_name; /* internal data */ - m_handler = generate_handler(); + m_handler = utl_generate_random_str(m_seed, 8); m_is_free = false; } @@ -83,7 +84,6 @@ public: private: - std::string generate_handler(); /* is this port owned by someone ? */ bool m_is_free; |