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