EthernetController.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #include "EthernetController.hh"
18 //#include "EthernetController.ih"
19 
20 // Custom includes
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <net/if.h>
24 #include <linux/ethtool.h>
25 #include <linux/sockios.h>
26 #include <senf/Utils/Exception.hh>
27 
28 #define prefix_
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 
31 #define doIoctl(ifr, errorMsg) \
32  if ( ::ioctl( sockfd_->fd, SIOCETHTOOL, &ifr ) < 0 ) \
33  SENF_THROW_SYSTEM_EXCEPTION("EthernetController: " errorMsg)
34 
35 
37  : NetdeviceController(interface_name)
38 {}
39 
41  : NetdeviceController(interface_index)
42 {}
43 
45  const
46 {
47  struct ifreq ifr;
48  ifrName(ifr);
49  struct ethtool_cmd edata;
50  edata.cmd = ETHTOOL_GSET;
51  ifr.ifr_data = reinterpret_cast<caddr_t>(&edata);
52  doIoctl(ifr, "Could not discover speed of ethernet interface");
53  return edata.speed;
54 }
55 
57  const
58 {
59  struct ifreq ifr;
60  ifrName(ifr);
61  struct ethtool_cmd edata;
62  edata.cmd = ETHTOOL_GSET;
63  ifr.ifr_data = reinterpret_cast<caddr_t>(&edata);
64  doIoctl(ifr, "Could not discover duplex mode of ethernet interface");
65  switch (edata.duplex) {
66  case DUPLEX_HALF:
67  return HALF_DUPLEX;
68  case DUPLEX_FULL:
69  return FULL_DUPLEX;
70  case DUPLEX_UNKNOWN:
71  return UNKNOWN_DUPLEX;
72  default:
73  SENF_THROW_SYSTEM_EXCEPTION("EthernetController: invalid duplex mode ")
74  << "(" << unsigned(edata.duplex) << ")";
75  }
76 }
77 
79  const
80 {
81  return duplexAsString(duplex());
82 }
83 
85 {
86  static const std::string duplexModes[] = { "half", "full", "unknown" };
87  return duplexModes[std::min(unsigned(mode),2u)];
88 }
89 
90 
92  const
93 {
94  struct ifreq ifr;
95  ifrName(ifr);
96  struct ethtool_value edata;
97  edata.cmd = ETHTOOL_GLINK;
98  ifr.ifr_data = reinterpret_cast<caddr_t>(&edata);
99  doIoctl(ifr, "Could not discover link status of ethernet interface");
100  return edata.data;
101 }
102 
104 {
105  struct ifreq ifr;
106  ifrName(ifr);
107  struct ethtool_value edata;
108  edata.cmd = ETHTOOL_NWAY_RST;
109  ifr.ifr_data = reinterpret_cast<caddr_t>(&edata);
110  doIoctl(ifr, "nWayReset failed.");
111  return edata.data;
112 }
113 
114 prefix_ bool senf::emu::EthernetController::ringSize(std::uint32_t rx, std::uint32_t tx)
115 {
116  struct ifreq ifr;
117  ifrName(ifr);
118  struct ethtool_ringparam edata;
119  memset(&edata, 0, sizeof(edata));
120  edata.cmd = ETHTOOL_SRINGPARAM;
121  edata.rx_pending = rx;
122  edata.tx_pending = tx;
123  ifr.ifr_data = reinterpret_cast<caddr_t>(&edata);
124  doIoctl(ifr, "setRingParam failed.");
125  return true;
126 }
127 
128 prefix_ std::pair<std::uint32_t,std::uint32_t> senf::emu::EthernetController::ringSize()
129 {
130  struct ifreq ifr;
131  ifrName(ifr);
132  struct ethtool_ringparam edata;
133  memset(&edata, 0, sizeof(edata));
134  edata.cmd = ETHTOOL_GRINGPARAM;
135  ifr.ifr_data = reinterpret_cast<caddr_t>(&edata);
136  doIoctl(ifr, "getRingParam failed.");
137  return std::make_pair(edata.rx_pending,edata.tx_pending);
138 }
139 
141 {
142  struct ifreq ifr;
143  ifrName(ifr);
144  struct ethtool_eee edata;
145  memset(&edata, 0, sizeof(edata));
146  edata.cmd = ETHTOOL_SEEE;
147  edata.eee_enabled = on;
148  ifr.ifr_data = reinterpret_cast<caddr_t>(&edata);
149  doIoctl(ifr, "setEEE failed.");
150  return true;
151 }
152 
154 {
155  struct ifreq ifr;
156  ifrName(ifr);
157  struct ethtool_eee edata;
158  memset(&edata, 0, sizeof(edata));
159  edata.cmd = ETHTOOL_GEEE;
160  ifr.ifr_data = reinterpret_cast<caddr_t>(&edata);
161  doIoctl(ifr, "getEEE failed.");
162  return edata.eee_enabled;
163 }
164 
165 
166 #undef doIoctl
167 
168 //-/////////////////////////////////////////////////////////////////////////////////////////////////
169 #undef prefix_
170 
171 
172 // Local Variables:
173 // mode: c++
174 // fill-column: 100
175 // comment-column: 40
176 // c-file-style: "senf"
177 // indent-tabs-mode: nil
178 // ispell-local-dictionary: "american"
179 // compile-command: "scons -u test"
180 // End:
#define prefix_
WLANController public header.
#define SENF_THROW_SYSTEM_EXCEPTION(desc)
std::int32_t min
std::string duplexAsString() const
void ifrName(ifreq &ifr) const
EthernetController(std::string const &interface_name)
#define doIoctl(ifr, errorMsg)
std::pair< std::uint32_t, std::uint32_t > ringSize()