WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@HedongGao
Copy link
Contributor

@HedongGao HedongGao commented Dec 27, 2025

Summary

When the interface bound to the socket is in the down state, polling the socket will return an error message, which in some cases will cause a busy loop.
The function call stack is roughly as follows: poll->pkt_pollsetup->pkt_callback_alloc->devif_callback_alloc. Then return NULL if interface is down.
To fix this problem, we modified the error judgment condition in devif_callback_alloc.

Impact

No impact on functions

Testing

Set up a sim environment, start the sim and ifdown eth0, then start the poll program, and repeat that the poll is always in the busy loop. After the current patch is marked, the problem is fixed.

`
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/poll.h>
#include <netpacket/packet.h>
#include <net/if.h>
#include <errno.h>
#include <arpa/inet.h>

#define TEST_DATA "Hello, PKT poll!"
#define PKT_IFNAME "eth0" // 使用eth0接口

#ifndef ETH_P_ALL
#define ETH_P_ALL 0x0003
#endif

int main(void)
{
int sockfd;
struct sockaddr_ll addr;
struct pollfd pfd;
char buf[128];
int ret;

sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (sockfd < 0) {
    perror("socket");
    return 1;
}

memset(&addr, 0, sizeof(addr));
addr.sll_family = AF_PACKET;
addr.sll_protocol = htons(ETH_P_ALL);
addr.sll_ifindex = if_nametoindex(PKT_IFNAME);
if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("bind");
    close(sockfd);
    return 1;
}


pfd.fd = sockfd;
pfd.events = POLLIN;

printf("Waiting for data with poll...\n");

while (1)
{
  ret = poll(&pfd, 1, 3000); 
  if (ret < 0) {
      perror("poll");
      close(sockfd);
      return 1;
  } else if (ret == 0) {
      printf("poll timeout, no data received\n");
      close(sockfd);
      return 1;
  }

  if (pfd.revents & POLLIN) {
      ret = recv(sockfd, buf, sizeof(buf) - 1, 0);
      if (ret < 0) {
          perror("recv");
      } else {
          buf[ret] = '\0';
          printf("Received data: %s\n", buf);
      }
  } else {
      printf("Unexpected poll revents: 0x%x\n", pfd.revents);
  }
}


close(sockfd);
return 0;

}
`

When the interface is in a down state, it triggers a poll busy loop issue.

Signed-off-by: gaohedong <[email protected]>
@github-actions github-actions bot added Area: Networking Effects networking subsystem Size: XS The size of the change in this PR is very small labels Dec 27, 2025
@xiaoxiang781216 xiaoxiang781216 merged commit 8638e9c into apache:master Dec 29, 2025
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Networking Effects networking subsystem Size: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants