arch/sim: Add host socket and epoll APIs#19369
Conversation
77c5256 to
5012caa
Compare
5012caa to
223ec07
Compare
Add host-side helpers for socket, epoll and additional file operations so sim applications can call selected host Linux APIs without being affected by NuttX symbol renaming. Move the socket ABI definitions shared with usrsock into a common host socket header, and keep usrsock-specific structures in the usrsock header. Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
223ec07 to
5b18907
Compare
|
@LingaoM please include proper testing. Just "build completed successfully" is not enough. |
acassis
left a comment
There was a problem hiding this comment.
@LingaoM you wrote a good introduction to sim host layer and currently our Documentation about SIM is very poor: https://nuttx.apache.org/docs/latest/platforms/sim/sim/index.html
Please add this Summary as an Introduction to SIM, to let people understand how the SIM solves the functions names collision, etc
linguini1
left a comment
There was a problem hiding this comment.
Can you please tell us some use cases/test cases where you used these new host interfaces? If you have any logs that would be good to add.
| int host_access(const char *path, int mode); | ||
| int host_chstat(const char *path, | ||
| const struct nuttx_stat_s *buf, int flags); | ||
| nuttx_file_t host_popen(const char *command, const char *mode); |
There was a problem hiding this comment.
All functions defined in hostfs.h aren't designed for application developer but consumed by https://github.com/apache/nuttx/tree/master/fs/hostfs.
I don't see that you modify the code under fs/hostfs/ to call new functions.
BTW, any application which call host_xxx function directly will be locked on sim board.
|
|
||
| int host_socket(int domain, int type, int protocol); | ||
| int host_bind(int fd, const struct host_socket_addr *addr); | ||
| int host_connect(int fd, const struct host_socket_addr *addr); |
There was a problem hiding this comment.
it's wrong to call the host socket api directly, since many functions (e.g. accept/connect/send/recv) will block the whole system, not just the current thread.
| return ret < 0 ? host_errno_convert(-errno) : ret; | ||
| } | ||
|
|
||
| int host_epoll_wait(int epoll_fd, struct host_epoll_event *events, |
There was a problem hiding this comment.
epoll_wait will block the whole OS, not just the current thread, which is definitely not you want behaviour.
but we should use or improve: https://github.com/apache/nuttx/blob/master/arch/sim/src/sim/posix/sim_hosthcisocket.c
should use usersock: https://github.com/apache/nuttx/blob/master/arch/sim/src/sim/posix/sim_hostusrsock.c
basically, you can't call Linux epoll api directly, since epoll_wait will block the whole OS, not just the calling thread.
should use: https://github.com/apache/nuttx/tree/master/fs/hostfs
it's wrong design to expose host api to application, since any application which call these functions will be locked on sim platform. Actually, all your modification already implements through the standard interface (hostfs/usersock/hci socket), why do you not use them?
All these functions can be achieved through usersock. IPv4/IPv6 is used daily in our project. Unix/Netlink/bluetooth need test.
again, you can't call host socket api directly, otherwise the blocking call will block the whole system. |
|
@xiaoxiang781216 Thanks for the review. I agree with the general design rule here: sim applications should not call blocking host APIs directly, because a blocking host call can block the whole simulated OS. I also agree that the long-term upstream direction should be standard NuttX interfaces, not application-visible host APIs. The reason I did not simply switch everything to the existing standard backends is that the current implementations do not fully cover this our use case yet.
|
|
One more point: this was not originally intended as a NuttX core change. Our first choice was to keep this entirely inside the application/porting layer. However, in sim builds NuttX intentionally renames libc/POSIX symbols before the final link, so application code cannot reliably opt in to selected host Linux APIs by itself. That restriction is real and is defined by the sim symbol renaming mechanism. If there is an accepted way for sim-only application code to explicitly opt in to host Linux APIs without bypassing NuttX’s normal interfaces, I am happy to move this PR out of the core changes or close that. Actually, I also think this may be the cleaner direction: provide an explicit sim-only opt-in mechanism for applications that intentionally need host Linux APIs. The default NuttX behavior should remain unchanged: applications should use standard NuttX interfaces, and libc/POSIX symbols should continue to be renamed to avoid accidental host binding. With that mechanism, we could keep these host-Linux shims in the application/porting layer and avoid adding workload-specific APIs to the NuttX core repository. The NuttX core would only provide the opt-in path; the app would decide whether to use it. |
it's better to improve usrsock infrastructure to allow forward more socket type to host.
Yes, I think so.
Why not use NuttX file lock support? but want forward to host fs. BTW, it plans to add file lock/unlock into mount_operation to support the hostfs/virtiofs/nfs usage.
|
NuttX sim renames many libc and POSIX symbols before the final link so
that NuttX applications do not accidentally bind to the host libc. This
is the right default for normal simulated applications, but it also makes
it difficult for selected sim-only code to intentionally access host
Linux interfaces such as Bluetooth HCI user channel sockets, Unix/TCP
sockets, epoll, and host file operations.
Add a small set of host-side wrappers for these interfaces. The new
helpers live in the sim host layer and are compiled as host objects, so
callers can explicitly opt in to host socket, epoll, and file operations
without bypassing the normal NuttX symbol namespace for the rest of the
application.
The socket helpers provide conversion between NuttX socket constants and
host socket constants, including support for Unix, IPv4, IPv6, Netlink,
and Bluetooth HCI socket addresses. Linux-only epoll support is kept in
a separate host epoll file and is only built for CONFIG_HOST_LINUX.
Extend the existing hostfs wrapper with additional file operations used
by sim applications, including pread, pwrite, fcntl, fsync, remove,
access, popen, fgets, and pclose.
Also split the socket ABI definitions that were previously local to
sim_hostusrsock.h into a shared sim_hostsock.h header. Definitions that
are specific to usrsock, such as message/control-message structures and
usrsock event flags, remain in sim_hostusrsock.h.
Testing:
Host:
Board/configuration:
Commands:
Result: