1/*
2Copyright (c) 2015-2015 Cisco Systems, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#include "trex_port_attr.h"
18#include "bp_sim.h"
19
20LayerConfigMAC::LayerConfigMAC(uint8_t port_id) {
21    /* use this for container (DP copies from here) */
22    m_src_mac = CGlobalInfo::m_options.m_mac_addr[port_id].u.m_mac.src;
23    m_dst_mac = CGlobalInfo::m_options.m_mac_addr[port_id].u.m_mac.dest;
24
25    m_state = STATE_UNCONFIGRED;
26}
27
28Json::Value
29LayerConfigMAC::to_json() const {
30    Json::Value output;
31
32    output["src"] = utl_macaddr_to_str(m_src_mac);
33    output["dst"] = utl_macaddr_to_str(m_dst_mac);
34
35    switch (m_state) {
36    case STATE_CONFIGURED:
37        output["state"] = "configured";
38        break;
39    case STATE_UNCONFIGRED:
40        output["state"] = "unconfigured";
41        break;
42
43    default:
44        assert(0);
45    }
46
47
48    return output;
49}
50
51Json::Value
52LayerConfigIPv4::to_json() const {
53    Json::Value output;
54
55    switch (m_state) {
56    case STATE_NONE:
57        output["state"] = "none";
58        break;
59
60    case STATE_UNRESOLVED:
61        output["state"] = "unresolved";
62        break;
63
64    case STATE_RESOLVED:
65        output["state"] = "resolved";
66        break;
67
68    default:
69        assert(0);
70    }
71
72    if (m_state != STATE_NONE) {
73        output["src"] = utl_uint32_to_ipv4(m_src_ipv4);
74        output["dst"] = utl_uint32_to_ipv4(m_dst_ipv4);
75    }
76
77    return output;
78}
79
80void LayerConfig::set_l2_mode(const uint8_t *dst_mac) {
81    /* set dst MAC */
82    m_l2_config.set_dst(dst_mac);
83    m_l2_config.set_state(LayerConfigMAC::STATE_CONFIGURED);
84
85    /* remove any IPv4 configuration*/
86    m_l3_ipv4_config.set_state(LayerConfigIPv4::STATE_NONE);
87}
88
89void LayerConfig::set_l3_mode(uint32_t src_ipv4, uint32_t dst_ipv4) {
90
91    /* L2 config */
92    m_l2_config.set_state(LayerConfigMAC::STATE_UNCONFIGRED);
93
94    /* L3 config */
95    m_l3_ipv4_config.set_src(src_ipv4);
96    m_l3_ipv4_config.set_dst(dst_ipv4);
97    m_l3_ipv4_config.set_state(LayerConfigIPv4::STATE_UNRESOLVED);
98}
99
100void LayerConfig::set_l3_mode(uint32_t src_ipv4, uint32_t dst_ipv4, const uint8_t *resolved_mac) {
101
102    /* L2 config */
103    m_l2_config.set_dst(resolved_mac);
104    m_l2_config.set_state(LayerConfigMAC::STATE_CONFIGURED);
105
106    /* L3 config */
107    m_l3_ipv4_config.set_src(src_ipv4);
108    m_l3_ipv4_config.set_dst(dst_ipv4);
109    m_l3_ipv4_config.set_state(LayerConfigIPv4::STATE_RESOLVED);
110}
111
112void
113LayerConfig::on_link_down() {
114
115    /* on IPv4 only - when the state is resolved, move to unresolved and invalidate the MAC */
116    if (m_l3_ipv4_config.get_state() == LayerConfigIPv4::STATE_RESOLVED) {
117        m_l2_config.set_state(LayerConfigMAC::STATE_UNCONFIGRED);
118        m_l3_ipv4_config.set_state(LayerConfigIPv4::STATE_UNRESOLVED);
119    }
120}
121
122Json::Value
123LayerConfig::to_json() const {
124    Json::Value output;
125
126    output["ether"] = m_l2_config.to_json();
127    output["ipv4"]  = m_l3_ipv4_config.to_json();
128
129    return output;
130}
131
132std::string
133TRexPortAttr::get_rx_filter_mode() const {
134    switch (m_rx_filter_mode) {
135    case RX_FILTER_MODE_ALL:
136        return "all";
137    case RX_FILTER_MODE_HW:
138        return "hw";
139    default:
140        assert(0);
141    }
142}
143
144void
145TRexPortAttr::on_link_down() {
146    m_layer_cfg.on_link_down();
147}
148
149void
150TRexPortAttr::to_json(Json::Value &output) {
151
152    output["promiscuous"]["enabled"] = get_promiscuous();
153    output["multicast"]["enabled"]   = get_multicast();
154    output["link"]["up"]             = is_link_up();
155    output["speed"]                  = get_link_speed() / 1000; // make sure we have no cards of less than 1 Gbps
156    output["rx_filter_mode"]         = get_rx_filter_mode();
157
158    int mode;
159    get_flow_ctrl(mode);
160    output["fc"]["mode"] = mode;
161
162    output["layer_cfg"] = m_layer_cfg.to_json();
163}
164
165
166