summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYaroslav Brustinov <ybrustin@cisco.com>2016-09-27 09:08:57 +0300
committerYaroslav Brustinov <ybrustin@cisco.com>2016-09-27 09:08:57 +0300
commit1e6b2bf6001275bdd2ab0691f335bbb7966bf619 (patch)
treecd601fa51b2645f9a21ed1cbad5c76da13e01cc3 /src
parent9ff1f808edcc9289d297b3a6cf34b1e5590ecf85 (diff)
/etc/trex_cfg.yaml allow MAC as string "12:34:56:78:9a:bc" etc.
dpdk_setup_ports: fix create config in case of VM & passthrough
Diffstat (limited to 'src')
-rwxr-xr-xsrc/common/c_common.h2
-rwxr-xr-xsrc/platform_cfg.cpp43
-rwxr-xr-xsrc/utl_yaml.cpp22
-rwxr-xr-xsrc/utl_yaml.h3
4 files changed, 57 insertions, 13 deletions
diff --git a/src/common/c_common.h b/src/common/c_common.h
index 3e43644f..8a970e6e 100755
--- a/src/common/c_common.h
+++ b/src/common/c_common.h
@@ -50,3 +50,5 @@ typedef void* c_pvoid;
#endif
#endif
+
+#define ASSERT_MSG(cond, msg) if (!(cond)) {std::cerr << msg << std::endl; exit(-1);}
diff --git a/src/platform_cfg.cpp b/src/platform_cfg.cpp
index e11b0fb5..64bbb71b 100755
--- a/src/platform_cfg.cpp
+++ b/src/platform_cfg.cpp
@@ -24,6 +24,7 @@ limitations under the License.
#include <stdlib.h>
#include "common/basic_utils.h"
#include "platform_cfg.h"
+#include "utl_yaml.h"
void CPlatformMemoryYamlInfo::reset(){
int i;
@@ -171,31 +172,47 @@ void CMacYamlInfo::Dump(FILE *fd){
return;
}
if (m_src_base.size() != 6) {
- fprintf(fd,"ERROR in dest mac addr \n");
+ fprintf(fd,"ERROR in src mac addr \n");
return;
}
fprintf (fd," src : ");
- dump_mac_vector( m_dest_base,fd);
- fprintf (fd," dest : ");
dump_mac_vector( m_src_base,fd);
+ fprintf (fd," dest : ");
+ dump_mac_vector( m_dest_base,fd);
}
void operator >> (const YAML::Node& node, CMacYamlInfo & mac_info) {
+ uint32_t fi;
+ bool res;
+ std::string mac_str;
+
const YAML::Node& dmac = node["dest_mac"];
- for(unsigned i=0;i<dmac.size();i++) {
- uint32_t fi;
- const YAML::Node & node =dmac;
- node[i] >> fi;
- mac_info.m_dest_base.push_back(fi);
+ if (dmac.Type() == YAML::NodeType::Sequence) { // [1,2,3,4,5,6]
+ ASSERT_MSG(dmac.size() == 6, "Array of dest MAC should have 6 elements.");
+ for(unsigned i=0;i<dmac.size();i++) {
+ dmac[i] >> fi;
+ mac_info.m_dest_base.push_back(fi);
+ }
+ }
+ else if (dmac.Type() == YAML::NodeType::Scalar) { // "12:34:56:78:9a:bc"
+ dmac >> mac_str;
+ res = mac2vect(mac_str, mac_info.m_dest_base);
+ ASSERT_MSG(res && mac_info.m_dest_base.size() == 6, "String of dest MAC should be in format '12:34:56:78:9a:bc'.");
}
const YAML::Node& smac = node["src_mac"];
- for(unsigned i=0;i<dmac.size();i++) {
- uint32_t fi;
- const YAML::Node & node =smac;
- node[i] >> fi;
- mac_info.m_src_base.push_back(fi);
+ if (smac.Type() == YAML::NodeType::Sequence) {
+ ASSERT_MSG(smac.size() == 6, "Array of src MAC should have 6 elements.");
+ for(unsigned i=0;i<smac.size();i++) {
+ smac[i] >> fi;
+ mac_info.m_src_base.push_back(fi);
+ }
+ }
+ else if (smac.Type() == YAML::NodeType::Scalar) {
+ smac >> mac_str;
+ res = mac2vect(mac_str, mac_info.m_src_base);
+ ASSERT_MSG(res && mac_info.m_src_base.size() == 6, "String of src MAC should be in format '12:34:56:78:9a:bc'.");
}
}
diff --git a/src/utl_yaml.cpp b/src/utl_yaml.cpp
index 8352e887..864c86f8 100755
--- a/src/utl_yaml.cpp
+++ b/src/utl_yaml.cpp
@@ -155,6 +155,28 @@ static bool mac2uint64(const std::string &mac_str, uint64_t &mac_num) {
return true;
}
+bool mac2vect(const std::string &mac_str, std::vector<uint8_t> &mac) {
+ std::vector<std::string> tokens;
+
+ split_str_by_delimiter(mac_str, ':', tokens);
+ if (tokens.size() != 6) {
+ return false;
+ }
+
+ for (int i = 0; i < 6 ; i++) {
+ char *endptr = NULL;
+ unsigned long octet = strtoul(tokens[i].c_str(), &endptr, 16);
+
+ if ( (*endptr != 0) || (octet > 0xff) ) {
+ return false;
+ }
+
+ mac.push_back(octet);
+ }
+
+ return true;
+}
+
/************************
* YAML Parser Wrapper
*
diff --git a/src/utl_yaml.h b/src/utl_yaml.h
index 59104b21..ed7d66d6 100755
--- a/src/utl_yaml.h
+++ b/src/utl_yaml.h
@@ -23,6 +23,7 @@ limitations under the License.
#include <stdint.h>
+#include <vector>
#include <yaml-cpp/yaml.h>
@@ -39,6 +40,8 @@ bool utl_yaml_read_uint16(const YAML::Node& node,
const std::string &name,
uint16_t & val);
+bool mac2vect(const std::string &mac_str, std::vector<uint8_t> &mac);
+
/* a thin wrapper to customize errors */
class YAMLParserWrapper {
public: