Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async TCP networking API sketch #28

Open
maximecb opened this issue Oct 22, 2023 · 1 comment
Open

Async TCP networking API sketch #28

maximecb opened this issue Oct 22, 2023 · 1 comment
Labels
help wanted Extra attention is needed proposal Proposal for a new feature/API

Comments

@maximecb
Copy link
Owner

I'm in the process of designing a simple TCP networking API that would make it possible to create servers running in UVM. This API works with callbacks that tell you when a new connection request is incoming, or when data is available to read. Then, you can call the listen or read functions to accept a connection or read incoming data. If there is no incoming connection or data, then these functions will blocks, so the API can also be used in a synchronous mode as well. Having to call read functions also makes it so we don't have to preallocate global buffers to receive data, we can simply pass pointers to those buffers when calling the functions to read data.

// Syscall to create a TCP listening socket to accept incoming connections
u64 socket_id = net_listen_tcp(
  u16 port_no,
  ip_space, // IPV4 / IPV6
  const char* net_iface, // Network interface address to listen on, null for any address
  callback on_new_connection, // Called on new incoming connection
  u64 flags // optional flags, default 0
)

The callback to be notified of an incoming connection request has the form:

void on_new_connection(u64 socket_id)

To accept new connections:

// Syscall to accept a new connection
// Gives you the client address in the buffer you define
// Will block if there is no incoming connection request
u64 socket_id = net_accept(u64 socket_id, client_addr_t *client_addr, callback on_incoming_data)

When there is incoming data to be read, we can be notified by a callback:

// Callback to notify you that incoming data is available to read
void on_incoming_data(u64 socket_id, u64 num_bytes)

To read and write data:

// Syscall to read data from a given socket into a buffer you specify
u64 num_bytes_read = net_read(u64 socket_id, void* buffer, u64 buf_len)

// Syscall to write data on a given socket
void net_write(u64 socket_id, void* buffer, u64 buf_len);

Finally, to close the socket:

// Syscall to close a socket
net_close(socket_id)
@maximecb maximecb added help wanted Extra attention is needed proposal Proposal for a new feature/API labels Oct 22, 2023
@maximecb
Copy link
Owner Author

maximecb commented Nov 7, 2023

I have a WIP version of this working in: https://github.com/maximecb/uvm/blob/main/vm/src/sys/net.rs

And an example with a trivial telnet server: https://github.com/maximecb/uvm/blob/main/ncc/examples/telnet_server.c

I'm able to connect and get a response :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed proposal Proposal for a new feature/API
Projects
None yet
Development

No branches or pull requests

1 participant