Skip to content

Conversation

@zhhyu7
Copy link
Contributor

@zhhyu7 zhhyu7 commented Dec 25, 2025

Summary

To avoid memory waste, can add support for using an independent iob buffer
The IP protocol often configures CONFIG_IOB_BUFSIZE to be relatively large,
such as 512, for higher throughput. However, the buffer required by CAN is
fixed at a length of 16 or 64. If the system's IOB is directly used,
a lot of memory will be wasted.

Impact

net/can and can device driver

Testing

qemu:local
test code:

#include <nuttx/can/can.h>
#include <nuttx/config.h>
#include <stdint.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

#define CAN_RCV_BUF_MAX 1024

void dump_can_msg(struct can_msg_s* msg, bool tx)
{
    if(tx)
      printf("---> TX: ");
    else
      printf("<--- RX: ");

    printf("ID 0x%02x, Data ", msg->cm_hdr.ch_id);
    for(int i=0;i<can_dlc2bytes(msg->cm_hdr.ch_dlc);i++)
    {
      printf(" 0x%02x ", msg->cm_data[i]);
    }
    printf("\n");
}

void build_tx_msg(struct can_msg_s* msg)
{
  static uint32_t echo_cnt = 0x120;
  memset(msg, 0, sizeof(struct can_msg_s));
  msg->cm_hdr.ch_id = 0x123;
  msg->cm_hdr.ch_dlc = 8;
  msg->cm_data[0] = echo_cnt & 0xff;
  msg->cm_data[1] = (echo_cnt >> 8) & 0xff; 

  echo_cnt++;
}

int main(int argc, FAR char *argv[])
{
  printf("Hello, World!!\n");
  char buf[CAN_RCV_BUF_MAX];
  int nbytes;
  int ret;
  struct can_msg_s* rxmsg;
  struct can_msg_s txmsg;

  int fd = open("/dev/can0", O_RDWR | O_CLOEXEC);
  if (fd < 0) {
    printf("open failed\n");
    return -1;
  }
  
  while(1)
  {
    sleep(1);
    build_tx_msg(&txmsg);
    dump_can_msg(&txmsg, true);
    ret = write(fd, &txmsg, sizeof(struct can_msg_s));
    if(ret < 0)
    {
      printf("write failed\n");
      return -1;
    }

    nbytes = read(fd, buf, CAN_RCV_BUF_MAX);
    if(nbytes < 0) 
    {
      printf("read failed\n");
      return -1;
    }
    rxmsg = (struct can_msg_s*)buf;
    dump_can_msg(rxmsg, false);    

  }
  close(fd);

  return 0;
}

test log:

NuttShell (NSH) NuttX-12.11.0
vela> hello
Hello, World!!
---> TX: ID 0x123, Data  0x20  0x01  0x00  0x00  0x00  0x00  0x00  0x00
<--- RX: ID 0x07, Data  0x20  0x01  0x00  0x00
...

@github-actions github-actions bot added Area: Networking Effects networking subsystem Size: M The size of the change in this PR is medium labels Dec 25, 2025
To avoid memory waste, can add support for using an independent iob buffer
The IP protocol often configures CONFIG_IOB_BUFSIZE to be relatively large,
such as 512, for higher throughput. However, the buffer required by CAN is
fixed at a length of 16 or 64. If the system's IOB is directly used,
a lot of memory will be wasted.

Signed-off-by: zhanghongyu <[email protected]>
when the network card type is NET_LL_CAN, packets are defaultly
allocated from the can buffer.

Signed-off-by: zhanghongyu <[email protected]>
@acassis acassis merged commit 6144b51 into apache:master Dec 25, 2025
63 of 76 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: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants