trex_port_attr.h revision 25be508c
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() {
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
137    uint32_t get_src_ipv4() {return m_src_ipv4;}
138    DestAttr & get_dest() {return m_dest;}
139
140    const uint8_t *get_src_mac() const;
141    std::string get_rx_filter_mode() const;
142
143    /* for a raw packet, write the src/dst MACs */
144    void update_src_dst_mac(uint8_t *raw_pkt);
145
146/*    SETTERS    */
147    virtual int set_promiscuous(bool enabled) = 0;
148    virtual int add_mac(char * mac) = 0;
149    virtual int set_link_up(bool up) = 0;
150    virtual int set_flow_ctrl(int mode) = 0;
151    virtual int set_led(bool on) = 0;
152    virtual int set_rx_filter_mode(rx_filter_mode_e mode) = 0;
153
154    void set_src_ipv4(uint32_t addr) {
155        m_src_ipv4 = addr;
156    }
157
158    /* DUMPS */
159    virtual void dump_link(FILE *fd) = 0;
160
161    /* dump object status to JSON */
162    void to_json(Json::Value &output);
163
164
165protected:
166
167    uint8_t                   m_port_id;
168    rte_eth_link              m_link;
169    uint32_t                  m_src_ipv4;
170    DestAttr                  m_dest;
171
172    struct rte_eth_dev_info   dev_info;
173
174    rx_filter_mode_e m_rx_filter_mode;
175
176    bool       flag_is_virtual;
177    bool       flag_is_fc_change_supported;
178    bool       flag_is_led_change_supported;
179    bool       flag_is_link_change_supported;
180
181
182    struct intf_info_st {
183        std::string     pci_addr;
184        std::string     description;
185        int             numa_node;
186    }intf_info_st;
187
188};
189
190class DpdkTRexPortAttr : public TRexPortAttr {
191public:
192
193    DpdkTRexPortAttr(uint8_t port_id, bool is_virtual, bool fc_change_allowed) : TRexPortAttr(port_id) {
194
195        m_port_id = port_id;
196        m_rx_filter_mode = RX_FILTER_MODE_HW;
197
198        flag_is_virtual = is_virtual;
199        int tmp;
200        flag_is_fc_change_supported = fc_change_allowed && (get_flow_ctrl(tmp) != -ENOTSUP);
201        flag_is_led_change_supported = (set_led(true) != -ENOTSUP);
202        flag_is_link_change_supported = (set_link_up(true) != -ENOTSUP);
203        update_description();
204        update_device_info();
205    }
206
207/*    UPDATES    */
208    virtual void update_link_status();
209    virtual bool update_link_status_nowait(); // returns true if the status was changed
210    virtual void update_device_info();
211    virtual void reset_xstats();
212    virtual void update_description();
213
214/*    GETTERS    */
215    virtual bool get_promiscuous();
216    virtual void get_hw_src_mac(struct ether_addr *mac_addr);
217    virtual int get_xstats_values(xstats_values_t &xstats_values);
218    virtual int get_xstats_names(xstats_names_t &xstats_names);
219    virtual int get_flow_ctrl(int &mode);
220    virtual void get_supported_speeds(supp_speeds_t &supp_speeds);
221
222/*    SETTERS    */
223    virtual int set_promiscuous(bool enabled);
224    virtual int add_mac(char * mac);
225    virtual int set_link_up(bool up);
226    virtual int set_flow_ctrl(int mode);
227    virtual int set_led(bool on);
228
229    virtual int set_rx_filter_mode(rx_filter_mode_e mode);
230
231/*    DUMPS    */
232    virtual void dump_link(FILE *fd);
233
234private:
235    rte_eth_fc_conf fc_conf_tmp;
236    std::vector <struct rte_eth_xstat> xstats_values_tmp;
237    std::vector <struct rte_eth_xstat_name> xstats_names_tmp;
238
239};
240
241
242class SimTRexPortAttr : public TRexPortAttr {
243public:
244    SimTRexPortAttr() : TRexPortAttr(0) {
245        m_link.link_speed   = 10000;
246        m_link.link_duplex  = 1;
247        m_link.link_autoneg = 0;
248        m_link.link_status  = 1;
249        flag_is_virtual = true;
250        flag_is_fc_change_supported = false;
251        flag_is_led_change_supported = false;
252        flag_is_link_change_supported = false;
253    }
254
255    /* DUMMY */
256    void update_link_status() {}
257    bool update_link_status_nowait() { return false; }
258    void update_device_info() {}
259    void reset_xstats() {}
260    void update_description() {}
261    bool get_promiscuous() { return false; }
262    void get_hw_src_mac(struct ether_addr *mac_addr) {}
263    int get_xstats_values(xstats_values_t &xstats_values) { return -ENOTSUP; }
264    int get_xstats_names(xstats_names_t &xstats_names) { return -ENOTSUP; }
265    int get_flow_ctrl(int &mode) { return -ENOTSUP; }
266    void get_description(std::string &description) {}
267    void get_supported_speeds(supp_speeds_t &supp_speeds) {}
268    int set_promiscuous(bool enabled) { return -ENOTSUP; }
269    int add_mac(char * mac) { return -ENOTSUP; }
270    int set_link_up(bool up) { return -ENOTSUP; }
271    int set_flow_ctrl(int mode) { return -ENOTSUP; }
272    int set_led(bool on) { return -ENOTSUP; }
273    void dump_link(FILE *fd) {}
274    int set_rx_filter_mode(rx_filter_mode_e mode) { return -ENOTSUP; }
275};
276
277
278#endif /* __TREX_PORT_ATTR_H__ */
279