zhhyu7 opened a new pull request, #17732:
URL: https://github.com/apache/nuttx/pull/17732

   ## Summary
   remove the use of net_lock in the netlink module and decouple it from other 
network modules.
   
   ## Impact
   net::netlink
   
   ## Testing
   sim:matter with test code:
   ```
   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>
   #include <unistd.h>
   #include <signal.h>
   #include <errno.h>
   #include <sys/socket.h>
   #include <netpacket/netlink.h>
   #include <net/if.h>
   #include <arpa/inet.h>
   
   #define NETLINK_BUFFER_SIZE 8192
   #define INTERFACE_NAME "eth0"
   
   static volatile int running = 1;
   
   static void signal_handler(int sig) {
       running = 0;
       printf("\nreceive signal %d, exiting...\n", sig);
   }
   
   static void print_interface_state(const char *ifname, int ifindex, unsigned 
int flags) {
       printf("interface: %s (index: %d)\n", ifname, ifindex);
       
       printf("state: ");
       
       if (flags & IFF_UP) {
           printf("up ");
       } else {
           printf("down ");
       }
   
       if (flags & IFF_RUNNING) {
           printf("running ");
       }
   
       printf("\n");
   }
   
   static void parse_netlink_message(struct nlmsghdr *nlh) {
       struct ifinfomsg *ifi;
       struct rtattr *attr;
       int attr_len;
       char ifname[IF_NAMESIZE] = {0};
       
       ifi = (struct ifinfomsg *)NLMSG_DATA(nlh);
       
       if (nlh->nlmsg_type != RTM_NEWLINK && nlh->nlmsg_type != RTM_DELLINK) {
           return;
       }
       
       attr = (struct rtattr *)IFLA_RTA(ifi);
       attr_len = IFLA_PAYLOAD(nlh);
   
       while (RTA_OK(attr, attr_len)) {
           if (attr->rta_type == IFLA_IFNAME) {
               strncpy(ifname, (char *)RTA_DATA(attr), IF_NAMESIZE - 1);
               ifname[IF_NAMESIZE - 1] = '\0';
               break;
           }
           attr = RTA_NEXT(attr, attr_len);
       }
       
       if (strlen(ifname) > 0 && strcmp(ifname, INTERFACE_NAME) == 0) {
           printf("==============================\n");
           printf("event type: %s\n", 
                  nlh->nlmsg_type == RTM_NEWLINK ? "RTM_NEWLINK" : 
"RTM_DELLINK");
           
           print_interface_state(ifname, ifi->ifi_index, ifi->ifi_flags);
       }
   }
   
   int main(int argc, char *argv[]) {
       int sock_fd, ret;
       struct sockaddr_nl src_addr, dest_addr;
       struct nlmsghdr *nlh = NULL;
       struct iovec iov;
       struct msghdr msg;
       char *buffer = NULL;
       
       signal(SIGINT, signal_handler);
       signal(SIGTERM, signal_handler);
       
       sock_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
       
       memset(&src_addr, 0, sizeof(src_addr));
       src_addr.nl_family = AF_NETLINK;
       src_addr.nl_pid = getpid();
       src_addr.nl_groups = RTMGRP_LINK;
       
       bind(sock_fd, (struct sockaddr *)&src_addr, sizeof(src_addr));
       
       buffer = malloc(NETLINK_BUFFER_SIZE);
       
       memset(&msg, 0, sizeof(msg));
       msg.msg_name = &dest_addr;
       msg.msg_namelen = sizeof(dest_addr);
       msg.msg_iov = &iov;
       msg.msg_iovlen = 1;
       
       while (running) {
           iov.iov_base = buffer;
           iov.iov_len = NETLINK_BUFFER_SIZE;
           
           ret = recvmsg(sock_fd, &msg, 0);
           nlh = (struct nlmsghdr *)buffer;
           while (NLMSG_OK(nlh, ret)) {
               if (nlh->nlmsg_type == NLMSG_DONE) {
                   break;
               }
               
               if (nlh->nlmsg_type == NLMSG_ERROR) {
                   struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(nlh);
                   if (err->error != 0) {
                       fprintf(stderr, "Netlink error: %s\n", 
strerror(-err->error));
                   }
                   break;
               }
               
               parse_netlink_message(nlh);
               
               nlh = NLMSG_NEXT(nlh, ret);
           }
       }
       
       free(buffer);
       close(sock_fd);
       
       return 0;
   }
   ```
   NuttX log:
   ```
   NuttShell (NSH) NuttX-12.12.0-RC0
   MOTD: username=admin password=Administrator
   nsh> 
   nsh> hello &
   hello [4:100]
   nsh> 
   nsh> ifdown eth0
   ifdown eth0...OK
   nsh> ==============================
   event type: RTM_NEWLINK
   interface: eth0 (index: 1)
   state: up 
   ==============================
   event type: RTM_DELLINK
   interface: eth0 (index: 1)
   state: down 
   
   nsh> 
   nsh> ifup eth0
   ifup eth0...OK
   nsh> ==============================
   event type: RTM_DELLINK
   interface: eth0 (index: 1)
   state: down running 
   ==============================
   event type: RTM_NEWLINK
   interface: eth0 (index: 1)
   state: up running 
   
   nsh> 
   ```
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to