Skip to content

Give each network namespace its own socktab - #156

Open
Stuartwastaken wants to merge 3 commits into
PlatformLab:mainfrom
Stuartwastaken:netns-socktab
Open

Stuartwastaken wants to merge 3 commits into
PlatformLab:mainfrom
Stuartwastaken:netns-socktab

Conversation

@Stuartwastaken

@Stuartwastaken Stuartwastaken commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

This is the first step of the namespace work. Today the sockets of all network namespaces share homa->socktab, and each lookup compares hsk->hnet. This moves the socktab into struct homa_net, so each namespace has its own table and its own port space.

homa_timer still has to visit every socket. It walks the kernel's net_namespace_list with for_each_net_rcu and scans each namespace's socktab. The scan calls schedule(), so it can't stay inside the RCU read lock: it takes a reference on the namespace with maybe_get_net, drops the RCU lock for the scan, and takes it again before put_net. The scans in homa_devel.c don't block, so they use for_each_net_rcu alone. Homa keeps no list of namespaces of its own, and there is no new lock.

This also fixes homa_abort_rpcs, which scanned the shared table without checking the namespace, so an ICMP unreachable received in one namespace aborted RPCs to the same address in all of them. It now takes the hnet from the error handler and scans only that table; homa_abort_rpcs__ignore_other_hnets fails without the change. Ashish's #143 contains the same fix for homa_abort_rpcs (an hnet argument plus a check in the loop), so you can take either one; whichever goes in second needs a small rebase.

Each namespace now allocates its own socktab (1024 buckets, a little over 8 KB) while Homa is loaded. homa_destroy no longer deletes sockets, so each unit-test fixture's teardown calls mock_hnets_destroy first.

Besides the unit tests, I ran this on 6.17.8 with the same port bound in four namespaces at once, RPCs between two namespaces over a veth pair, and 300 namespace create/delete cycles under load. That kernel has neither KASAN nor lockdep, so those have not been run.

Before this change all namespaces shared homa->socktab, with the
namespace as part of the hash key. Now each homa_net has its own
socktab, and homa->nets links the homa_nets together so that
homa_timer can scan the sockets of all namespaces.

This fixes a bug in homa_abort_rpcs: an ICMP error received in one
namespace aborted RPCs to the same address in every namespace.
@Stuartwastaken
Stuartwastaken marked this pull request as ready for review November 23, 2026 17:23

@johnousterhout johnousterhout left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a good start. I made a quick scan and added a few comments. Let's discuss the ones labeled "Let's discuss".

Comment thread homa_impl.h Outdated
* holding either @nets_mutex or the RCU read lock.
*/
struct homa_socktab *socktab;
struct list_head nets;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you make this an RCU list so that nets_mutex doesn't need to be held while traversing it? Or, if this is already an RCU list, document that explicitly. If it can be scanned with only an RCU read lock, why would anyone want to use nets_mutex? Let's discuss.

Comment thread homa_impl.h Outdated
struct list_head nets;

/** @nets_mutex: Synchronizes access to @nets. */
struct mutex nets_mutex;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The convention in Homa is to use spinlocks for everything, unless there is a particular reason why a sleep lock is needed.

Comment thread homa_impl.h Outdated
* homa->nets_mutex, all locks should be considered "leaf" locks: don't
* acquire other locks while holding them.
* 2. The lock order is:
* * homa->nets_mutex

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Adding a new layer in the locking hierarchy is a major change; it's not obvious to me that this is necessary. Let's discuss.

Comment thread homa_plumbing.c Outdated
void homa_net_pre_exit(struct net *net)
{
homa_net_unlink(homa_net(net));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a pass-through function; why not just put homa_net_unlink into the struct pernet_operations? Let's discuss.

Comment thread homa_sock.c Outdated

return homa_socktab_next(scan);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this function needed? Why not just let callers do the 2-level scan themselves (iterate over the list of hnets, then invoke homa_socktab_start_scan on each)? Let's discuss.

Comment thread homa_sock.h Outdated
* struct homa_socktab - A hash table that maps from port numbers (either
* client or server) to homa_sock objects.
* client or server) to homa_sock objects. There is one of these for each
* network namespace.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure this additional comment is needed. Is there anything in this module that needs to care about the existence of network namespaces? Ideally all of the network namespace can be built on top of this module, without this module needing to know how it is being used.

Comment thread homa_timer.c Outdated
* RCU to keep network namespaces from being deleted during the scan,
* because the scan may block.
*/
mutex_lock(&homa->nets_mutex);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Where/how does the scan block? Given that RCU is already used below when scanning an individual socket, why not just use it for namespace scan also? Let's discuss.

Comment thread homa_utils.c Outdated
list_for_each_entry_safe(hnet, next, &homa->nets, links) {
homa_net_unlink(hnet);
homa_socktab_destroy(hnet->socktab);
kfree(hnet->socktab);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Having a bunch of code here that only matters for unit tests is awkward (and can't be upstreamed). Can you find a way to get that code out of here and put it someplace in the unit testing framework instead?

@breakertt

Copy link
Copy Markdown
Contributor

Thanks for starting this. I have been testing Homa between namespaces
(veth pairs, and pods routed through the host) and hit a few problems
as well. Have you seen any of them?

  1. Over a veth pair, messages longer than one packet never complete.
    veth delivers the GSO packet unsegmented, and the receive side
    assumes one segment per skb. Did your veth tests use multi-packet
    messages?
    Same for virtio without gso force software
  2. When the host forwards Homa packets to a namespace (ip_forward=1),
    homa_gro_receive merges them as local packets; the merged skb can
    then hit the BUG in skb_segment on the way out.
  3. The net.homa.* sysctls are registered only in init_net, so a
    namespace cannot see or set them.

I have local fixes for 1 and 2, but they are not cleaned up for
upstream yet. I am happy to do that and send PRs.

@Stuartwastaken

Copy link
Copy Markdown
Contributor Author

This is a good start. I made a quick scan and added a few comments. Let's discuss the ones labeled "Let's discuss".

Happy to discuss, @johnousterhout. Our call this week was cancelled by Thiya but we can pick this up next week. Happy Thanksgiving! Perusing through here I can understand my own naive mistake of doing a mutex lock on the whole namespace table, and not taking care of layering correctly by involving the socket code. And actually while doing this I learned that linux keeps track of the namespaces for us, so that makes this easier. Pushing some fixes now to use the linux namespace, keeping the socktab code separate and namespace-unaware, and making sure the unit test code stays out of the main code.

To answer your question about where the scan blocks: it's only the schedule() every 10 RPCs. My plan is for the timer to take a reference on each namespace (maybe_get_net), drop the RCU lock while it scans that namespace's sockets, then retake RCU and release the reference before moving on.

Replaces homa->nets and homa->nets_mutex. homa_timer now walks the
kernel's list of namespaces under RCU and holds a reference on each
namespace while it scans that namespace's sockets. The socktab code
no longer knows about namespaces, and the unit-test cleanup of
homa_nets has moved from homa_destroy to mock.c.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants