-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bpf: Derive host netns cookie via SO_NETNS_COOKIE
When running in nested environments (e.g. Kind), cilium-agent does not run in the host netns. So, in such cases the cookie comparison based on bpf_get_netns_cookie(NULL) in bpf_sock.c for checking whether a socket belongs to a host netns does not work. This breaks some socket-lb functionality. To fix this, we derive the cookie of the netns in which cilium-agent runs via getsockopt(...SO_NETNS_COOKIE...) and then use it in the check above. This is based on an assumption that cilium-agent always runs with "hostNetwork: true". Signed-off-by: Martynas Pumputis <m@lambda.lt>
- Loading branch information
Showing
3 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package netns | ||
|
||
import ( | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
const SO_NETNS_COOKIE = 71 | ||
|
||
// GetNetNSCookie tries to retrieve the cookie of the host netns. | ||
func GetNetNSCookie() (uint64, error) { | ||
s, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
cookie, err := unix.GetsockoptUint64(s, unix.SOL_SOCKET, SO_NETNS_COOKIE) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return cookie, nil | ||
} |