diff options
author | imarom <imarom@cisco.com> | 2016-12-25 12:54:09 +0200 |
---|---|---|
committer | imarom <imarom@cisco.com> | 2016-12-25 13:50:56 +0200 |
commit | 34cb66c9f06c7a43e68a17b4a8802f8d34298a65 (patch) | |
tree | aa6b081213e5715cf09a18503cc7b0a20168c6fc /src/trex_port_attr.cpp | |
parent | 549f2750ab22db5695a8d29575c136ab06e37235 (diff) |
refactor layer config on prot attributes
added guard against unconfigured MAC when going up with empty config
file
Signed-off-by: imarom <imarom@cisco.com>
Diffstat (limited to 'src/trex_port_attr.cpp')
-rw-r--r-- | src/trex_port_attr.cpp | 174 |
1 files changed, 93 insertions, 81 deletions
diff --git a/src/trex_port_attr.cpp b/src/trex_port_attr.cpp index 2a68fcb9..4c5ac9e4 100644 --- a/src/trex_port_attr.cpp +++ b/src/trex_port_attr.cpp @@ -17,96 +17,118 @@ limitations under the License. #include "trex_port_attr.h" #include "bp_sim.h" -DestAttr::DestAttr(uint8_t port_id) { - m_port_id = port_id; +LayerConfigMAC::LayerConfigMAC(uint8_t port_id) { + /* use this for container (DP copies from here) */ + m_src_mac = CGlobalInfo::m_options.m_mac_addr[port_id].u.m_mac.src; + m_dst_mac = CGlobalInfo::m_options.m_mac_addr[port_id].u.m_mac.dest; - 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; + m_state = STATE_UNCONFIGRED; } -/** - * set dest as a resolved IPv4 - */ -void -DestAttr::set_dest(uint32_t ipv4, const uint8_t *mac) { - assert(ipv4 != 0); - - m_ipv4 = ipv4; +Json::Value +LayerConfigMAC::to_json() const { + Json::Value output; + + output["src"] = utl_macaddr_to_str(m_src_mac); + output["dst"] = utl_macaddr_to_str(m_dst_mac); + + switch (m_state) { + case STATE_CONFIGURED: + output["state"] = "configured"; + break; + case STATE_UNCONFIGRED: + output["state"] = "unconfigured"; + break; + + default: + assert(0); + } - /* source might be the same as dest (this shadows the datapath memory) */ - memmove(m_mac, mac, 6); - m_type = DEST_TYPE_IPV4; -} - -/** - * dest dest as MAC - * - */ -void -DestAttr::set_dest(const uint8_t *mac) { - - m_ipv4 = 0; - /* source might be the same as dest (this shadows the datapath memory) */ - memmove(m_mac, mac, 6); - m_type = DEST_TYPE_MAC; + return output; } -void -DestAttr::to_json(Json::Value &output) const { - switch (m_type) { +Json::Value +LayerConfigIPv4::to_json() const { + Json::Value output; - case DEST_TYPE_IPV4: - output["type"] = "ipv4"; - output["ipv4"] = utl_uint32_to_ipv4(m_ipv4); - output["arp"] = utl_macaddr_to_str(m_mac); + switch (m_state) { + case STATE_NONE: + output["state"] = "none"; break; - case DEST_TYPE_IPV4_UNRESOLVED: - output["type"] = "ipv4_u"; - output["ipv4"] = utl_uint32_to_ipv4(m_ipv4); + case STATE_UNRESOLVED: + output["state"] = "unresolved"; break; - - case DEST_TYPE_MAC: - output["type"] = "mac"; - output["mac"] = utl_macaddr_to_str(m_mac); + + case STATE_RESOLVED: + output["state"] = "resolved"; break; - + default: assert(0); } + + if (m_state != STATE_NONE) { + output["src"] = utl_uint32_to_ipv4(m_src_ipv4); + output["dst"] = utl_uint32_to_ipv4(m_dst_ipv4); + } + + return output; +} + +void LayerConfig::set_l2_mode(const uint8_t *dst_mac) { + /* set dst MAC */ + m_l2_config.set_dst(dst_mac); + m_l2_config.set_state(LayerConfigMAC::STATE_CONFIGURED); + + /* remove any IPv4 configuration*/ + m_l3_ipv4_config.set_state(LayerConfigIPv4::STATE_NONE); +} +void LayerConfig::set_l3_mode(uint32_t src_ipv4, uint32_t dst_ipv4) { + + /* L2 config */ + m_l2_config.set_state(LayerConfigMAC::STATE_UNCONFIGRED); + + /* L3 config */ + m_l3_ipv4_config.set_src(src_ipv4); + m_l3_ipv4_config.set_dst(dst_ipv4); + m_l3_ipv4_config.set_state(LayerConfigIPv4::STATE_UNRESOLVED); } -const uint8_t * -TRexPortAttr::get_src_mac() const { - return CGlobalInfo::m_options.get_src_mac_addr(m_port_id); +void LayerConfig::set_l3_mode(uint32_t src_ipv4, uint32_t dst_ipv4, const uint8_t *resolved_mac) { + + /* L2 config */ + m_l2_config.set_dst(resolved_mac); + m_l2_config.set_state(LayerConfigMAC::STATE_CONFIGURED); + + /* L3 config */ + m_l3_ipv4_config.set_src(src_ipv4); + m_l3_ipv4_config.set_dst(dst_ipv4); + m_l3_ipv4_config.set_state(LayerConfigIPv4::STATE_RESOLVED); } void -TRexPortAttr::set_src_ipv4(uint32_t addr) { - m_src_ipv4 = addr; +LayerConfig::on_link_down() { + + m_l2_config.set_state(LayerConfigMAC::STATE_UNCONFIGRED); - /* when IP source changes - consider this as link down */ - m_dest.on_link_down(); + if (m_l3_ipv4_config.get_state() == LayerConfigIPv4::STATE_RESOLVED) { + m_l3_ipv4_config.set_state(LayerConfigIPv4::STATE_UNRESOLVED); + } } + +Json::Value +LayerConfig::to_json() const { + Json::Value output; + output["ether"] = m_l2_config.to_json(); + output["ipv4"] = m_l3_ipv4_config.to_json(); + + return output; +} + std::string TRexPortAttr::get_rx_filter_mode() const { switch (m_rx_filter_mode) { @@ -119,34 +141,24 @@ TRexPortAttr::get_rx_filter_mode() const { } } +void +TRexPortAttr::on_link_down() { + m_layer_cfg.on_link_down(); +} void TRexPortAttr::to_json(Json::Value &output) { - output["src_mac"] = utl_macaddr_to_str(get_src_mac()); output["promiscuous"]["enabled"] = get_promiscuous(); output["link"]["up"] = is_link_up(); output["speed"] = get_link_speed() / 1000; // make sure we have no cards of less than 1 Gbps output["rx_filter_mode"] = get_rx_filter_mode(); - if (get_src_ipv4() != 0) { - output["src_ipv4"] = utl_uint32_to_ipv4(get_src_ipv4()); - } else { - output["src_ipv4"] = Json::nullValue; - } - - int mode; get_flow_ctrl(mode); output["fc"]["mode"] = mode; - m_dest.to_json(output["dest"]); - + output["layer_cfg"] = m_layer_cfg.to_json(); } -void -TRexPortAttr::update_src_dst_mac(uint8_t *raw_pkt) const { - memcpy(raw_pkt, m_dest.get_dest_mac(), 6); - memcpy(raw_pkt + 6, get_src_mac(), 6); -} |