ns

package
v0.0.0-...-c16cc43 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Copyright © 2022 Merbridge Authors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package ns On Linux each OS thread can have a different network namespace. Go's thread scheduling model switches goroutines between OS threads based on OS thread load and whether the goroutine would block other goroutines. This can result in a goroutine switching network namespaces without notice and lead to errors in your code.

Index

Constants

View Source
const (

	// SoMark mark packets sent from a specific socket.
	SoMark = 0x24
)

Variables

This section is empty.

Functions

func Gettid

func Gettid() (tid int)

Gettid returns thread id

func IsNSorErr

func IsNSorErr(nspath string) error

IsNSorErr checks the ns path

func Setns

func Setns(fd int, nstype int) (err error)

Setns set fd's netns

func WithNetNSPath

func WithNetNSPath(nspath string, toRun func(NetNS) error) error

WithNetNSPath executes the passed closure under the given network namespace, restoring the original namespace afterwards.

Types

type NetNS

type NetNS interface {
	// Do execute the passed closure in this object's network namespace,
	// attempting to restore the original namespace before returning.
	// However, since each OS thread can have a different network namespace,
	// and Go's thread scheduling is highly variable, callers cannot
	// guarantee any specific namespace is set unless operations that
	// require that namespace are wrapped with Do().  Also, no code called
	// from Do() should call runtime.UnlockOSThread(), or the risk
	// of executing code in an incorrect namespace will be greater.  See
	// https://github.com/golang/go/wiki/LockOSThread for further details.
	Do(toRun func(NetNS) error) error

	// Set the current network namespace to this object's network namespace.
	// Note that since Go's thread scheduling is highly variable, callers
	// cannot guarantee the requested namespace will be the current namespace
	// after this function is called; to ensure this wrap operations that
	// require the namespace with Do() instead.
	Set() error

	// Path returns the filesystem path representing this object's network namespace
	Path() string

	// Fd returns a file descriptor representing this object's network namespace
	Fd() uintptr

	// Close cleans up this instance of the network namespace; if this instance
	// is the last user the namespace will be destroyed
	Close() error
}

NetNS define netns ops

func GetCurrentNS

func GetCurrentNS() (NetNS, error)

GetCurrentNS returns an object representing the current OS thread's network namespace

func GetNS

func GetNS(nspath string) (NetNS, error)

GetNS returns an object representing the namespace referred to by @path

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL