go.ligato.io/vpp-agent/v3@v3.5.0/plugins/linux/nsplugin/linuxcalls/system_api.go (about) 1 // Copyright (c) 2018 Cisco and/or its affiliates. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at: 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package linuxcalls 16 17 import ( 18 "os" 19 20 "github.com/vishvananda/netns" 21 ) 22 23 // SystemAPI defines all methods required for managing network namespaces 24 // on the system level. 25 type SystemAPI interface { 26 FileSystemAPI 27 NetworkNamespaceAPI 28 } 29 30 // FileSystemAPI defines all methods used to access file system. 31 type FileSystemAPI interface { 32 // FileExists checks whether the file exists. 33 FileExists(name string) (bool, error) 34 // OpenFile opens a file. 35 OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) 36 // MkDirAll creates a directory with all parent directories. 37 MkDirAll(path string, perm os.FileMode) error 38 // Remove removes named file or directory. 39 Remove(name string) error 40 // Mount makes resources available. 41 Mount(source string, target string, fsType string, flags uintptr, data string) error 42 // Unmount resources. 43 Unmount(target string, flags int) (err error) 44 } 45 46 // NetworkNamespaceAPI defines methods for low-level handling of network namespaces. 47 type NetworkNamespaceAPI interface { 48 // NewNetworkNamespace creates a new namespace and returns a handle to manage it further. 49 NewNetworkNamespace() (ns netns.NsHandle, err error) 50 // DuplicateNamespaceHandle duplicates network namespace handle. 51 DuplicateNamespaceHandle(ns netns.NsHandle) (netns.NsHandle, error) 52 // GetCurrentNamespace gets a handle to the current threads network namespace. 53 GetCurrentNamespace() (ns netns.NsHandle, err error) 54 // GetNamespaceFromPath gets a handle to a network namespace identified 55 // by the path. 56 GetNamespaceFromPath(path string) (ns netns.NsHandle, err error) 57 // GetNamespaceFromPid gets a handle to the network namespace of a given pid. 58 GetNamespaceFromPid(pid int) (ns netns.NsHandle, err error) 59 // GetNamespaceFromName gets a handle to a named network namespace such as one 60 // created by `ip netns add`. 61 GetNamespaceFromName(name string) (ns netns.NsHandle, err error) 62 // SetNamespace sets the current namespace to the namespace represented by the handle. 63 SetNamespace(ns netns.NsHandle) (err error) 64 } 65 66 // systemHandler implements SystemAPI using actual syscalls (i.e. not suitable for tests). 67 type systemHandler struct { 68 } 69 70 // NewSystemHandler returns new handler. 71 func NewSystemHandler() SystemAPI { 72 return &systemHandler{} 73 }