trex_port_attr.h revision 0fdd81a9
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#ifndef __TREX_PORT_ATTR_H__
18#define __TREX_PORT_ATTR_H__
19
20#include <string>
21#include <vector>
22#include "rte_ethdev_includes.h"
23#include "trex_defs.h"
24#include "common/basic_utils.h"
25#include <json/json.h>
26#include "trex_stateless_rx_defs.h"
27#include <string.h>
28
29/**
30 * destination port attribute
31 *
32 */
33class DestAttr {
34
35public:
36
37    DestAttr(uint8_t port_id);
38
39    /**
40     * dest can be either MAC IPv4, or IPv4 unresolved
41     */
42    enum dest_type_e {
43        DEST_TYPE_IPV4 = 1,
44        DEST_TYPE_IPV4_UNRESOLVED,
45        DEST_TYPE_MAC,
46    };
47
48    /**
49     * set dest as an IPv4 unresolved
50     */
51    void set_dest(uint32_t ipv4);
52
53    /**
54     * set dest as a resolved IPv4
55     */
56    void set_dest(uint32_t ipv4, const uint8_t *mac);
57
58    /**
59     * set dest as a plain MAC
60     */
61    void set_dest(const uint8_t *mac);
62
63
64    /**
65     * return true if destination is resolved
66     */
67    bool is_resolved() const {
68        return (m_type != DEST_TYPE_IPV4_UNRESOLVED);
69    }
70
71    /**
72     * get the dest mac
73     * if the dest is not resolved
74     * it will return the default MAC
75     */
76    const uint8_t *get_dest_mac() const {
77        return m_mac;
78    }
79
80    /**
81     * when link gets down - this should be called
82     *
83     */
84    void on_link_down() {
85        if (m_type == DEST_TYPE_IPV4) {
86            /* reset the IPv4 dest with no resolution */
87            set_dest(m_ipv4);
88        }
89    }
90
91    void to_json(Json::Value &output) const;
92
93private:
94    uint32_t          m_ipv4;
95    uint8_t          *m_mac;
96    dest_type_e       m_type;
97    uint8_t           m_port_id;
98
99private:
100    uint8_t m_default_mac[6];
101};
102
103
104class TRexPortAttr {
105public:
106
107    TRexPortAttr(uint8_t port_id) : m_dest(port_id) {
108        m_src_ipv4 = 0;
109    }
110
111    virtual ~TRexPortAttr(){}
112
113/*    UPDATES    */
114    virtual void update_link_status() = 0;
115    virtual bool update_link_status_nowait() = 0; // returns true if the status was changed
116    virtual void update_device_info() = 0;
117    virtual void reset_xstats() = 0;
118    virtual void update_description() = 0;
119
120/*    GETTERS    */
121    virtual bool get_promiscuous() = 0;
122    virtual void get_hw_src_mac(struct ether_addr *mac_addr) = 0;
123    virtual uint32_t get_link_speed() { return m_link.link_speed; } // L1 Mbps
124    virtual bool is_link_duplex() { return (m_link.link_duplex ? true : false); }
125    virtual bool is_link_autoneg() { return (m_link.link_autoneg ? true : false); }
126    virtual bool is_link_up() { return (m_link.link_status ? true : false); }
127    virtual int get_xstats_values(xstats_values_t &xstats_values) = 0;
128    virtual int get_xstats_names(xstats_names_t &xstats_names) = 0;
129    virtual int get_flow_ctrl(int &mode) = 0;
130    virtual bool is_virtual() { return flag_is_virtual; }
131    virtual bool is_fc_change_supported() { return flag_is_fc_change_supported; }
132    virtual bool is_led_change_supported() { return flag_is_led_change_supported; }
133    virtual bool is_link_change_supported() { return flag_is_link_change_supported; }
134    virtual void get_description(std::string &description) { description = intf_info_st.description; }
135    virtual void get_supported_speeds(supp_speeds_t &supp_speeds) = 0;
136    virtual bool is_loopback() const = 0;
137
138    uint32_t get_src_ipv4() const {return m_src_ipv4;}
139    DestAttr & get_dest() {return m_dest;}
140
141    const uint8_t *get_src_mac() const;
142    std::string get_rx_filter_mode() const;
143
144    /* for a raw packet, write the src/dst MACs */
145    void update_src_dst_mac(uint8_t *raw_pkt) const;
146
147/*    SETTERS    */
148    virtual int set_promiscuous(bool enabled) = 0;
149    virtual int add_mac(char * mac) = 0;
150    virtual int set_link_up(bool up) = 0;
151    virtual int set_flow_ctrl(int mode) = 0;
152    virtual int set_led(bool on) = 0;
153    virtual int set_rx_filter_mode(rx_filter_mode_e mode) = 0;
154
155    void set_src_ipv4(uint32_t addr);
156
157    /* DUMPS */
158    virtual void dump_link(FILE *fd) = 0;
159
160    /* dump object status to JSON */
161    void to_json(Json::Value &output);
162
163    uint8_t get_port_id() const {
164        return m_port_id;
165    }
166
167protected:
168
169    uint8_t                   m_port_id;
170    rte_eth_link              m_link;
171    uint32_t                  m_src_ipv4;
172    DestAttr                  m_dest;
173
174    struct rte_eth_dev_info   dev_info;
175
176    rx_filter_mode_e m_rx_filter_mode;
177
178    bool       flag_is_virtual;
179    bool       flag_is_fc_change_supported;
180    bool       flag_is_led_change_supported;
181    bool       flag_is_link_change_supported;
182
183
184    struct intf_info_st {
185        std::string     pci_addr;
186        std::string     description;
187        int             numa_node;
188    }intf_info_st;
189
190};
191
192class DpdkTRexPortAttr : public TRexPortAttr {
193public:
194
195    DpdkTRexPortAttr(uint8_t port_id, bool is_virtual, bool fc_change_allowed) : TRexPortAttr(port_id) {
196
197        m_port_id = port_id;
198        m_rx_filter_mode = RX_FILTER_MODE_HW;
199
200        flag_is_virtual = is_virtual;
201        int tmp;
202        flag_is_fc_change_supported = fc_change_allowed && (get_flow_ctrl(tmp) != -ENOTSUP);
203        flag_is_led_change_supported = (set_led(true) != -ENOTSUP);
204        flag_is_link_change_supported = (set_link_up(true) != -ENOTSUP);
205        update_description();
206        update_device_info();
207    }
208
209/*    UPDATES    */
210    virtual void update_link_status();
211    virtual bool update_link_status_nowait(); // returns true if the status was changed
212    virtual void update_device_info();
213    virtual void reset_xstats();
214    virtual void update_description();
215
216/*    GETTERS    */
217    virtual bool get_promiscuous();
218    virtual void get_hw_src_mac(struct ether_addr *mac_addr);
219    virtual int get_xstats_values(xstats_values_t &xstats_values);
220    virtual int get_xstats_names(xstats_names_t &xstats_names);
221    virtual int get_flow_ctrl(int &mode);
222    virtual void get_supported_speeds(supp_speeds_t &supp_speeds);
223    virtual bool is_loopback() const;
224
225/*    SETTERS    */
226    virtual int set_promiscuous(bool enabled);
227    virtual int add_mac(char * mac);
228    virtual int set_link_up(bool up);
229    virtual int set_flow_ctrl(int mode);
230    virtual int set_led(bool on);
231
232    virtual int set_rx_filter_mode(rx_filter_mode_e mode);
233
234/*    DUMPS    */
235    virtual void dump_link(FILE *fd);
236
237private:
238    rte_eth_fc_conf fc_conf_tmp;
239    std::vector <struct rte_eth_xstat> xstats_values_tmp;
240    std::vector <struct rte_eth_xstat_name> xstats_names_tmp;
241
242};
243
244
245class SimTRexPortAttr : public TRexPortAttr {
246public:
247    SimTRexPortAttr() : TRexPortAttr(0) {
248        m_link.link_speed   = 10000;
249        m_link.link_duplex  = 1;
250        m_link.link_autoneg = 0;
251        m_link.link_status  = 1;
252        flag_is_virtual = true;
253        flag_is_fc_change_supported = false;
254        flag_is_led_change_supported = false;
255        flag_is_link_change_supported = false;
256    }
257
258    /* DUMMY */
259    void update_link_status() {}
260    bool update_link_status_nowait() { return false; }
261    void update_device_info() {}
262    void reset_xstats() {}
263    void update_description() {}
264    bool get_promiscuous() { return false; }
265    void get_hw_src_mac(struct ether_addr *mac_addr) {}
266    int get_xstats_values(xstats_values_t &xstats_values) { return -ENOTSUP; }
267    int get_xstats_names(xstats_names_t &xstats_names) { return -ENOTSUP; }
268    int get_flow_ctrl(int &mode) { return -ENOTSUP; }
269    void get_description(std::string &description) {}
270    void get_supported_speeds(supp_speeds_t &supp_speeds) {}
271    int set_promiscuous(bool enabled) { return -ENOTSUP; }
272    int add_mac(char * mac) { return -ENOTSUP; }
273    int set_link_up(bool up) { return -ENOTSUP; }
274    int set_flow_ctrl(int mode) { return -ENOTSUP; }
275    int set_led(bool on) { return -ENOTSUP; }
276    void dump_link(FILE *fd) {}
277    int set_rx_filter_mode(rx_filter_mode_e mode) { return -ENOTSUP; }
278    virtual bool is_loopback() const { return false; }
279};
280
281
282#endif /* __TREX_PORT_ATTR_H__ */
283