1558ce764Simarom/*
2558ce764SimaromCopyright (c) 2015-2015 Cisco Systems, Inc.
3558ce764Simarom
4558ce764SimaromLicensed under the Apache License, Version 2.0 (the "License");
5558ce764Simaromyou may not use this file except in compliance with the License.
6558ce764SimaromYou may obtain a copy of the License at
7558ce764Simarom
8558ce764Simarom    http://www.apache.org/licenses/LICENSE-2.0
9558ce764Simarom
10558ce764SimaromUnless required by applicable law or agreed to in writing, software
11558ce764Simaromdistributed under the License is distributed on an "AS IS" BASIS,
12558ce764SimaromWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13558ce764SimaromSee the License for the specific language governing permissions and
14558ce764Simaromlimitations under the License.
15558ce764Simarom*/
16558ce764Simarom
17558ce764Simarom#include "trex_port_attr.h"
18558ce764Simarom#include "bp_sim.h"
19558ce764Simarom
2034cb66c9SimaromLayerConfigMAC::LayerConfigMAC(uint8_t port_id) {
2134cb66c9Simarom    /* use this for container (DP copies from here) */
2234cb66c9Simarom    m_src_mac = CGlobalInfo::m_options.m_mac_addr[port_id].u.m_mac.src;
2334cb66c9Simarom    m_dst_mac = CGlobalInfo::m_options.m_mac_addr[port_id].u.m_mac.dest;
2425be508cSimarom
2534cb66c9Simarom    m_state = STATE_UNCONFIGRED;
2625be508cSimarom}
2725be508cSimarom
2834cb66c9SimaromJson::Value
2934cb66c9SimaromLayerConfigMAC::to_json() const {
3034cb66c9Simarom    Json::Value output;
3134cb66c9Simarom
3234cb66c9Simarom    output["src"] = utl_macaddr_to_str(m_src_mac);
3334cb66c9Simarom    output["dst"] = utl_macaddr_to_str(m_dst_mac);
3434cb66c9Simarom
3534cb66c9Simarom    switch (m_state) {
3634cb66c9Simarom    case STATE_CONFIGURED:
3734cb66c9Simarom        output["state"] = "configured";
3834cb66c9Simarom        break;
3934cb66c9Simarom    case STATE_UNCONFIGRED:
4034cb66c9Simarom        output["state"] = "unconfigured";
4134cb66c9Simarom        break;
4234cb66c9Simarom
4334cb66c9Simarom    default:
4434cb66c9Simarom        assert(0);
4534cb66c9Simarom    }
46051a334bSimarom
47051a334bSimarom
4834cb66c9Simarom    return output;
4925be508cSimarom}
5025be508cSimarom
5134cb66c9SimaromJson::Value
5234cb66c9SimaromLayerConfigIPv4::to_json() const {
5334cb66c9Simarom    Json::Value output;
5425be508cSimarom
5534cb66c9Simarom    switch (m_state) {
5634cb66c9Simarom    case STATE_NONE:
5734cb66c9Simarom        output["state"] = "none";
5825be508cSimarom        break;
5925be508cSimarom
6034cb66c9Simarom    case STATE_UNRESOLVED:
6134cb66c9Simarom        output["state"] = "unresolved";
6225be508cSimarom        break;
6334cb66c9Simarom
6434cb66c9Simarom    case STATE_RESOLVED:
6534cb66c9Simarom        output["state"] = "resolved";
6625be508cSimarom        break;
6734cb66c9Simarom
6825be508cSimarom    default:
6925be508cSimarom        assert(0);
7025be508cSimarom    }
7134cb66c9Simarom
7234cb66c9Simarom    if (m_state != STATE_NONE) {
7334cb66c9Simarom        output["src"] = utl_uint32_to_ipv4(m_src_ipv4);
7434cb66c9Simarom        output["dst"] = utl_uint32_to_ipv4(m_dst_ipv4);
7534cb66c9Simarom    }
7634cb66c9Simarom
7734cb66c9Simarom    return output;
7834cb66c9Simarom}
7934cb66c9Simarom
8034cb66c9Simaromvoid LayerConfig::set_l2_mode(const uint8_t *dst_mac) {
8134cb66c9Simarom    /* set dst MAC */
8234cb66c9Simarom    m_l2_config.set_dst(dst_mac);
8334cb66c9Simarom    m_l2_config.set_state(LayerConfigMAC::STATE_CONFIGURED);
8434cb66c9Simarom
8534cb66c9Simarom    /* remove any IPv4 configuration*/
8634cb66c9Simarom    m_l3_ipv4_config.set_state(LayerConfigIPv4::STATE_NONE);
8734cb66c9Simarom}
8825be508cSimarom
8934cb66c9Simaromvoid LayerConfig::set_l3_mode(uint32_t src_ipv4, uint32_t dst_ipv4) {
9034cb66c9Simarom
9134cb66c9Simarom    /* L2 config */
9234cb66c9Simarom    m_l2_config.set_state(LayerConfigMAC::STATE_UNCONFIGRED);
9334cb66c9Simarom
9434cb66c9Simarom    /* L3 config */
9534cb66c9Simarom    m_l3_ipv4_config.set_src(src_ipv4);
9634cb66c9Simarom    m_l3_ipv4_config.set_dst(dst_ipv4);
9734cb66c9Simarom    m_l3_ipv4_config.set_state(LayerConfigIPv4::STATE_UNRESOLVED);
98e46e3f59Simarom}
99e46e3f59Simarom
10034cb66c9Simaromvoid LayerConfig::set_l3_mode(uint32_t src_ipv4, uint32_t dst_ipv4, const uint8_t *resolved_mac) {
10134cb66c9Simarom
10234cb66c9Simarom    /* L2 config */
10334cb66c9Simarom    m_l2_config.set_dst(resolved_mac);
10434cb66c9Simarom    m_l2_config.set_state(LayerConfigMAC::STATE_CONFIGURED);
10534cb66c9Simarom
10634cb66c9Simarom    /* L3 config */
10734cb66c9Simarom    m_l3_ipv4_config.set_src(src_ipv4);
10834cb66c9Simarom    m_l3_ipv4_config.set_dst(dst_ipv4);
10934cb66c9Simarom    m_l3_ipv4_config.set_state(LayerConfigIPv4::STATE_RESOLVED);
110558ce764Simarom}
111558ce764Simarom
1120c458152Simaromvoid
11334cb66c9SimaromLayerConfig::on_link_down() {
11434cb66c9Simarom
115836996ffSimarom    /* on IPv4 only - when the state is resolved, move to unresolved and invalidate the MAC */
11634cb66c9Simarom    if (m_l3_ipv4_config.get_state() == LayerConfigIPv4::STATE_RESOLVED) {
117836996ffSimarom        m_l2_config.set_state(LayerConfigMAC::STATE_UNCONFIGRED);
11834cb66c9Simarom        m_l3_ipv4_config.set_state(LayerConfigIPv4::STATE_UNRESOLVED);
11934cb66c9Simarom    }
1200c458152Simarom}
12134cb66c9Simarom
12234cb66c9SimaromJson::Value
12334cb66c9SimaromLayerConfig::to_json() const {
12434cb66c9Simarom    Json::Value output;
1250c458152Simarom
12634cb66c9Simarom    output["ether"] = m_l2_config.to_json();
12734cb66c9Simarom    output["ipv4"]  = m_l3_ipv4_config.to_json();
12834cb66c9Simarom
12934cb66c9Simarom    return output;
13034cb66c9Simarom}
13134cb66c9Simarom
132558ce764Simaromstd::string
133558ce764SimaromTRexPortAttr::get_rx_filter_mode() const {
134558ce764Simarom    switch (m_rx_filter_mode) {
135558ce764Simarom    case RX_FILTER_MODE_ALL:
136558ce764Simarom        return "all";
137558ce764Simarom    case RX_FILTER_MODE_HW:
138558ce764Simarom        return "hw";
139558ce764Simarom    default:
140558ce764Simarom        assert(0);
141558ce764Simarom    }
142558ce764Simarom}
143558ce764Simarom
14434cb66c9Simaromvoid
14534cb66c9SimaromTRexPortAttr::on_link_down() {
14634cb66c9Simarom    m_layer_cfg.on_link_down();
14734cb66c9Simarom}
148558ce764Simarom
149558ce764Simaromvoid
150558ce764SimaromTRexPortAttr::to_json(Json::Value &output) {
151558ce764Simarom
152558ce764Simarom    output["promiscuous"]["enabled"] = get_promiscuous();
15339000f46SYaroslav Brustinov    output["multicast"]["enabled"]   = get_multicast();
154558ce764Simarom    output["link"]["up"]             = is_link_up();
155c2e0fc65Simarom    output["speed"]                  = get_link_speed() / 1000; // make sure we have no cards of less than 1 Gbps
156558ce764Simarom    output["rx_filter_mode"]         = get_rx_filter_mode();
157558ce764Simarom
158558ce764Simarom    int mode;
159558ce764Simarom    get_flow_ctrl(mode);
160558ce764Simarom    output["fc"]["mode"] = mode;
161558ce764Simarom
16234cb66c9Simarom    output["layer_cfg"] = m_layer_cfg.to_json();
163558ce764Simarom}
164558ce764Simarom
165558ce764Simarom
166