netdev-linux: Introduce netdev_linux_ethtool_set_flag().
authorJustin Pettit <jpettit@nicira.com>
Wed, 17 Aug 2011 21:38:08 +0000 (14:38 -0700)
committerJustin Pettit <jpettit@nicira.com>
Sun, 28 Aug 2011 20:43:38 +0000 (13:43 -0700)
There will be a caller added soon.

lib/netdev-linux.c
lib/netdev-linux.h

index 05b830c..838dac6 100644 (file)
@@ -90,6 +90,15 @@ COVERAGE_DEFINE(netdev_ethtool);
 #define ADVERTISED_Asym_Pause           (1 << 14)
 #endif
 
+/* These were introduced in Linux 2.6.24, so they might be missing if we
+ * have old headers. */
+#ifndef ETHTOOL_GFLAGS
+#define ETHTOOL_GFLAGS       0x00000025 /* Get flags bitmap(ethtool_value) */
+#endif
+#ifndef ETHTOOL_SFLAGS
+#define ETHTOOL_SFLAGS       0x00000026 /* Set flags bitmap(ethtool_value) */
+#endif
+
 /* This was introduced in Linux 2.6.25, so it might be missing if we have old
  * headers. */
 #ifndef TC_RTAB_SIZE
@@ -4210,6 +4219,51 @@ netdev_linux_do_ethtool(const char *name, struct ethtool_cmd *ecmd,
     }
 }
 
+/* Modifies the 'flag' bit in ethtool's flags field for 'netdev'.  If
+ * 'enable' is true, the bit is set.  Otherwise, it is cleared. */
+int
+netdev_linux_ethtool_set_flag(struct netdev *netdev, uint32_t flag,
+                              const char *flag_name, bool enable)
+{
+    const char *netdev_name = netdev_get_name(netdev);
+    struct ethtool_value evalue;
+    uint32_t new_flags;
+    int error;
+
+    memset(&evalue, 0, sizeof evalue);
+    error = netdev_linux_do_ethtool(netdev_name,
+                                    (struct ethtool_cmd *)&evalue,
+                                    ETHTOOL_GFLAGS, "ETHTOOL_GFLAGS");
+    if (error) {
+        return error;
+    }
+
+    evalue.data = new_flags = (evalue.data & ~flag) | (enable ? flag : 0);
+    error = netdev_linux_do_ethtool(netdev_name,
+                                    (struct ethtool_cmd *)&evalue,
+                                    ETHTOOL_SFLAGS, "ETHTOOL_SFLAGS");
+    if (error) {
+        return error;
+    }
+
+    memset(&evalue, 0, sizeof evalue);
+    error = netdev_linux_do_ethtool(netdev_name,
+                                    (struct ethtool_cmd *)&evalue,
+                                    ETHTOOL_GFLAGS, "ETHTOOL_GFLAGS");
+    if (error) {
+        return error;
+    }
+
+    if (new_flags != evalue.data) {
+        VLOG_WARN_RL(&rl, "attempt to %s ethtool %s flag on network "
+                     "device %s failed", enable ? "enable" : "disable",
+                     flag_name, netdev_name);
+        return EOPNOTSUPP;
+    }
+
+    return 0;
+}
+
 static int
 netdev_linux_do_ioctl(const char *name, struct ifreq *ifr, int cmd,
                       const char *cmd_name)
index 7a11204..d34a440 100644 (file)
 #ifndef NETDEV_LINUX_H
 #define NETDEV_LINUX_H 1
 
+#include <stdint.h>
+#include <stdbool.h>
+
 /* These functions are Linux specific, so they should be used directly only by
  * Linux-specific code. */
 
+struct netdev;
 struct netdev_stats;
 struct rtnl_link_stats;
 struct rtnl_link_stats64;
@@ -31,4 +35,7 @@ void netdev_stats_from_rtnl_link_stats64(struct netdev_stats *dst,
 void netdev_stats_to_rtnl_link_stats64(struct rtnl_link_stats64 *dst,
                                        const struct netdev_stats *src);
 
+int netdev_linux_ethtool_set_flag(struct netdev *netdev, uint32_t flag,
+                                  const char *flag_name, bool enable);
+
 #endif /* netdev-linux.h */