You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/09/08 06:48:45 UTC

[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a diff in pull request #7004: net/procfs: add tcp profs support (IPv4 only)

xiaoxiang781216 commented on code in PR #7004:
URL: https://github.com/apache/incubator-nuttx/pull/7004#discussion_r965563936


##########
net/procfs/net_tcp.c:
##########
@@ -0,0 +1,172 @@
+/****************************************************************************
+ * net/procfs/net_tcp.c
+ *
+ * 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 <nuttx/config.h>
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <debug.h>
+
+#include <arpa/inet.h>
+#include <netinet/in.h>
+
+#include <nuttx/net/netstats.h>
+
+#include "procfs/procfs.h"
+#include "tcp/tcp.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#if defined(CONFIG_NET_TCP) && defined(CONFIG_NET_IPv4)
+
+#define TCP_LINELEN 120
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: netprocfs_read_tcpstats
+ *
+ * Description:
+ *   Read and format TCP statistics.
+ *
+ * Input Parameters:
+ *   priv - A reference to the network procfs file structure
+ *   buffer - The user-provided buffer into which network status will be
+ *            returned.
+ *   bulen  - The size in bytes of the user provided buffer.
+ *
+ * Returned Value:
+ *   Zero (OK) is returned on success; a negated errno value is returned
+ *   on failure.
+ *
+ ****************************************************************************/
+
+ssize_t netprocfs_read_tcpstats(FAR struct netprocfs_file_s *priv,
+                                FAR char *buffer, size_t buflen)
+{
+  FAR struct tcp_conn_s *conn = NULL;
+  char remote[INET_ADDRSTRLEN + 1];
+  char local[INET_ADDRSTRLEN + 1];
+  struct in_addr laddr;
+  struct in_addr raddr;
+  int skip = 1;
+  int len = 0;
+
+  net_lock();
+
+  if (tcp_nextconn(NULL) == NULL)
+    {
+      goto end_lock;
+    }
+
+  if (priv->offset == 0)
+    {
+      len = snprintf(buffer, buflen, "TCP sl     "
+                                     "local_address          "
+                                     "remote_address     "
+                                     "st flg ref tmr uack nrt   "
+#if CONFIG_NET_SEND_BUFSIZE > 0
+                                     "txsz   "
+#endif
+                                     "rxsz"
+#ifdef CONFIG_NET_IPv6
+                                     "  (IPv4)"
+#endif /* CONFIG_NET_IPv6 */
+                                     "\n");
+      priv->offset = 1;
+    }
+
+  local[INET_ADDRSTRLEN] = '\0';
+  remote[INET_ADDRSTRLEN] = '\0';
+
+  while ((conn = tcp_nextconn(conn)) != NULL)
+    {
+#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
+      if (conn->domain != PF_INET)
+        {
+          continue;
+        }
+#endif /* CONFIG_NET_IPv4 && CONFIG_NET_IPv6 */
+
+      if (++skip <= priv->offset)
+        {
+          continue;
+        }
+
+      if (buflen - len < TCP_LINELEN)
+        {
+          break;
+        }
+
+      laddr.s_addr = conn->u.ipv4.laddr;
+      raddr.s_addr = conn->u.ipv4.raddr;
+
+      len += snprintf(buffer + len, buflen - len,
+                      " %2" PRIu8 ": %16s:%6" PRIu16 " %16s:%6" PRIu16,
+                      priv->offset++,
+                      inet_ntoa_r(laddr, local, INET_ADDRSTRLEN),
+                      ntohs(conn->lport),
+                      inet_ntoa_r(raddr, remote, INET_ADDRSTRLEN),

Review Comment:
   ditto



##########
net/procfs/net_tcp.c:
##########
@@ -0,0 +1,172 @@
+/****************************************************************************
+ * net/procfs/net_tcp.c
+ *
+ * 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 <nuttx/config.h>
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <debug.h>
+
+#include <arpa/inet.h>
+#include <netinet/in.h>
+
+#include <nuttx/net/netstats.h>
+
+#include "procfs/procfs.h"
+#include "tcp/tcp.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#if defined(CONFIG_NET_TCP) && defined(CONFIG_NET_IPv4)
+
+#define TCP_LINELEN 120
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: netprocfs_read_tcpstats
+ *
+ * Description:
+ *   Read and format TCP statistics.
+ *
+ * Input Parameters:
+ *   priv - A reference to the network procfs file structure
+ *   buffer - The user-provided buffer into which network status will be
+ *            returned.
+ *   bulen  - The size in bytes of the user provided buffer.
+ *
+ * Returned Value:
+ *   Zero (OK) is returned on success; a negated errno value is returned
+ *   on failure.
+ *
+ ****************************************************************************/
+
+ssize_t netprocfs_read_tcpstats(FAR struct netprocfs_file_s *priv,
+                                FAR char *buffer, size_t buflen)
+{
+  FAR struct tcp_conn_s *conn = NULL;
+  char remote[INET_ADDRSTRLEN + 1];
+  char local[INET_ADDRSTRLEN + 1];
+  struct in_addr laddr;
+  struct in_addr raddr;
+  int skip = 1;
+  int len = 0;
+
+  net_lock();
+
+  if (tcp_nextconn(NULL) == NULL)
+    {
+      goto end_lock;
+    }
+
+  if (priv->offset == 0)
+    {
+      len = snprintf(buffer, buflen, "TCP sl     "
+                                     "local_address          "
+                                     "remote_address     "
+                                     "st flg ref tmr uack nrt   "
+#if CONFIG_NET_SEND_BUFSIZE > 0
+                                     "txsz   "
+#endif
+                                     "rxsz"
+#ifdef CONFIG_NET_IPv6
+                                     "  (IPv4)"
+#endif /* CONFIG_NET_IPv6 */
+                                     "\n");
+      priv->offset = 1;
+    }
+
+  local[INET_ADDRSTRLEN] = '\0';
+  remote[INET_ADDRSTRLEN] = '\0';
+
+  while ((conn = tcp_nextconn(conn)) != NULL)
+    {
+#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
+      if (conn->domain != PF_INET)
+        {
+          continue;
+        }
+#endif /* CONFIG_NET_IPv4 && CONFIG_NET_IPv6 */
+
+      if (++skip <= priv->offset)
+        {
+          continue;
+        }
+
+      if (buflen - len < TCP_LINELEN)
+        {
+          break;
+        }
+
+      laddr.s_addr = conn->u.ipv4.laddr;
+      raddr.s_addr = conn->u.ipv4.raddr;
+
+      len += snprintf(buffer + len, buflen - len,
+                      " %2" PRIu8 ": %16s:%6" PRIu16 " %16s:%6" PRIu16,
+                      priv->offset++,
+                      inet_ntoa_r(laddr, local, INET_ADDRSTRLEN),

Review Comment:
   let's call inet_ntop to support both ipv4 and ipv6



##########
net/procfs/Make.defs:
##########
@@ -33,6 +33,11 @@ ifeq ($(CONFIG_NET_STATISTICS),y)
 ifeq ($(CONFIG_NET_MLD),y)
   NET_CSRCS += net_mld.c
 endif
+ifeq ($(CONFIG_NET_IPv4),y)

Review Comment:
   remove let's support ipv6 too



##########
net/procfs/net_udp.c:
##########
@@ -0,0 +1,157 @@
+/****************************************************************************
+ * net/procfs/net_udp.c
+ *
+ * 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 <nuttx/config.h>
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <debug.h>
+
+#include <arpa/inet.h>
+#include <netinet/in.h>
+
+#include <nuttx/net/netstats.h>
+
+#include "procfs/procfs.h"
+#include "udp/udp.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#if defined(CONFIG_NET_UDP) && defined(CONFIG_NET_IPv4)
+
+#define UDP_LINELEN 120
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: netprocfs_read_udpstats
+ *
+ * Description:
+ *   Read and format TCP statistics.
+ *
+ * Input Parameters:
+ *   priv - A reference to the network procfs file structure
+ *   buffer - The user-provided buffer into which network status will be
+ *            returned.
+ *   bulen  - The size in bytes of the user provided buffer.
+ *
+ * Returned Value:
+ *   Zero (OK) is returned on success; a negated errno value is returned
+ *   on failure.
+ *
+ ****************************************************************************/
+
+ssize_t netprocfs_read_udpstats(FAR struct netprocfs_file_s *priv,
+                                FAR char *buffer, size_t buflen)
+{
+  FAR struct udp_conn_s *conn = NULL;
+  char remote[INET_ADDRSTRLEN + 1];
+  char local[INET_ADDRSTRLEN + 1];
+  struct in_addr laddr;
+  struct in_addr raddr;
+  int skip = 1;
+  int len = 0;
+
+  net_lock();
+
+  if (udp_nextconn(NULL) == NULL)
+    {
+      goto end_lock;
+    }
+
+  if (priv->offset == 0)
+    {
+      len = snprintf(buffer, buflen, "UDP sl     "
+                                     "local_address          "
+                                     "remote_address    "
+                                     " flg   "
+#if CONFIG_NET_SEND_BUFSIZE > 0
+                                     "txsz   "
+#endif
+                                     "rxsz"
+#ifdef CONFIG_NET_IPv6
+                                     "  (IPv4)"

Review Comment:
   remove, let's support both ipv4 and ipv6



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org