On Fri, Sep 2, 2022 at 8:18 AM Ammar Faizi wrote: > From: Ammar Faizi <ammarfaizi2@xxxxxxxxxxx> > > This is a prep patch to fix an intermittent issue with the port number. > > We have many places where we need to bind() a socket to any unused port > number. To achieve that, the current approach does one of the following > mechanisms: > > 1) Randomly brute force the port number until the bind() syscall > succeeds. > > 2) Use a static port at compile time (randomly chosen too). > > This is not reliable and it results in an intermittent issue (test > fails when the selected port is in use). > > Setting @addr->sin_port to zero on a bind() syscall lets the kernel > choose a port number that is not in use. The caller then can know the > port number to be bound by invoking a getsockname() syscall after > bind() succeeds. > > Wrap this procedure in a new function called t_bind_ephemeral_port(). > The selected port will be returned into @addr->sin_port, the caller > can use it later to connect() or whatever they need. > > Link: https://lore.kernel.org/r/918facd1-78ba-2de7-693a-5f8c65ea2fcd@xxxxxxxxxxx > Cc: Dylan Yudaken <dylany@xxxxxx> > Cc: Facebook Kernel Team <kernel-team@xxxxxx> > Cc: Pavel Begunkov <asml.silence@xxxxxxxxx> > Signed-off-by: Ammar Faizi <ammarfaizi2@xxxxxxxxxxx> Reviewed-by: Alviro Iskandar Setiawan <alviro.iskandar@xxxxxxxxxxx> tq -- Viro