0

I was reading about SockFS, and I'm wondering why there seems to be no mount driver for it. That is, if SockFS implements the VFS API for UDP and TCP sockets, why is there no userspace filesystem representation of sockets, such that I could cat /net/tcp/151.101.65.69/80 and echo "GET /v1/users/some-id HTTP/1.1" > /net/tcp/192.168.0.5/8000 or something?

In fact, with Unix socket implementing the same socket API as TCP and UDP sockets, and also having filesystem representations, this makes even less sense.

EDIT: Found out about /proc/*/fd/* socket files/symlinks from What's the meaning of [socket:number] in /proc/pid/fd, though they don't seem very useful since you can't read or write from/to them.

root@drpyser-thinkpad[192.168.8.224] in /proc/1
❯ ll fd/18
lrwx------ root root 64 B Fri Feb 24 13:54:16 2023  fd/18 ⇒ socket:[14810]

root@drpyser-thinkpad[192.168.8.224] in /proc/1 ❯ file fd/18 fd/18: symbolic link to socket:[14810]

root@drpyser-thinkpad[192.168.8.224] in /proc/1 ❯ socket:[14810]^C

root@drpyser-thinkpad[192.168.8.224] in /proc/1 ❯ cat fd/18 cat: fd/18: No such device or address

root@drpyser-thinkpad[192.168.8.224] in /proc/1 ✖1 ❯ cat > fd/18 warning: An error occurred while redirecting file 'fd/18' open: No such device or address

EDIT2: Beside Plan 9, I also found this implementation of the concept for FreeBSD: http://csr.bu.edu/icnp2005/posters/netfs_abstract.pdf.

1 Answers1

0

why is there no userspace filesystem representation of sockets, such that I could cat /net/tcp/151.101.65.69/80 and echo "GET /v1/users/some-id HTTP/1.1" > /net/tcp/192.168.0.5/8000 or something?

Two different things. SockFS represents sockets that have been created in some way that is consistent with having file descriptors.

What you want is a userland file system that does something completely different! It's hiding complex actions like creating a new socket, establishing a connection to a remote host and starting to send and accept data behind file names. Not the same thing as having a structure to represent a socket descriptor that's already been established in something VFS-compatible, at all!

I'm told the Plan9 operating system happens to have exactly such a file system hierarchy as you want, by the way.

So, probably someone somewhere already wrote what you want; and the both of us just don't know of it. Matter of fact, I think zsh emulates that when it sees you redirecting something to a specific string instead of an actual file name: establish a TCP connection to the specified host/port and send the redirected characters there.

Anyway, that's not actually a file system. If you wanted to write one, go ahead: FUSE isn't that hard, and albeit certainly limited in performance, there's nothing obvious that would stop you from implementing what you describe. It does get a bit confusing, though! Multiple processes could both open /net/tcp/192.168.0.5/8000, and would you expect that to be the same connection, or two separate connections? You would need to be a bit careful with what you do on that file system!

  • I was not expecting SockFS to create sockets if I created files on the filesystem, no. Rather, existing sockets available in SockFS would simply appear as files under some mountpoint, and the VFS implementation of SockFS would do the job of providing the system calls for those applications that interact with the filesystem, such as shells and cat and such. Creating sockets would be done with existing socket-creation utilities. – Charles Langlois Feb 28 '23 at 22:42
  • I do know about plan9, which inspires my expectation perhaps. But considering other virtual filesystems and "filesystem objects" exist in linux that do appear in the userspace filesystem hierarchy, I'm not understanding this inconsistency. – Charles Langlois Feb 28 '23 at 22:45
  • It does get a bit confusing, though! Multiple processes could both open /net/tcp/192.168.0.5/8000, and would you expect that to be the same connection, or two separate connections?

    That's a good point. How does SockFS work is the question. I can find very little documentation on SockFS, which is suspicious.

    – Charles Langlois Feb 28 '23 at 22:58
  • Again sockfs does not do that at all. It's just apsedo-filesystem hierarchy just to be able to deal with socket descriptors the way we deal with file descriptors. – Marcus Müller Mar 01 '23 at 06:50
  • My question is why though. Is it just an omission because of the challenges? Or asked another way, what is missing technically to allow one to mount SockFS into the filesystem view alongside other virtual FS? – Charles Langlois Mar 01 '23 at 20:18
  • And actually, is the open syscall implemented for sockfs? What does it do? – Charles Langlois Mar 01 '23 at 20:25
  • "what is missing technically": A different file system for the main file system hierarchy. Sockfs under Linux has absolutely no relation to what you want to achieve. The moment you make a new socket using the socket call, you get a socket descriptor that behaves a lot like a file descriptor. File descriptors are part of some file system hierarchy. Sockfs is basically nothing but the "do nothing" file system hierarchy, so that socket descriptors really can be handled like file descriptors. It literally has nothing to do with what you want to achieve. – Marcus Müller Mar 01 '23 at 20:27
  • I think the problem here is that it's called sockfs. If it was called helper_data_structure_so_that_we_dont_have_to_do_if_else_in_every_read_and_write_call_to_treat_file_descriptors_differently_from_socket_descriptors_fs, you would have no problem understanding it :) – Marcus Müller Mar 01 '23 at 20:42
  • Indeed the name is confusing(again in comparison with other virtual filesystem implementations). But in my questions I'm trying to understand sockfs and the VFS abstraction better. I guess I now understand that it's more of an helper & implementation detail than an actual useful abstraction outside the kernel. – Charles Langlois Mar 02 '23 at 17:13
  • 1
    I suppose I should be asking a separate question in order to understand what's necessary to implement a userspace VFS for networking? Or for that matter the difference between the unix socket interface and other socket types. – Charles Langlois Mar 02 '23 at 17:19