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

lib/posix-socket: Socket events #1224

Merged
merged 2 commits into from Dec 23, 2023

Conversation

skuenzer
Copy link
Member

Base target

  • Architecture(s): [N/A]
  • Platform(s): [N/A]
  • Application(s): [N/A]

Additional configuration

  • CONFIG_LIBPOSIX_SOCKET=y
  • CONFIG_LIBPOSIX_SOCKET_EVENTS=y

Description of changes

POSIX-Socket events are events that can be used to track socket connections. As part of the provided information, also the endpoint addresses (local and remote) are reported. Because the implementation is purely integrated to lib/posix-socket, it is independent of the target address family or underlying network stack or implementation.
An event is emitted in the following situations:

  • LISTEN: The host listens on a given address for incoming connections
  • ACCEPT: An incoming connection is accepted
  • CONNECT: A connection to a remote endpoint is established
  • CLOSE: A connection or listener is closed

The following snippet is a short example that demonstrates the integration and receiving of event messages: example.c:

#include <sys/socket.h>
#include <uk/socket_event.h>
#include <uk/print.h>
#include <uk/hexdump.h>

static const char *evname(enum uk_socket_event_type event)
{
	switch (event) {
	case UK_SOCKET_EVENT_LISTEN:
		return "listen";
	case UK_SOCKET_EVENT_ACCEPT:
		return "accept";
	case UK_SOCKET_EVENT_CONNECT:
		return "connect";
	case UK_SOCKET_EVENT_CLOSE:
		return "close";
	default:
	}

	return "<undef>";
}

static const char *afname(int family)
{
	switch (family) {
	case AF_UNIX:
		return "unix";
	case AF_INET:
		return "inet";
	case AF_INET6:
		return "inet6";
	default:
	}

	return "<unknown>";
}

static const char *stname(int sockettype)
{
	sockettype &= ~ SOCK_NONBLOCK;
	sockettype &= ~ SOCK_CLOEXEC;

	switch (sockettype) {
	case SOCK_STREAM:
		return "stream";
	case SOCK_DGRAM:
		return "dgram";
	case SOCK_RAW:
		return "raw";
	case SOCK_SEQPACKET:
		return "seqpacket";
	case SOCK_RDM:
		return "rdm";
	default:
	}

	return "<unknown>";
}

static void socket_event(const struct uk_socket_event *e)
{
	uk_pr_crit("- SOCKET EVENT ------------\n");
	uk_pr_crit("socket id: 0x%"__PRIx64"\n", (__u64)e->id);
	uk_pr_crit("event:     %s\n", evname(e->event));
	uk_pr_crit("family:    %s (%u)\n", afname(e->family), e->family);
	uk_pr_crit("type:      %s (%u)\n", stname(e->type), e->type);
	uk_pr_crit("protocol:  %u\n", e->protocol);
	uk_pr_crit("laddr_len: %u\n", e->laddr_len);
	uk_hexdumpk(KLVL_CRIT, e->laddr, e->laddr_len, UK_HXDF_GRPQWORD, 2);
	uk_pr_crit("raddr_len: %u\n", e->raddr_len);
	uk_hexdumpk(KLVL_CRIT, e->raddr, e->raddr_len, UK_HXDF_GRPQWORD, 2);
	uk_pr_crit("===========================\n");
}

UK_SOCKET_EVENT_RECEIVER(AF_INET, socket_event);

This commit introduces the necessary data structures for tracking and
emitting socket events. The original purpose of socket events are the
ability to do connection tracking independent of the target address
family.
An event is emitted in the following situations:
- LISTEN: The host listens on a given address for incoming connections
- ACCEPT: An incoming connection is accepted
- CONNECT: A connection to a remote endpoint is established
- CLOSE: A connection or listener is closed

Checkpatch-Ignore: BLOCK_COMMENT_STYLE
Signed-off-by: Simon Kuenzer <simon@unikraft.io>
This commit integrates the socket event infrastructure so that events are
emitted accordingly.

Signed-off-by: Simon Kuenzer <simon@unikraft.io>
@skuenzer skuenzer requested a review from a team as a code owner December 22, 2023 03:51
@razvand razvand requested review from razvand and removed request for a team December 23, 2023 19:04
@razvand razvand added area/lib Internal Unikraft Microlibrary topic/posix Topics pertaining to POSIX standard lib/posix-socket Abstraction for communication sockets labels Dec 23, 2023
@razvand razvand added this to the v0.16.0 (Telesto) milestone Dec 23, 2023
Copy link
Contributor

@razvand razvand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed-by: Razvan Deaconescu razvand@unikraft.io
Approved-by: Razvan Deaconescu razvand@unikraft.io

@razvand razvand changed the base branch from staging to staging-1224 December 23, 2023 19:09
@razvand razvand merged commit 256636a into unikraft:staging-1224 Dec 23, 2023
9 checks passed
razvand pushed a commit that referenced this pull request Dec 23, 2023
This commit introduces the necessary data structures for tracking and
emitting socket events. The original purpose of socket events are the
ability to do connection tracking independent of the target address
family.
An event is emitted in the following situations:
- LISTEN: The host listens on a given address for incoming connections
- ACCEPT: An incoming connection is accepted
- CONNECT: A connection to a remote endpoint is established
- CLOSE: A connection or listener is closed

Checkpatch-Ignore: BLOCK_COMMENT_STYLE
Signed-off-by: Simon Kuenzer <simon@unikraft.io>
Approved-by: Razvan Deaconescu <razvand@unikraft.io>
Reviewed-by: Razvan Deaconescu <razvand@unikraft.io>
GitHub-Closes: #1224
razvand pushed a commit that referenced this pull request Dec 23, 2023
This commit integrates the socket event infrastructure so that events are
emitted accordingly.

Signed-off-by: Simon Kuenzer <simon@unikraft.io>
Approved-by: Razvan Deaconescu <razvand@unikraft.io>
Reviewed-by: Razvan Deaconescu <razvand@unikraft.io>
GitHub-Closes: #1224
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/lib Internal Unikraft Microlibrary lib/posix-socket Abstraction for communication sockets topic/posix Topics pertaining to POSIX standard
Projects
Status: Done!
Development

Successfully merging this pull request may close these issues.

None yet

2 participants