Page MenuHomeFreeBSD

sysctl: Update 'master' copy of vnet SYSCTLs on kernel environment variables change
ClosedPublic

Authored by zlei on Sep 12 2023, 3:21 PM.
Tags
None
Referenced Files
Unknown Object (File)
Fri, Nov 8, 6:43 AM
Unknown Object (File)
Tue, Oct 22, 11:47 AM
Unknown Object (File)
Wed, Oct 16, 8:04 AM
Unknown Object (File)
Oct 2 2024, 1:33 AM
Unknown Object (File)
Oct 1 2024, 10:03 PM
Unknown Object (File)
Sep 30 2024, 10:16 PM
Unknown Object (File)
Sep 30 2024, 7:22 AM
Unknown Object (File)
Sep 29 2024, 6:05 PM
Subscribers

Details

Summary

Complete phase three of 3da1cf1e88f8.

With commit 110113bc086f, vnet sysctl variables can be loader tunable
but the feature is limited. When the kernel modules have been initialized,
any changes (e.g. via kenv) to kernel environment variable will not affect
subsequently created VNETs.

This change relexes the limitation by listening on kernel environment
variable's set / unset events, and then update the 'master' copy of vnet
SYSCTL or restore it to its initial value.

With this change, TUNABLE_XXX_FETCH can be greately eliminated for vnet
loader tunables.

Fixes: 110113bc086f sysctl(9): Enable vnet sysctl variables to be loader tunable
MFC after: 2 weeks

Test Plan

Change the kernel environment and create new jails. Verify the SYSCTLs have new values.

A snip to help repeat the test.

#!/bin/sh

kldload -nq if_lagg

# check defaults of vnet0's sysctls
[ "$( sysctl -n net.add_addr_allfibs )" -eq "0" ] || exit 1  
[ "$( sysctl -n net.inet.tcp.syncache.hashsize )" -eq "512" ] || exit 1  
[ "$( sysctl -n net.link.lagg.default_use_flowid )" -eq "0" ] || exit 1  

kenv net.add_addr_allfibs=1
kenv net.inet.tcp.syncache.hashsize=1024
kenv net.link.lagg.default_use_flowid=1

# kenv should not change vnet0's sysctls
[ "$( sysctl -n net.add_addr_allfibs )" -eq "0" ] || exit 1  
[ "$( sysctl -n net.inet.tcp.syncache.hashsize )" -eq "512" ] || exit 1  
[ "$( sysctl -n net.link.lagg.default_use_flowid )" -eq "0" ] || exit 1  

jid=$( jail -ic vnet persist )

# test set env by kenv
[ "$( jexec $jid sysctl -n net.add_addr_allfibs )" -eq "1" ] || exit 1  
[ "$( jexec $jid sysctl -n net.inet.tcp.syncache.hashsize )" -eq "1024" ] || exit 1  
[ "$( jexec $jid sysctl -n net.link.lagg.default_use_flowid )" -eq "1" ] || exit 1  

kenv -u net.add_addr_allfibs
kenv -u net.inet.tcp.syncache.hashsize
kenv -u net.link.lagg.default_use_flowid

# test unset env by kenv, should not affect existing jails
[ "$( sysctl -n net.add_addr_allfibs )" -eq "0" ] || exit 1  
[ "$( sysctl -n net.inet.tcp.syncache.hashsize )" -eq "512" ] || exit 1  
[ "$( sysctl -n net.link.lagg.default_use_flowid )" -eq "0" ] || exit 1  

[ "$( jexec $jid sysctl -n net.add_addr_allfibs )" -eq "1" ] || exit 1  
[ "$( jexec $jid sysctl -n net.inet.tcp.syncache.hashsize )" -eq "1024" ] || exit 1  
[ "$( jexec $jid sysctl -n net.link.lagg.default_use_flowid )" -eq "1" ] || exit 1  

# on unset env, new jail should have default value
j2=$( jail -ic vnet persist )
[ "$( jexec $j2 sysctl -n net.add_addr_allfibs )" -eq "0" ] || exit 1  
[ "$( jexec $j2 sysctl -n net.inet.tcp.syncache.hashsize )" -eq "512" ] || exit 1  
[ "$( jexec $j2 sysctl -n net.link.lagg.default_use_flowid )" -eq "0" ] || exit 1  

jail -R $jid
kldunload if_lagg
jail -R $j2

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

zlei requested review of this revision.Sep 12 2023, 3:21 PM

This can supersede D40127 .
Comparing to D40127, it is much simpler.

Tested with AMD64 and RISCV (QEMU) .

sys/kern/kern_sysctl.c
513–526

Collapse if clauses, if you don't mind. Resulting code:

	if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
	    (oidp->oid_kind & (CTLFLAG_TUN) != 0 &&
#ifdef VIMAGE
	    /*
	     * Can fetch value multiple times for VNET loader tunables.
	     * Only fetch once for non-VNET loader tunables.
	     */
	    ((oidp->oid_kind & CTLFLAG_VNET) == 0) &&
#endif
	    (oidp->oid_kind & CTLFLAG_NOFETCH) == 0)
		/* try to fetch value from kernel environment */
		sysctl_load_tunable_by_oid_locked(oidp);
sys/net/vnet.c
364

There is no need to cast malloc result to anything.

zlei marked an inline comment as done.Sep 20 2023, 9:40 AM
zlei added inline comments.
sys/kern/kern_sysctl.c
513–526

No, the suggested collapsing will result wrong logic.

If VIMAGE is enabled, SYSCTLs flagged with CTLFLAG_VNET will not be able to be updated from kernel environment.

glebius added inline comments.
sys/kern/kern_sysctl.c
513–526

Yep, my bad.

This revision is now accepted and ready to land.Sep 20 2023, 3:57 PM
zlei marked 2 inline comments as done.Sep 21 2023, 1:58 AM
zlei added inline comments.
sys/net/vnet.c
364

It should be casted. Otherwise compiler complains:

error: incompatible pointer to integer conversion assigning to 'uintptr_t' (aka 'unsigned long') from 'void *' [-Wint-conversion]