root/opal/util/ethtool.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. opal_ethtool_get_speed

   1 /*
   2  * Copyright (c) 2016      Karol Mroz.  All rights reserved.
   3  * Copyright (c) 2016      Research Organization for Information Science
   4  *                         and Technology (RIST). All rights reserved.
   5  * Copyright (c) 2016      Cisco Systems, Inc.  All rights reserved.
   6  * $COPYRIGHT$
   7  *
   8  * Additional copyrights may follow
   9  *
  10  * $HEADER$
  11  */
  12 
  13 #include "opal_config.h"
  14 
  15 #include <string.h>
  16 #include <limits.h>
  17 
  18 #ifdef HAVE_UNISTD_H
  19 #include <unistd.h>
  20 #endif
  21 #ifdef HAVE_SYS_TYPES_H
  22 #include <sys/types.h>
  23 #endif
  24 #ifdef HAVE_SYS_SOCKET_H
  25 #include <sys/socket.h>
  26 #endif
  27 #ifdef HAVE_NET_IF_H
  28 #include <net/if.h>
  29 #endif
  30 #ifdef HAVE_LINUX_ETHTOOL_H
  31 #include <linux/ethtool.h>
  32 #endif
  33 #ifdef HAVE_SYS_IOCTL_H
  34 #include <sys/ioctl.h>
  35 #endif
  36 #ifdef HAVE_LINUX_SOCKIOS_H
  37 #include <linux/sockios.h>
  38 #endif
  39 
  40 #include "opal/util/ethtool.h"
  41 #include "opal/util/if.h"
  42 #include "opal/util/string_copy.h"
  43 
  44 /*
  45  * Obtain an appropriate bandwidth for the interface if_name. On Linux, we
  46  * get this via an ioctl(). Elsewhere or in the error case, we return the
  47  * speed as 0.
  48  */
  49 unsigned int
  50 opal_ethtool_get_speed (const char *if_name)
  51 {
  52     unsigned int speed = 0;
  53 
  54 #if defined(HAVE_DECL_SIOCETHTOOL) && defined(HAVE_STRUCT_IFREQ) && defined(HAVE_STRUCT_ETHTOOL_CMD)
  55     int sockfd;
  56     struct ifreq ifr;
  57     struct ethtool_cmd edata = {
  58         .cmd = ETHTOOL_GSET,
  59     };
  60 
  61     sockfd = socket(PF_INET, SOCK_DGRAM, 0);
  62     if (sockfd < 0) {
  63         goto out;
  64     }
  65 
  66     memset(&ifr, 0, sizeof(struct ifreq));
  67     opal_string_copy(ifr.ifr_name, if_name, IF_NAMESIZE);
  68     ifr.ifr_data = (char *)&edata;
  69 
  70     if (ioctl(sockfd, SIOCETHTOOL, &ifr) < 0) {
  71         goto out;
  72     }
  73 
  74 #if HAVE_DECL_ETHTOOL_CMD_SPEED
  75     speed = ethtool_cmd_speed(&edata);
  76 #elif defined(HAVE_STRUCT_ETHTOOL_CMD_SPEED_HI)
  77     speed = (edata.speed_hi << 16) | edata.speed;
  78 #else
  79     speed = edata.speed;
  80 #endif
  81     if (UINT_MAX == speed) {
  82         speed = 0;
  83     }
  84 
  85 out:
  86     close(sockfd);
  87 #endif
  88 
  89     return speed;
  90 }

/* [<][>][^][v][top][bottom][index][help] */