diff options
author | gVisor bot <gvisor-bot@google.com> | 2020-02-20 23:23:04 +0000 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2020-02-20 23:23:04 +0000 |
commit | f7f6b5ae0fad1f5bfd050b3b290e63efc83d7a28 (patch) | |
tree | dc72bfda207b99be7ee6ab527728d67e524f1552 /pkg/sentry/inet | |
parent | b1b3ef0213be56f5c3636b6399700ac313203f53 (diff) | |
parent | 4a73bae269ae9f52a962ae3b08a17ccaacf7ba80 (diff) |
Merge release-20200211.0-57-g4a73bae (automated)
Diffstat (limited to 'pkg/sentry/inet')
-rwxr-xr-x | pkg/sentry/inet/inet_state_autogen.go | 14 | ||||
-rwxr-xr-x | pkg/sentry/inet/namespace.go | 99 |
2 files changed, 113 insertions, 0 deletions
diff --git a/pkg/sentry/inet/inet_state_autogen.go b/pkg/sentry/inet/inet_state_autogen.go index dc2f459e5..97047d993 100755 --- a/pkg/sentry/inet/inet_state_autogen.go +++ b/pkg/sentry/inet/inet_state_autogen.go @@ -21,6 +21,20 @@ func (x *TCPBufferSize) load(m state.Map) { m.Load("Max", &x.Max) } +func (x *Namespace) beforeSave() {} +func (x *Namespace) save(m state.Map) { + x.beforeSave() + m.Save("creator", &x.creator) + m.Save("isRoot", &x.isRoot) +} + +func (x *Namespace) load(m state.Map) { + m.Load("creator", &x.creator) + m.Load("isRoot", &x.isRoot) + m.AfterLoad(x.afterLoad) +} + func init() { state.Register("pkg/sentry/inet.TCPBufferSize", (*TCPBufferSize)(nil), state.Fns{Save: (*TCPBufferSize).save, Load: (*TCPBufferSize).load}) + state.Register("pkg/sentry/inet.Namespace", (*Namespace)(nil), state.Fns{Save: (*Namespace).save, Load: (*Namespace).load}) } diff --git a/pkg/sentry/inet/namespace.go b/pkg/sentry/inet/namespace.go new file mode 100755 index 000000000..c16667e7f --- /dev/null +++ b/pkg/sentry/inet/namespace.go @@ -0,0 +1,99 @@ +// Copyright 2020 The gVisor 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. + +package inet + +// Namespace represents a network namespace. See network_namespaces(7). +// +// +stateify savable +type Namespace struct { + // stack is the network stack implementation of this network namespace. + stack Stack `state:"nosave"` + + // creator allows kernel to create new network stack for network namespaces. + // If nil, no networking will function if network is namespaced. + creator NetworkStackCreator + + // isRoot indicates whether this is the root network namespace. + isRoot bool +} + +// NewRootNamespace creates the root network namespace, with creator +// allowing new network namespaces to be created. If creator is nil, no +// networking will function if the network is namespaced. +func NewRootNamespace(stack Stack, creator NetworkStackCreator) *Namespace { + return &Namespace{ + stack: stack, + creator: creator, + isRoot: true, + } +} + +// NewNamespace creates a new network namespace from the root. +func NewNamespace(root *Namespace) *Namespace { + n := &Namespace{ + creator: root.creator, + } + n.init() + return n +} + +// Stack returns the network stack of n. Stack may return nil if no network +// stack is configured. +func (n *Namespace) Stack() Stack { + return n.stack +} + +// IsRoot returns whether n is the root network namespace. +func (n *Namespace) IsRoot() bool { + return n.isRoot +} + +// RestoreRootStack restores the root network namespace with stack. This should +// only be called when restoring kernel. +func (n *Namespace) RestoreRootStack(stack Stack) { + if !n.isRoot { + panic("RestoreRootStack can only be called on root network namespace") + } + if n.stack != nil { + panic("RestoreRootStack called after a stack has already been set") + } + n.stack = stack +} + +func (n *Namespace) init() { + // Root network namespace will have stack assigned later. + if n.isRoot { + return + } + if n.creator != nil { + var err error + n.stack, err = n.creator.CreateStack() + if err != nil { + panic(err) + } + } +} + +// afterLoad is invoked by stateify. +func (n *Namespace) afterLoad() { + n.init() +} + +// NetworkStackCreator allows new instances of a network stack to be created. It +// is used by the kernel to create new network namespaces when requested. +type NetworkStackCreator interface { + // CreateStack creates a new network stack for a network namespace. + CreateStack() (Stack, error) +} |