7

I have a driver, which handles several TCP connections.

Is there a way to perform something similar to user space application api's select/poll()/epoll() in kernel given a list of struct sock's?

Thanks

Dilip Kumar
  • 1,736
  • 11
  • 22
dimba
  • 26,717
  • 34
  • 141
  • 196
  • 3
    Not sure if you have already gone through this link http://stackoverflow.com/questions/11336034/simulate-effect-of-select-and-poll-in-kernel-socket-programming – askb Dec 08 '14 at 13:51
  • @askb Post you comment as an answer and I'll accept it. – dimba Dec 11 '14 at 13:44

2 Answers2

6

You may want to write your own custom sk_buff handler, which calls the kernel_select() that tries to lock the semaphore and does a blocking wait when the socket is open.

Not sure if you have already gone through this link Simulate effect of select() and poll() in kernel socket programming

Community
  • 1
  • 1
askb
  • 6,501
  • 30
  • 43
2

On the kernel side it's easy to avoid using sys_epoll() interface outright. After all, you've got a direct access to kernel objects, no need to jump through hoops.

Each file object, sockets included, "overrides" a poll method in its file_operations "vtable". You can simply loop around all your sockets, calling ->poll() on each of them and yielding periodically or when there's no data available.

If the sockets are fairly high traffic, you won't need anything more than this.

A note on the API:

poll() method requires a poll_table() argument, however if you do not intend to wait on it, it can safely be initialized to null:

poll_table pt;
init_poll_funcptr(&pt, NULL);
...
// struct socket *sk;
...
unsigned event_mask = sk->ops->poll(sk->file, sk, &pt);

If you do want to wait, just play around with the callback set into poll_table by init_poll_funcptr().

oakad
  • 6,945
  • 1
  • 22
  • 31
  • I try to do this on a UDP socket, but as soon as I send a UDP message to the listener, the "poll"-call crashes with `BUG: kernel NULL pointer dereference, address: 0000000000000041`. Independent of if I pass a callback to `init_poll_funcptr`. Do you know why? Thanks – Kevin Meier Oct 14 '20 at 14:25
  • 1. `poll` is optional method, it may not be set on your socket. 2. `file` is a user space thing, it may not be attached to your socket. 3. Other things can be NULL elsewhere. 4. When hacking kernel at this level you should be able to trace the fairly common NULL ptr issue up and down the code. :-) – oakad Oct 14 '20 at 14:43