NetworkInterfaceCard.h 3.98 KB
Newer Older
Christian Mohrbacher's avatar
Christian Mohrbacher committed
1 2 3 4 5 6 7 8 9
#ifndef NETWORKINTERFACECARD_H_
#define NETWORKINTERFACECARD_H_

#include <common/Common.h>
#include <common/toolkit/serialization/Serialization.h>

#include <net/if.h>


Bernd Lietzow's avatar
Bernd Lietzow committed
10 11 12 13 14
enum NicAddrType {
   NICADDRTYPE_STANDARD,
   NICADDRTYPE_SDP,
   NICADDRTYPE_RDMA
};
Christian Mohrbacher's avatar
Christian Mohrbacher committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

/**
 * Note: Make sure this struct can be copied with the assignment operator.
 */
struct NicAddress
{
   struct in_addr ipAddr;
   NicAddrType    nicType;
   char           name[IFNAMSIZ];

   static void serialize(const NicAddress* obj, Serializer& ser)
   {
      ser.putBlock(&obj->ipAddr.s_addr, sizeof(obj->ipAddr.s_addr) );
      ser.putBlock(obj->name, BEEGFS_MIN(sizeof(obj->name), SERIALIZATION_NICLISTELEM_NAME_SIZE) );
      ser % serdes::as<char>(obj->nicType);
      ser.skip(3); // PADDING
   }

   static void serialize(NicAddress* obj, Deserializer& des)
   {
      static const unsigned minNameSize =
         BEEGFS_MIN(sizeof(obj->name), SERIALIZATION_NICLISTELEM_NAME_SIZE);

      ::memset(obj, 0, sizeof(*obj) );

      des.getBlock(&obj->ipAddr.s_addr, sizeof(obj->ipAddr.s_addr) );
      des.getBlock(obj->name, minNameSize);
      obj->name[minNameSize - 1] = 0;
      des % serdes::as<char>(obj->nicType);
      des.skip(3); // PADDING
   }
};

template<>
struct ListSerializationHasLength<NicAddress> : boost::false_type {};

typedef struct NicListCapabilities
{
   bool supportsSDP;
   bool supportsRDMA;
} NicListCapabilities;


typedef std::list<NicAddress> NicAddressList;
typedef NicAddressList::iterator NicAddressListIter;


class NetworkInterfaceCard
{
   public:
Bernd Lietzow's avatar
Bernd Lietzow committed
65
      static bool findAll(StringList* allowedInterfacesList, bool useRDMA,
Christian Mohrbacher's avatar
Christian Mohrbacher committed
66
         NicAddressList* outList);
Bernd Lietzow's avatar
Bernd Lietzow committed
67
      static bool findAllInterfaces(const StringList& allowedInterfacesList,
Christian Mohrbacher's avatar
Christian Mohrbacher committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
         NicAddressList& outList);
      static bool findByName(const char* interfaceName, NicAddress* outAddr);

      static const char* nicTypeToString(NicAddrType nicType);
      static std::string nicAddrToString(NicAddress* nicAddr);

      static bool supportsSDP(NicAddressList* nicList);
      static bool supportsRDMA(NicAddressList* nicList);
      static void supportedCapabilities(NicAddressList* nicList,
         NicListCapabilities* outCapabilities);

      static bool checkAndAddRdmaCapability(NicAddressList& nicList);

   private:
      NetworkInterfaceCard() {}

      static bool fillNicAddress(int sock, NicAddrType nicType, struct ifreq* ifr,
         NicAddress* outAddr);
      static bool findAllBySocketDomain(int domain, NicAddrType nicType,
         const StringList* allowedInterfacesList, NicAddressList* outList);
      static void filterInterfacesForRDMA(NicAddressList* list, NicAddressList* outList);


   public:
Bernd Lietzow's avatar
Bernd Lietzow committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
      struct NicAddrComp {
         // the comparison implemented here is a model of Compare only if preferences contains
         // all possible names ever encountered or is unset.
         StringList* preferences = nullptr;

         explicit NicAddrComp(StringList* preferences = nullptr): preferences(preferences) {}

         bool operator()(const NicAddress& lhs, const NicAddress& rhs) const
         {
            // compares the preference of NICs
            // returns true if lhs is preferred compared to rhs
            if (preferences) {
               for (const auto& p : *preferences) {
                  if (p == lhs.name)
                     return true;
                  if (p == rhs.name)
                     return false;
               }
            }

            // prefer RDMA NICs
            if( (lhs.nicType == NICADDRTYPE_RDMA) && (rhs.nicType != NICADDRTYPE_RDMA) )
               return true;
            if( (rhs.nicType == NICADDRTYPE_RDMA) && (lhs.nicType != NICADDRTYPE_RDMA) )
               return false;

            // prefer higher ipAddr
            unsigned lhsHostOrderIP = ntohl(lhs.ipAddr.s_addr);
            unsigned rhsHostOrderIP = ntohl(rhs.ipAddr.s_addr);

            // this is the original IP-order version
            return lhsHostOrderIP > rhsHostOrderIP;
         }
      };
Christian Mohrbacher's avatar
Christian Mohrbacher committed
126 127 128 129
};


#endif /*NETWORKINTERFACECARD_H_*/