From d7a7e2d17d9a62f303d00a13aac77c2e778147aa Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Tue, 1 Jun 2021 15:53:29 -0700 Subject: [PATCH] move msghdr to Go --- net/uring/io_uring.c | 13 ++----------- net/uring/io_uring_linux.go | 5 ++++- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/net/uring/io_uring.c b/net/uring/io_uring.c index 164daa0cd..371552bd8 100644 --- a/net/uring/io_uring.c +++ b/net/uring/io_uring.c @@ -15,6 +15,7 @@ // TODO: use fixed buffers? https://unixism.net/loti/tutorial/fixed_buffers.html typedef struct io_uring go_uring; +typedef struct msghdr go_msghdr; // Wait for a completion to be available, fetch the data static int receive_into(int sock, struct io_uring *ring, char *ip, uint16_t *port) { @@ -50,7 +51,6 @@ again:; free(mhdr->msg_iov); free(mhdr->msg_name); - free(mhdr); io_uring_cqe_seen(ring, cqe); return n; @@ -58,17 +58,10 @@ again:; // submit a recvmsg request via liburing // TODO: What recvfrom support arrives, maybe use that instead? -static int submit_recvmsg_request(int sock, struct io_uring *ring, char *buf, int buflen) { - struct msghdr *mhdr = malloc(sizeof(struct msghdr)); - if (!mhdr) { - perror("malloc(msghdr)"); - return 1; - } - +static int submit_recvmsg_request(int sock, struct io_uring *ring, struct msghdr *mhdr, char *buf, int buflen) { struct iovec *iov = malloc(sizeof(struct iovec)); if (!iov) { perror("malloc(iov)"); - free(iov); return 1; } @@ -76,7 +69,6 @@ static int submit_recvmsg_request(int sock, struct io_uring *ring, char *buf, in if (!sender) { perror("malloc(sender)"); free(iov); - free(mhdr); return 1; } @@ -84,7 +76,6 @@ static int submit_recvmsg_request(int sock, struct io_uring *ring, char *buf, in iov->iov_base = buf; iov->iov_len = buflen; - memset(mhdr, 0, sizeof(*mhdr)); mhdr->msg_iov = iov; mhdr->msg_iovlen = 1; diff --git a/net/uring/io_uring_linux.go b/net/uring/io_uring_linux.go index 29c401d02..09ad7970c 100644 --- a/net/uring/io_uring_linux.go +++ b/net/uring/io_uring_linux.go @@ -9,6 +9,7 @@ "fmt" "net" "os" + "runtime" "sync" "time" "unsafe" @@ -69,8 +70,9 @@ func (u *UDPConn) ReadFromNetaddr(buf []byte) (int, netaddr.IPPort, error) { if u.fd == 0 { return 0, netaddr.IPPort{}, errors.New("invalid uring.UDPConn") } + mhdr := new(C.go_msghdr) // TODO: eventually separate submitting the request and waiting for the response. - errno := C.submit_recvmsg_request(u.fd, u.ptr, (*C.char)(unsafe.Pointer(&buf[0])), C.int(len(buf))) + errno := C.submit_recvmsg_request(u.fd, u.ptr, mhdr, (*C.char)(unsafe.Pointer(&buf[0])), C.int(len(buf))) if errno < 0 { return 0, netaddr.IPPort{}, fmt.Errorf("uring.UDPConn recv failed: %v", errno) // TODO: Improve errno } @@ -82,6 +84,7 @@ func (u *UDPConn) ReadFromNetaddr(buf []byte) (int, netaddr.IPPort, error) { return 0, netaddr.IPPort{}, errors.New("something wrong") } ipp := netaddr.IPPortFrom(netaddr.IPFrom4(*a), uint16(port)) + runtime.KeepAlive(mhdr) return int(n), ipp, nil }