summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorimarom <imarom@cisco.com>2016-11-28 16:47:41 +0200
committerimarom <imarom@cisco.com>2016-11-28 16:47:41 +0200
commit25be508c9922f558552b950fb25599826b1b8308 (patch)
tree70b4f12457bbf9c07617767f2602713c7401f385 /src
parent537f5831c4400dea7fa15032c4cd6bd2fae86bb1 (diff)
self code review
Signed-off-by: imarom <imarom@cisco.com>
Diffstat (limited to 'src')
-rw-r--r--src/main_dpdk.cpp6
-rw-r--r--src/rpc-server/commands/trex_rpc_cmd_general.cpp6
-rw-r--r--src/trex_port_attr.cpp72
-rwxr-xr-xsrc/trex_port_attr.h104
4 files changed, 102 insertions, 86 deletions
diff --git a/src/main_dpdk.cpp b/src/main_dpdk.cpp
index 2aab7f5d..1341def4 100644
--- a/src/main_dpdk.cpp
+++ b/src/main_dpdk.cpp
@@ -3345,9 +3345,9 @@ void CGlobalTRex::pre_test() {
uint32_t dg = CGlobalInfo::m_options.m_ip_cfg[port_id].get_def_gw();
const uint8_t *dst_mac = CGlobalInfo::m_options.m_mac_addr[port_id].u.m_mac.dest;
if (dg) {
- m_ports[port_id].get_port_attr()->get_dest().set_dest_ipv4(dg, dst_mac);
+ m_ports[port_id].get_port_attr()->get_dest().set_dest(dg, dst_mac);
} else {
- m_ports[port_id].get_port_attr()->get_dest().set_dest_mac(dst_mac);
+ m_ports[port_id].get_port_attr()->get_dest().set_dest(dst_mac);
}
}
@@ -4932,7 +4932,7 @@ bool CPhyEthIF::Create(uint8_t portid) {
/* for now set as unresolved IPv4 destination */
uint32_t dest_ipv4 = CGlobalInfo::m_options.m_ip_cfg[m_port_id].get_def_gw();
if (dest_ipv4) {
- m_port_attr->get_dest().set_dest_ipv4(dest_ipv4);
+ m_port_attr->get_dest().set_dest(dest_ipv4);
}
return true;
diff --git a/src/rpc-server/commands/trex_rpc_cmd_general.cpp b/src/rpc-server/commands/trex_rpc_cmd_general.cpp
index 11dd99bd..ccdcbd8c 100644
--- a/src/rpc-server/commands/trex_rpc_cmd_general.cpp
+++ b/src/rpc-server/commands/trex_rpc_cmd_general.cpp
@@ -385,10 +385,10 @@ TrexRpcCmdSetPortAttr::parse_dest(const Json::Value &msg, uint8_t port_id, Json:
uint8_t mac[6];
if (utl_ipv4_to_uint32(addr.c_str(), ipv4_addr)) {
- port_attr->get_dest().set_dest_ipv4(ipv4_addr);
+ port_attr->get_dest().set_dest(ipv4_addr);
} else if (utl_str_to_macaddr(addr, mac)) {
- port_attr->get_dest().set_dest_mac(mac);
+ port_attr->get_dest().set_dest(mac);
} else {
std::stringstream ss;
@@ -850,7 +850,7 @@ TrexRpcCmdSetARPRes::_run(const Json::Value &params, Json::Value &result) {
generate_parse_err(result, ss.str());
}
- port->getPortAttrObj()->get_dest().set_dest_ipv4(ipv4_addr, mac);
+ port->getPortAttrObj()->get_dest().set_dest(ipv4_addr, mac);
return (TREX_RPC_CMD_OK);
diff --git a/src/trex_port_attr.cpp b/src/trex_port_attr.cpp
index 20b441e2..b215a245 100644
--- a/src/trex_port_attr.cpp
+++ b/src/trex_port_attr.cpp
@@ -17,13 +17,77 @@ limitations under the License.
#include "trex_port_attr.h"
#include "bp_sim.h"
-const uint8_t DestAttr::g_dummy_mac[6] = {0x0,0x0,0x0,0x1,0x0,0x0};
-
-
DestAttr::DestAttr(uint8_t port_id) {
m_port_id = port_id;
-
+
m_mac = CGlobalInfo::m_options.m_mac_addr[port_id].u.m_mac.dest;
+ m_type = DEST_TYPE_MAC;
+
+ /* save the default */
+ memcpy(m_default_mac, m_mac, 6);
+}
+
+
+/**
+ * set dest as an IPv4 unresolved
+ */
+void
+DestAttr::set_dest(uint32_t ipv4) {
+ assert(ipv4 != 0);
+
+ m_ipv4 = ipv4;
+ memset(m_mac, 0, 6); // just to be on the safe side
+ m_type = DEST_TYPE_IPV4_UNRESOLVED;
+}
+
+/**
+ * set dest as a resolved IPv4
+ */
+void
+DestAttr::set_dest(uint32_t ipv4, const uint8_t *mac) {
+ assert(ipv4 != 0);
+
+ m_ipv4 = ipv4;
+ memcpy(m_mac, mac, 6);
+ m_type = DEST_TYPE_IPV4;
+}
+
+/**
+ * dest dest as MAC
+ *
+ */
+void
+DestAttr::set_dest(const uint8_t *mac) {
+
+ m_ipv4 = 0;
+ memcpy(m_mac, mac, 6);
+ m_type = DEST_TYPE_MAC;
+}
+
+void
+DestAttr::to_json(Json::Value &output) const {
+ switch (m_type) {
+
+ case DEST_TYPE_IPV4:
+ output["type"] = "ipv4";
+ output["ipv4"] = utl_uint32_to_ipv4(m_ipv4);
+ output["arp"] = utl_macaddr_to_str(m_mac);
+ break;
+
+ case DEST_TYPE_IPV4_UNRESOLVED:
+ output["type"] = "ipv4_u";
+ output["ipv4"] = utl_uint32_to_ipv4(m_ipv4);
+ break;
+
+ case DEST_TYPE_MAC:
+ output["type"] = "mac";
+ output["mac"] = utl_macaddr_to_str(m_mac);
+ break;
+
+ default:
+ assert(0);
+ }
+
}
const uint8_t *
diff --git a/src/trex_port_attr.h b/src/trex_port_attr.h
index 95050d1b..3cb9beff 100755
--- a/src/trex_port_attr.h
+++ b/src/trex_port_attr.h
@@ -31,79 +31,50 @@ limitations under the License.
*
*/
class DestAttr {
-private:
- static const uint8_t g_dummy_mac[6];
+
public:
DestAttr(uint8_t port_id);
+ /**
+ * dest can be either MAC IPv4, or IPv4 unresolved
+ */
enum dest_type_e {
- DEST_TYPE_IPV4 = 1,
- DEST_TYPE_MAC = 2
+ DEST_TYPE_IPV4 = 1,
+ DEST_TYPE_IPV4_UNRESOLVED,
+ DEST_TYPE_MAC,
};
/**
* set dest as an IPv4 unresolved
*/
- void set_dest_ipv4(uint32_t ipv4) {
- assert(ipv4 != 0);
-
- m_src_ipv4 = ipv4;
- memset(m_mac, 0, 6);
- m_type = DEST_TYPE_IPV4;
- }
+ void set_dest(uint32_t ipv4);
/**
- * set dest as a resolved IPv4
+ * set dest as a resolved IPv4
*/
- void set_dest_ipv4(uint32_t ipv4, const uint8_t *mac) {
- assert(ipv4 != 0);
-
- m_src_ipv4 = ipv4;
- memcpy(m_mac, mac, 6);
- m_type = DEST_TYPE_IPV4;
-
- }
-
+ void set_dest(uint32_t ipv4, const uint8_t *mac);
+
/**
- * dest dest as MAC
- *
+ * set dest as a plain MAC
*/
- void set_dest_mac(const uint8_t *mac) {
-
- m_src_ipv4 = 0;
- memcpy(m_mac, mac, 6);
- m_type = DEST_TYPE_MAC;
- }
-
+ void set_dest(const uint8_t *mac);
+
+ /**
+ * return true if destination is resolved
+ */
bool is_resolved() const {
- if (m_type == DEST_TYPE_MAC) {
- return true;
- }
-
- for (int i = 0; i < 6; i++) {
- if (m_mac[i] != 0) {
- return true;
- }
- }
-
- /* all zeroes - non resolved */
- return false;
+ return (m_type != DEST_TYPE_IPV4_UNRESOLVED);
}
/**
- * get the dest mac
- * if no MAC is configured and dest was not resolved
- * will return a dummy
+ * get the dest mac
+ * if the dest is not resolved
+ * it will return the default MAC
*/
const uint8_t *get_dest_mac() {
-
- if (is_resolved()) {
- return m_mac;
- } else {
- return g_dummy_mac;
- }
+ return m_mac;
}
/**
@@ -113,39 +84,20 @@ public:
void on_link_down() {
if (m_type == DEST_TYPE_IPV4) {
/* reset the IPv4 dest with no resolution */
- set_dest_ipv4(m_src_ipv4);
+ set_dest(m_ipv4);
}
}
- void to_json(Json::Value &output) {
- switch (m_type) {
-
- case DEST_TYPE_IPV4:
- output["type"] = "ipv4";
- output["addr"] = utl_uint32_to_ipv4(m_src_ipv4);
- if (is_resolved()) {
- output["arp"] = utl_macaddr_to_str(m_mac);
- } else {
- output["arp"] = Json::nullValue;
- }
- break;
-
- case DEST_TYPE_MAC:
- output["type"] = "mac";
- output["addr"] = utl_macaddr_to_str(m_mac);
- break;
-
- default:
- assert(0);
- }
-
- }
+ void to_json(Json::Value &output) const;
private:
- uint32_t m_src_ipv4;
+ uint32_t m_ipv4;
uint8_t *m_mac;
dest_type_e m_type;
uint8_t m_port_id;
+
+private:
+ uint8_t m_default_mac[6];
};