HW-SD  1.0.0
netInfo.h
1 
2 /* Copyright (c) 2012, Daniel Nachbaur <danielnachbaur@gmail.com>
3  * 2013, Stefan.Eilemann@epfl.ch
4  *
5  * This library is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License version 2.1 as published
7  * by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef HWSD_NETINFO_H
20 #define HWSD_NETINFO_H
21 
22 #include <hwsd/nodeInfo.h> // base class
23 
24 #include <lunchbox/debug.h>
25 #include <climits>
26 #include <ostream>
27 
28 namespace hwsd
29 {
31  struct NetInfo : public NodeInfo
32  {
33  enum Type
34  {
35  TYPE_UNKNOWN = LB_BIT_NONE,
36  TYPE_ETHERNET = LB_BIT1,
37  TYPE_INFINIBAND = LB_BIT2,
38  TYPE_LOOPBACK = LB_BIT3,
40  TYPE_TUNNEL_6TO4 = LB_BIT5,
41  TYPE_ALL = LB_BIT_ALL_32
42  };
43 
45  static const unsigned defaultValue = UINT_MAX;
46 
48  NetInfo() : type( TYPE_UNKNOWN ), linkspeed( defaultValue )
49  , up( false ) {}
50 
52  bool operator == ( const NetInfo& rhs ) const
53  {
54  return NodeInfo::operator==( rhs ) && type == rhs.type &&
55  name == rhs.name && hostname == rhs.hostname &&
56  hwAddress == rhs.hwAddress &&
57  inetAddress == rhs.inetAddress &&
58  inet6Address == rhs.inet6Address &&
59  linkspeed == rhs.linkspeed && up == rhs.up;
60  }
61 
63  bool operator != ( const NetInfo& rhs ) const
64  {
65  return !(*this == rhs );
66  }
67 
68  std::string getType() const
69  {
70  switch( type )
71  {
72  case NetInfo::TYPE_ETHERNET:
73  return "ETHERNET";
74  case NetInfo::TYPE_INFINIBAND:
75  return "INFINIBAND";
76  case NetInfo::TYPE_LOOPBACK:
77  return "LOOPBACK";
79  return "TUNNEL_ETHERNET";
81  return "TUNNEL_6TO4";
82  case NetInfo::TYPE_UNKNOWN:
83  return "UNKNOWN";
84  default:
85  LBUNREACHABLE;
86  return "ERROR";
87  }
88  }
89 
90  void setType( const std::string& strType )
91  {
92  if( strType == "ETHERNET" )
93  type = NetInfo::TYPE_ETHERNET;
94  else if( strType == "INFINIBAND" )
95  type = NetInfo::TYPE_INFINIBAND;
96  else if( strType == "LOOPBACK" )
97  type = NetInfo::TYPE_LOOPBACK;
98  else
99  type = NetInfo::TYPE_UNKNOWN;
100  }
101 
104 
106  std::string name;
107 
109  std::string hostname;
110 
112  std::string hwAddress;
113 
115  std::string inetAddress;
116 
118  std::string inet6Address;
119 
121  unsigned int linkspeed;
122 
124  bool up;
125 
126  char dummy[24];
127  };
128 
129  inline std::ostream& operator << ( std::ostream& os, const NetInfo& info )
130  {
131  os << "NetInfo\n" << static_cast< const NodeInfo& >( info );
132  os << " Type " << info.getType() << std::endl;
133  os << " Status " << (info.up ? "UP" : "DOWN") << std::endl;
134  if( !info.name.empty( ))
135  os << " Name " << info.name << std::endl;
136  if( !info.hostname.empty( ))
137  os << " Hostname " << info.hostname << std::endl;
138  if( !info.hwAddress.empty( ))
139  os << " HWaddr " << info.hwAddress << std::endl;
140  if( !info.inetAddress.empty( ))
141  os << " IPv4 " << info.inetAddress << std::endl;
142  if( !info.inet6Address.empty( ))
143  os << " IPv6 " << info.inet6Address << std::endl;
144  if( info.linkspeed != NetInfo::defaultValue )
145  os << " Linkspeed " << info.linkspeed << "Mbps" << std::endl;
146 
147  return os;
148  }
149 }
150 
151 #endif // HWSD_NETINFO_H