1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <macro.h>
5 #include <net/ethernet.h>
6 #include <linux/ethtool.h>
7 
8 #include "conf-parser.h"
9 #include "ether-addr-util.h"
10 
11 #define N_ADVERTISE 3
12 
13 /* we can't use DUPLEX_ prefix, as it
14  * clashes with <linux/ethtool.h> */
15 typedef enum Duplex {
16         DUP_HALF = DUPLEX_HALF,
17         DUP_FULL = DUPLEX_FULL,
18         _DUP_MAX,
19         _DUP_INVALID = -EINVAL,
20 } Duplex;
21 
22 typedef enum NetDevFeature {
23         NET_DEV_FEAT_SG,
24         NET_DEV_FEAT_IP_CSUM,
25         NET_DEV_FEAT_HW_CSUM,
26         NET_DEV_FEAT_IPV6_CSUM,
27         NET_DEV_FEAT_HIGHDMA,
28         NET_DEV_FEAT_FRAGLIST,
29         NET_DEV_FEAT_HW_VLAN_CTAG_TX,
30         NET_DEV_FEAT_HW_VLAN_CTAG_RX,
31         NET_DEV_FEAT_HW_VLAN_CTAG_FILTER,
32         NET_DEV_FEAT_HW_VLAN_STAG_TX,
33         NET_DEV_FEAT_HW_VLAN_STAG_RX,
34         NET_DEV_FEAT_HW_VLAN_STAG_FILTER,
35         NET_DEV_FEAT_VLAN_CHALLENGED,
36         NET_DEV_FEAT_GSO,
37         NET_DEV_FEAT_LLTX,
38         NET_DEV_FEAT_NETNS_LOCAL,
39         NET_DEV_FEAT_GRO,
40         NET_DEV_FEAT_GRO_HW,
41         NET_DEV_FEAT_LRO,
42         NET_DEV_FEAT_TSO,
43         NET_DEV_FEAT_GSO_ROBUST,
44         NET_DEV_FEAT_TSO_ECN,
45         NET_DEV_FEAT_TSO_MANGLEID,
46         NET_DEV_FEAT_TSO6,
47         NET_DEV_FEAT_FSO,
48         NET_DEV_FEAT_GSO_GRE,
49         NET_DEV_FEAT_GSO_GRE_CSUM,
50         NET_DEV_FEAT_GSO_IPXIP4,
51         NET_DEV_FEAT_GSO_IPXIP6,
52         NET_DEV_FEAT_GSO_UDP_TUNNEL,
53         NET_DEV_FEAT_GSO_UDP_TUNNEL_CSUM,
54         NET_DEV_FEAT_GSO_PARTIAL,
55         NET_DEV_FEAT_GSO_TUNNEL_REMCSUM,
56         NET_DEV_FEAT_GSO_SCTP,
57         NET_DEV_FEAT_GSO_ESP,
58         NET_DEV_FEAT_GSO_UDP_L4,
59         NET_DEV_FEAT_GSO_FRAGLIST,
60         NET_DEV_FEAT_FCOE_CRC,
61         NET_DEV_FEAT_SCTP_CRC,
62         NET_DEV_FEAT_FCOE_MTU,
63         NET_DEV_FEAT_NTUPLE,
64         NET_DEV_FEAT_RXHASH,
65         NET_DEV_FEAT_RXCSUM,
66         NET_DEV_FEAT_NOCACHE_COPY,
67         NET_DEV_FEAT_LOOPBACK,
68         NET_DEV_FEAT_RXFCS,
69         NET_DEV_FEAT_RXALL,
70         NET_DEV_FEAT_HW_L2FW_DOFFLOAD,
71         NET_DEV_FEAT_HW_TC,
72         NET_DEV_FEAT_HW_ESP,
73         NET_DEV_FEAT_HW_ESP_TX_CSUM,
74         NET_DEV_FEAT_RX_UDP_TUNNEL_PORT,
75         NET_DEV_FEAT_HW_TLS_RECORD,
76         NET_DEV_FEAT_HW_TLS_TX,
77         NET_DEV_FEAT_HW_TLS_RX,
78         NET_DEV_FEAT_GRO_FRAGLIST,
79         NET_DEV_FEAT_HW_MACSEC,
80         NET_DEV_FEAT_GRO_UDP_FWD,
81         NET_DEV_FEAT_HW_HSR_TAG_INS,
82         NET_DEV_FEAT_HW_HSR_TAG_RM,
83         NET_DEV_FEAT_HW_HSR_FWD,
84         NET_DEV_FEAT_HW_HSR_DUP,
85         _NET_DEV_FEAT_SIMPLE_MAX,
86 
87         NET_DEV_FEAT_TXCSUM = _NET_DEV_FEAT_SIMPLE_MAX,
88         _NET_DEV_FEAT_MAX,
89         _NET_DEV_FEAT_INVALID = -EINVAL,
90 } NetDevFeature;
91 
92 typedef enum NetDevPort {
93         NET_DEV_PORT_TP     = PORT_TP,
94         NET_DEV_PORT_AUI    = PORT_AUI,
95         NET_DEV_PORT_MII    = PORT_MII,
96         NET_DEV_PORT_FIBRE  = PORT_FIBRE,
97         NET_DEV_PORT_BNC    = PORT_BNC,
98         NET_DEV_PORT_DA     = PORT_DA,
99         NET_DEV_PORT_NONE   = PORT_NONE,
100         NET_DEV_PORT_OTHER  = PORT_OTHER,
101         _NET_DEV_PORT_MAX,
102         _NET_DEV_PORT_INVALID = -EINVAL,
103 } NetDevPort;
104 
105 #define ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32    (SCHAR_MAX)
106 #define ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NBYTES  (4 * ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32)
107 
108 /* layout of the struct passed from/to userland */
109 struct ethtool_link_usettings {
110         struct ethtool_link_settings base;
111 
112         struct {
113                 uint32_t supported[ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32];
114                 uint32_t advertising[ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32];
115                 uint32_t lp_advertising[ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32];
116         } link_modes;
117 };
118 
119 typedef struct u32_opt {
120         uint32_t value; /* a value of 0 indicates the hardware advertised maximum should be used.*/
121         bool set;
122 } u32_opt;
123 
124 typedef struct netdev_channels {
125         u32_opt rx;
126         u32_opt tx;
127         u32_opt other;
128         u32_opt combined;
129 } netdev_channels;
130 
131 typedef struct netdev_ring_param {
132         u32_opt rx;
133         u32_opt rx_mini;
134         u32_opt rx_jumbo;
135         u32_opt tx;
136 } netdev_ring_param;
137 
138 typedef struct netdev_coalesce_param {
139         u32_opt rx_coalesce_usecs;
140         u32_opt rx_max_coalesced_frames;
141         u32_opt rx_coalesce_usecs_irq;
142         u32_opt rx_max_coalesced_frames_irq;
143         u32_opt tx_coalesce_usecs;
144         u32_opt tx_max_coalesced_frames;
145         u32_opt tx_coalesce_usecs_irq;
146         u32_opt tx_max_coalesced_frames_irq;
147         u32_opt stats_block_coalesce_usecs;
148         int use_adaptive_rx_coalesce;
149         int use_adaptive_tx_coalesce;
150         u32_opt pkt_rate_low;
151         u32_opt rx_coalesce_usecs_low;
152         u32_opt rx_max_coalesced_frames_low;
153         u32_opt tx_coalesce_usecs_low;
154         u32_opt tx_max_coalesced_frames_low;
155         u32_opt pkt_rate_high;
156         u32_opt rx_coalesce_usecs_high;
157         u32_opt rx_max_coalesced_frames_high;
158         u32_opt tx_coalesce_usecs_high;
159         u32_opt tx_max_coalesced_frames_high;
160         u32_opt rate_sample_interval;
161 } netdev_coalesce_param;
162 
163 int ethtool_get_driver(int *ethtool_fd, const char *ifname, char **ret);
164 int ethtool_get_link_info(int *ethtool_fd, const char *ifname,
165                           int *ret_autonegotiation, uint64_t *ret_speed,
166                           Duplex *ret_duplex, NetDevPort *ret_port);
167 int ethtool_get_permanent_hw_addr(int *ethtool_fd, const char *ifname, struct hw_addr_data *ret);
168 int ethtool_set_wol(int *ethtool_fd, const char *ifname, uint32_t wolopts, const uint8_t password[SOPASS_MAX]);
169 int ethtool_set_nic_buffer_size(int *ethtool_fd, const char *ifname, const netdev_ring_param *ring);
170 int ethtool_set_features(int *ethtool_fd, const char *ifname, const int features[static _NET_DEV_FEAT_MAX]);
171 int ethtool_set_glinksettings(
172                 int *fd,
173                 const char *ifname,
174                 int autonegotiation,
175                 const uint32_t advertise[static N_ADVERTISE],
176                 uint64_t speed,
177                 Duplex duplex,
178                 NetDevPort port,
179                 uint8_t mdi);
180 int ethtool_set_channels(int *ethtool_fd, const char *ifname, const netdev_channels *channels);
181 int ethtool_set_flow_control(int *fd, const char *ifname, int rx, int tx, int autoneg);
182 int ethtool_set_nic_coalesce_settings(int *ethtool_fd, const char *ifname, const netdev_coalesce_param *coalesce);
183 
184 const char *duplex_to_string(Duplex d) _const_;
185 Duplex duplex_from_string(const char *d) _pure_;
186 
187 int wol_options_to_string_alloc(uint32_t opts, char **ret);
188 
189 const char *port_to_string(NetDevPort port) _const_;
190 NetDevPort port_from_string(const char *port) _pure_;
191 
192 const char *mdi_to_string(int mdi) _const_;
193 
194 const char *ethtool_link_mode_bit_to_string(enum ethtool_link_mode_bit_indices val) _const_;
195 enum ethtool_link_mode_bit_indices ethtool_link_mode_bit_from_string(const char *str) _pure_;
196 
197 CONFIG_PARSER_PROTOTYPE(config_parse_duplex);
198 CONFIG_PARSER_PROTOTYPE(config_parse_wol);
199 CONFIG_PARSER_PROTOTYPE(config_parse_port);
200 CONFIG_PARSER_PROTOTYPE(config_parse_mdi);
201 CONFIG_PARSER_PROTOTYPE(config_parse_advertise);
202 CONFIG_PARSER_PROTOTYPE(config_parse_ring_buffer_or_channel);
203 CONFIG_PARSER_PROTOTYPE(config_parse_coalesce_u32);
204 CONFIG_PARSER_PROTOTYPE(config_parse_coalesce_sec);
205 CONFIG_PARSER_PROTOTYPE(config_parse_nic_coalesce_setting);
206