[osv-dev] Pull requests

2023-08-08 Thread Waldek Kozaczuk
Hi,

I am about to create a number of pull requests to address various things I 
have discovered when working on adding support for static executables in 
OSv. Most of those are minimal or fairly simple and I wonder if I should 
create one large pull request with many commits or if I shall create PR per 
each fixed issue/commit? Preferences?

Regards,
Waldek

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/3be2ab22-dbc0-46d8-b08b-066635e38487n%40googlegroups.com.


[osv-dev] [COMMIT osv master] Support EPOLLRDHUP in AF_LOCAL sockets

2023-08-08 Thread Commit Bot
From: Waldemar Kozaczuk 
Committer: WALDEMAR KOZACZUK 
Branch: master

Support EPOLLRDHUP in AF_LOCAL sockets

On startup, nginx tries to detect if EPOLLRDHUP is supported by epoll
by creating a pair of AF_LOCAL sockets and checking if the event is
reported when one of the sockets is abruptly closed. This causes a 5
seconds delay before nginx can start responding to any traffic.
The code in nginx is actually quite similar to the unit test added by this 
commit.

To address this shortcoming this commit changes underlying pipe_buffer
implementation to report POLLRDHUP instead of POLLERR|POLLOUT when
receiver is disconnected for af_local. It still reports the latter
events for pipes.

Signed-off-by: Waldemar Kozaczuk 

---
diff --git a/libc/af_local.cc b/libc/af_local.cc
--- a/libc/af_local.cc
+++ b/libc/af_local.cc
@@ -65,6 +65,7 @@ int af_local::ioctl(u_long cmd, void *data)
 void af_local::init()
 {
 send->attach_sender(this);
+send->set_no_receiver_event(POLLRDHUP);
 receive->attach_receiver(this);
 }
 
diff --git a/libc/pipe_buffer.cc b/libc/pipe_buffer.cc
--- a/libc/pipe_buffer.cc
+++ b/libc/pipe_buffer.cc
@@ -29,7 +29,7 @@ void pipe_buffer::detach_receiver()
 if (receiver) {
 receiver = nullptr;
 if (sender)
-poll_wake(sender, POLLERR|POLLOUT);
+poll_wake(sender, no_receiver_event);
 may_write.wake_all();
 }
 }
@@ -57,7 +57,7 @@ int pipe_buffer::read_events_unlocked()
 int pipe_buffer::write_events_unlocked()
 {
 if (!receiver) {
-return POLLERR|POLLOUT;
+return no_receiver_event;
 }
 int ret = 0;
 ret |= q.size() < max_buf ? POLLOUT : 0;
diff --git a/libc/pipe_buffer.hh b/libc/pipe_buffer.hh
--- a/libc/pipe_buffer.hh
+++ b/libc/pipe_buffer.hh
@@ -30,6 +30,9 @@ public:
 void detach_receiver();
 void attach_sender(struct file *f);
 void attach_receiver(struct file *f);
+void set_no_receiver_event(int event) {
+this->no_receiver_event = event;
+}
 private:
 int read_events_unlocked();
 int write_events_unlocked();
@@ -41,6 +44,7 @@ private:
 std::atomic refs = {};
 condvar may_read;
 condvar may_write;
+int no_receiver_event = POLLERR|POLLOUT;
 friend void intrusive_ptr_add_ref(pipe_buffer* p) {
 p->refs.fetch_add(1, std::memory_order_relaxed);
 }
diff --git a/tests/tst-epoll.cc b/tests/tst-epoll.cc
--- a/tests/tst-epoll.cc
+++ b/tests/tst-epoll.cc
@@ -184,6 +184,60 @@ static void test_socket_epollrdhup()
 client.join();
 }
 
+static void test_af_local_epollrdhup()
+{
+int s[2], events;
+struct epoll_event ee;
+
+int ep = epoll_create(1);
+report(ep >= 0, "epoll_create");
+
+if (socketpair(AF_UNIX, SOCK_STREAM, 0, s) == -1) {
+report(0, "socketpair() failed");
+return;
+}
+report(1, "socketpair() succeeded");
+
+ee.events = EPOLLET|EPOLLIN|EPOLLRDHUP;
+
+if (epoll_ctl(ep, EPOLL_CTL_ADD, s[0], ) == -1) {
+report(0, "epoll_ctl() failed");
+goto failed;
+}
+report(1, "epoll_crtl() succeeded");
+
+if (close(s[1]) == -1) {
+report(0, "close() failed");
+s[1] = -1;
+goto failed;
+}
+
+s[1] = -1;
+
+events = epoll_wait(ep, , 1, 5000);
+
+if (events == -1) {
+report(0, "epoll_wait() failed");
+goto failed;
+}
+
+if (events) {
+report(ee.events & EPOLLRDHUP, "EPOLLRDHUP active");
+} else {
+report(0, "epoll_wait() timed out\n");
+}
+
+failed:
+
+if (s[1] != -1 && close(s[1]) == -1) {
+report(0, "close() failed");
+}
+
+if (close(s[0]) == -1) {
+report(0, "close() failed");
+}
+}
+
 int main(int ac, char** av)
 {
 int ep = epoll_create(1);
@@ -338,6 +392,7 @@ int main(int ac, char** av)
 test_epolloneshot();
 test_epoll_file();
 test_socket_epollrdhup();
+test_af_local_epollrdhup();
 
 std::cout << "SUMMARY: " << tests << ", " << fails << " failures\n";
 return !!fails;

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/425a9d0602724b4f%40google.com.


[osv-dev] [COMMIT osv master] poll: drop POLLINIGNEOF and fix POLLRDHUP

2023-08-08 Thread Commit Bot
From: Waldemar Kozaczuk 
Committer: WALDEMAR KOZACZUK 
Branch: master

poll: drop POLLINIGNEOF and fix POLLRDHUP

Both POLLIGNEOF and POLLRDHUP use the same value. POLLRDHUP must
be congruent to EPOLLRDHUP, so it can't change. However, POLLIGNEOF is
unused so extirpate POLLINIGNEOF from the code base.

Also update sopoll_generic_locked so that POLLRDHUP can actually
be set when requested by clients.

See 
https://github.com/SpirentOrion/osv/commit/32bb00dc512512b170bdf773ab2ddb3b1e760737

tests/test-epoll: add socket EPOLLRDHUP test
Add test to verify that EPOLLRDHUP works for sockets.

See 
https://github.com/SpirentOrion/osv/commit/52bfb5130c1a7f39964a2d25c528755ef8ce9f9f

Signed-off-by: Waldemar Kozaczuk 

---
diff --git a/bsd/sys/kern/uipc_socket.cc b/bsd/sys/kern/uipc_socket.cc
--- a/bsd/sys/kern/uipc_socket.cc
+++ b/bsd/sys/kern/uipc_socket.cc
@@ -3182,12 +3182,10 @@ sopoll_generic_locked(struct socket *so, int events)
if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK))
revents |= events & (POLLPRI | POLLRDBAND);
 
-   if ((events & POLLINIGNEOF) == 0) {
-   if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
-   revents |= events & (POLLIN | POLLRDNORM);
-   if (so->so_snd.sb_state & SBS_CANTSENDMORE)
-   revents |= POLLHUP;
-   }
+   if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
+   revents |= events & (POLLIN | POLLRDNORM | POLLRDHUP);
+   if (so->so_snd.sb_state & SBS_CANTSENDMORE)
+   revents |= POLLHUP;
}
 
if (revents == 0 || events & EPOLLET) {
diff --git a/include/osv/poll.h b/include/osv/poll.h
--- a/include/osv/poll.h
+++ b/include/osv/poll.h
@@ -80,9 +80,6 @@ struct pollfd {
 #define POLLRDBAND  0x0080  /* OOB/Urgent readable data */
 #define POLLWRBAND  0x0100  /* OOB/Urgent data can be written */
 
-/* General FreeBSD extension (currently only supported for sockets): */
-#define POLLINIGNEOF0x2000  /* like POLLIN, except ignore EOF */
-
 /*
  * These events are set if they occur regardless of whether they were
  * requested.
@@ -105,8 +102,6 @@ struct pollfd {
 #include 
 #define POLLSTANDARD(POLLIN|POLLPRI|POLLOUT|POLLRDNORM|POLLRDBAND|\
  POLLWRBAND|POLLERR|POLLHUP|POLLNVAL)
-/* General FreeBSD extension (currently only supported for sockets): */
-#define POLLINIGNEOF0x2000  /* like POLLIN, except ignore EOF */
 
 #endif
 
diff --git a/tests/tst-epoll.cc b/tests/tst-epoll.cc
--- a/tests/tst-epoll.cc
+++ b/tests/tst-epoll.cc
@@ -116,6 +116,74 @@ static void test_epoll_file()
 close(fd);
 }
 
+#include 
+#include 
+#include 
+static void test_socket_epollrdhup()
+{
+constexpr int MAXEVENTS = 1024;
+struct epoll_event events[MAXEVENTS];
+
+int ep = epoll_create(1);
+report(ep >= 0, "epoll_create");
+
+// Create a TCP server socket
+int serverfd = socket(AF_INET, SOCK_STREAM, 0);
+report(serverfd >= 0, "server socket");
+struct sockaddr_in server = {
+.sin_family = AF_INET,
+.sin_port   = htons(65432),
+.sin_addr   = {
+.s_addr = htonl(INADDR_LOOPBACK),
+},
+};
+int r = bind(serverfd,
+ reinterpret_cast(),
+ sizeof(server));
+report(r == 0, "bind serverfd");
+
+r = listen(serverfd, 1);
+report(r == 0, "listen on serverfd");
+
+// Create a thread to connect and disconnect
+std::thread client([&] {
+int clientfd = socket(AF_INET, SOCK_STREAM, 0);
+report (clientfd >= 0, "client socket");
+struct sockaddr_in server = {
+.sin_family = AF_INET,
+.sin_port   = htons(65432),
+.sin_addr   = {
+.s_addr = htonl(INADDR_LOOPBACK),
+}
+};
+int r = connect(clientfd,
+reinterpret_cast(),
+sizeof(server));
+report(r == 0, "connect to server");
+close(clientfd);
+});
+
+// Accept the client
+int c = accept(serverfd, NULL, NULL);
+report(c >= 0, "accepted client");
+
+// and wait for socket to close
+struct epoll_event event = {
+.events  = EPOLLRDHUP,
+.data= {
+.u32 = 789,
+}
+};
+r = epoll_ctl(ep, EPOLL_CTL_ADD, c, );
+report(r == 0, "epoll_ctl ADD");
+
+r = epoll_wait(ep, events, MAXEVENTS, 10);
+report(r == 1 && (event.events & EPOLLRDHUP) &&
+   (event.data.u32 == 789), "epoll_wait for EPOLLRDHUP");
+close(serverfd);
+client.join();
+}
+
 int main(int ac, char** av)
 {
 int ep = epoll_create(1);
@@ -269,6 +337,7 @@ int main(int ac, char** av)
 
 test_epolloneshot();
 test_epoll_file();
+test_socket_epollrdhup();
 
 std::cout << "SUMMARY: " << tests << ", " << fails << " failures\n";
 return !!fails;

-- 
You received this message because 

[osv-dev] [COMMIT osv master] uipc_socket: replace spaces to tabs to keep file consistent

2023-08-08 Thread Commit Bot
From: Waldemar Kozaczuk 
Committer: WALDEMAR KOZACZUK 
Branch: master

uipc_socket: replace spaces to tabs to keep file consistent

Signed-off-by: Waldemar Kozaczuk 

---
diff --git a/bsd/sys/kern/uipc_socket.cc b/bsd/sys/kern/uipc_socket.cc
--- a/bsd/sys/kern/uipc_socket.cc
+++ b/bsd/sys/kern/uipc_socket.cc
@@ -3190,17 +3190,17 @@ sopoll_generic_locked(struct socket *so, int events)
}
}
 
-if (revents == 0 || events & EPOLLET) {
-if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
-so->so_rcv.sb_flags |= SB_SEL;
-}
+   if (revents == 0 || events & EPOLLET) {
+   if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
+   so->so_rcv.sb_flags |= SB_SEL;
+   }
 
-if (events & (POLLOUT | POLLWRNORM)) {
-so->so_snd.sb_flags |= SB_SEL;
-}
-}
+   if (events & (POLLOUT | POLLWRNORM)) {
+   so->so_snd.sb_flags |= SB_SEL;
+   }
+   }
 
-return revents;
+   return revents;
 }
 
 /*

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/1a6fcf0602724b1c%40google.com.


Re: [osv-dev] OSv and Genezio presentations

2023-08-08 Thread 'Dor Laor' via OSv Development
On Tue, Aug 8, 2023 at 10:32 PM Waldek Kozaczuk 
wrote:

> Hi,
>
> I am back after a family summer break vacation so you should hear from me
> more often hopefully.
>
> I would also like to share links to the OSv presentation I gave a month
> ago on the Unikernel Alliance forum:
> - recording:
> https://www.youtube.com/watch?v=KnzRnKTCmjc=PLnqfImRjARAjX5veufPxCkGYntfsl8Eb_=3
> - slides PDF:
> https://drive.google.com/file/d/1XPzjLPRnq58HzW84yn98Ut7GVbOp6_cq/view?pli=1
> - slides on Google docs:
> https://docs.google.com/presentation/d/1-dlart1tFslsdjZVajkKpbyREN0UKGoIo55HvZtAl9s/edit?usp=sharing
>
> I would also like to share the presentation by the Genezio team that has
> been building their serverless platform based on OSv:
>
> - recording:
> https://www.youtube.com/watch?v=xRmRXCY_h6I=PLnqfImRjARAjX5veufPxCkGYntfsl8Eb_=4
> - slides PDF:
> https://drive.google.com/file/d/1G_41wvszW8WAgOW1m108_cAP1SZ5lkOC/view?usp=drive_link
>

Nice to see a real life use case !


>
> Enjoy,
> Waldek Kozaczuk
>
> --
> You received this message because you are subscribed to the Google Groups
> "OSv Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to osv-dev+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/osv-dev/853de34d-67aa-470b-96c1-16f6e8fb7589n%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/CAKUaUn7LFYdRg6d%2Bj6%2BWbCXJBhi9Cnc0h8Na0HEkQJ80i2COOw%40mail.gmail.com.


[osv-dev] OSv and Genezio presentations

2023-08-08 Thread Waldek Kozaczuk
Hi,

I am back after a family summer break vacation so you should hear from me 
more often hopefully.

I would also like to share links to the OSv presentation I gave a month ago 
on the Unikernel Alliance forum:
- 
recording: 
https://www.youtube.com/watch?v=KnzRnKTCmjc=PLnqfImRjARAjX5veufPxCkGYntfsl8Eb_=3
- slides PDF: 
https://drive.google.com/file/d/1XPzjLPRnq58HzW84yn98Ut7GVbOp6_cq/view?pli=1
- slides on Google 
docs: 
https://docs.google.com/presentation/d/1-dlart1tFslsdjZVajkKpbyREN0UKGoIo55HvZtAl9s/edit?usp=sharing

I would also like to share the presentation by the Genezio team that has 
been building their serverless platform based on OSv:

- 
recording: 
https://www.youtube.com/watch?v=xRmRXCY_h6I=PLnqfImRjARAjX5veufPxCkGYntfsl8Eb_=4
- slides 
PDF: 
https://drive.google.com/file/d/1G_41wvszW8WAgOW1m108_cAP1SZ5lkOC/view?usp=drive_link

Enjoy,
Waldek Kozaczuk

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/853de34d-67aa-470b-96c1-16f6e8fb7589n%40googlegroups.com.