This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 80753df92c23eb1bcdbe7f48d4c5d51e0e492c5b
Author: gaohedong <[email protected]>
AuthorDate: Fri Nov 14 15:10:07 2025 +0800

    net/ethernet: add netlib api to get/set driver channels info
    
    add netlib api to get/set driver channels info
    
    Signed-off-by: gaohedong <[email protected]>
    Assisted-by: Kiro:claude-opus-4
---
 include/netutils/netlib.h        |  21 +++++
 netutils/netlib/CMakeLists.txt   |   4 +
 netutils/netlib/Makefile         |   4 +
 netutils/netlib/netlib_ethtool.c | 165 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 194 insertions(+)

diff --git a/include/netutils/netlib.h b/include/netutils/netlib.h
index 57491645e..b140a09c4 100644
--- a/include/netutils/netlib.h
+++ b/include/netutils/netlib.h
@@ -228,6 +228,27 @@ ssize_t netlib_get_devices(FAR struct netlib_device_s 
*devlist,
                            unsigned int nentries, sa_family_t family);
 #endif
 
+#ifdef CONFIG_NETDEV_ETHTOOL_IOCTL
+/* Perform an ethtool ioctl on the named device.  data points to an
+ * ethtool request structure (e.g. struct ethtool_chns).
+ *
+ * Return: 0 on success; -1 on failure with errno set.
+ */
+
+int netlib_ethtool_ioctl(FAR const char *ifname, FAR void *data);
+
+/* Get or set the mask of combined channels that are switched on.  Bit n
+ * set means channel n is on; e.g. 0x51 means channels 0, 4 and 6.
+ *
+ * Return: 0 on success; -1 on failure with errno set.
+ */
+
+int netlib_ethtool_getchannel(FAR const char *ifname,
+                              FAR uint32_t *combined_chns_map);
+int netlib_ethtool_setchannel(FAR const char *ifname,
+                              uint32_t combined_chns_map);
+#endif
+
 /* Convert a textual representation of an IP address to a numerical
  * representation.
  *
diff --git a/netutils/netlib/CMakeLists.txt b/netutils/netlib/CMakeLists.txt
index 6463c15bd..409fb0c3a 100644
--- a/netutils/netlib/CMakeLists.txt
+++ b/netutils/netlib/CMakeLists.txt
@@ -101,6 +101,10 @@ if(CONFIG_NETUTILS_NETLIB)
     list(APPEND SRCS netlib_getifstatistics.c)
   endif()
 
+  if(CONFIG_NETDEV_ETHTOOL_IOCTL)
+    list(APPEND SRCS netlib_ethtool.c)
+  endif()
+
   # Netfilter connection support
 
   if(CONFIG_NETLINK_NETFILTER)
diff --git a/netutils/netlib/Makefile b/netutils/netlib/Makefile
index 2850cddeb..1bdec0ee8 100644
--- a/netutils/netlib/Makefile
+++ b/netutils/netlib/Makefile
@@ -102,6 +102,10 @@ ifeq ($(CONFIG_NETDEV_STATISTICS),y)
 CSRCS += netlib_getifstatistics.c
 endif
 
+ifeq ($(CONFIG_NETDEV_ETHTOOL_IOCTL),y)
+CSRCS += netlib_ethtool.c
+endif
+
 # Netfilter connection support
 
 ifeq ($(CONFIG_NETLINK_NETFILTER),y)
diff --git a/netutils/netlib/netlib_ethtool.c b/netutils/netlib/netlib_ethtool.c
new file mode 100644
index 000000000..d97a42153
--- /dev/null
+++ b/netutils/netlib/netlib_ethtool.c
@@ -0,0 +1,165 @@
+/****************************************************************************
+ * apps/netutils/netlib/netlib_ethtool.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <nuttx/debug.h>
+
+#include <netinet/in.h>
+#include <net/if.h>
+#include <nuttx/ethtool.h>
+
+#include "netutils/netlib.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: netlib_ethtool_ioctl
+ *
+ * Description:
+ *   Perform ethtool-related IOCTL operations on network devices
+ *
+ * Parameters:
+ *   ifname  - The name of the ethernet device
+ *   data    - Pointer to an ethtool-related data structure
+ *             (e.g., ethtool_cmd, ethtool_chns), used to store
+ *             input/output parameters (specific type determined by @cmd)
+ *
+ * Return:
+ *   0 on success; -1 on failure
+ *
+ ****************************************************************************/
+
+int netlib_ethtool_ioctl(FAR const char *ifname, FAR void *data)
+{
+  struct ifreq req;
+  int sockfd;
+  int ret;
+
+  if (ifname == NULL || data == NULL)
+    {
+      return -EINVAL;
+    }
+
+  sockfd = socket(AF_INET, SOCK_CTRL, 0);
+  if (sockfd < 0)
+    {
+      nerr("Failed to create socket, cmd %" PRIx32 ", errno:%d",
+           *(uint32_t *)data, errno);
+      return sockfd;
+    }
+
+  /* Add the device name to the request */
+
+  strlcpy(req.ifr_name, ifname, IFNAMSIZ);
+
+  req.ifr_data = data;
+
+  ret = ioctl(sockfd, SIOCETHTOOL, (unsigned long)&req);
+  close(sockfd);
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: netlib_ethtool_getchannel
+ *
+ * Description:
+ *   Ethernet device hardware may have multiple channels, and users can turn
+ *   on/off the channels through the API. When the channel is open, data can
+ *   be sent and received through the channel. When the channel is closed, no
+ *   corresponding transmit/receive interrupt is found.
+ *
+ * Parameters:
+ *   ifname   - The name of the ethernet device
+ *   combined_chns_map - The status of channel opening or closing.  For
+ *              example, 0x51 means that channel 0,4, and 6 will be turned
+ *              on, and other channels will be turned off.
+ *
+ * Return:
+ *   0 on success; -1 on failure
+ *
+ ****************************************************************************/
+
+int netlib_ethtool_getchannel(FAR const char *ifname,
+                              FAR uint32_t *combined_chns_map)
+{
+  struct ethtool_chns2 chns;
+  int ret;
+
+  /* Get device combined channel */
+
+  chns.cmd = ETHTOOL_GCHANNELS2;
+
+  ret = netlib_ethtool_ioctl(ifname, &chns);
+  if (ret == OK)
+    {
+      *combined_chns_map = chns.combined_chns_map;
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: netlib_ethtool_setchannel
+ *
+ * Description:
+ *   Ethernet device hardware may have multiple channels, and users can turn
+ *   on/off the channels through the API. When the channel is open, data can
+ *   be sent and received through the channel. When the channel is closed, no
+ *   corresponding transmit/receive interrupt is found.
+ *
+ * Parameters:
+ *   ifname   - The name of the ethernet device
+ *   combined_chns_map - The status of channel opening or closing is set in
+ *              the form of bitmap.  For example, 0x51 means that channel
+ *              0,4, and 6 will be turned on, and others will be turned off.
+ *
+ * Return:
+ *   0 on success; -1 on failure
+ *
+ ****************************************************************************/
+
+int netlib_ethtool_setchannel(FAR const char *ifname,
+                              uint32_t combined_chns_map)
+{
+  struct ethtool_chns2 chns;
+
+  /* Set device combined channel */
+
+  chns.cmd = ETHTOOL_SCHANNELS2;
+  chns.rx_chns_map = 0;
+  chns.tx_chns_map = 0;
+  chns.combined_chns_map = combined_chns_map;
+
+  return netlib_ethtool_ioctl(ifname, &chns);
+}

Reply via email to