SocketCAN: How to query buffer usage

4.7k views Asked by At

I am trying to determine the buffer usage for my CAN interface on linux.

At the level of the interface, I know of two ways to get statistics:

  • From /proc/net/can/stats
  • ip -details -statistics link show can0

The first one gives me the number of transmitted/received frames. The second one gives the number of transmitted/received bytes and packets, as well as the number of dropped packets and overruns. Unfortunately neither of these shows the current number of frames/bytes in the buffer.

Note: I know that we can set the txqueuelen (ifconfig can0 txqueuelen <len>), and setting it too low triggers a write: No buffer space available when it fills up. However I still can't get the number of frames in the queue at any time.

At the socket level (the socket was opened like this: socket(PF_CAN, SOCK_RAW, CAN_RAW)), I've tried to query the input/output queue size (as can be done on an internet socket):

int queued_in = -1;
ioctl(sockfd, SIOCINQ, &queued_in);
int queued_out = -1;
ioctl(sockfd, SIOCOUTQ, &queued_out);

But the value remains -1 for both variables so it wasn't written to. What am I doing wrong here?

1

There are 1 answers

0
Jos Seldenthuis On

I'm not aware of any way to query the current size of kernel buffers, only their maximum capacity. However, you can find some statistics of the transmit queue under /sys/class/net/can0/queues/tx-0 and similar directories.

Even if you could get the current number of frames in a kernel buffer, answering your question is complex because there are multiple buffers involved. Apart from the transmit queue of the network interface, there is also the send buffer of the socket(s). The interaction between those two can cause some unexpected behavior (see section 3.4 in https://rtime.felk.cvut.cz/can/socketcan-qdisc-final.pdf for more details). On top of that, Linux allows you to add extra queues with different queuing disciplines (also explained in that PDF). You would have to add the size of all those buffers together to get the total number of buffered frames. But those buffers probably change more quickly than the time it takes to query their size.