1 # Copyright (c) 2019 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Linux namespace utilities library."""
16 from resources.libraries.python.ssh import exec_cmd_no_error, exec_cmd
20 """Linux namespace utilities."""
24 def create_namespace(self, node, namespace_name):
25 """Create namespace and add the name to the list for later clean-up.
27 :param node: Where to create namespace.
28 :param namespace_name: Name for namespace.
30 :type namespace_name: str
32 cmd = f"ip netns add {namespace_name}"
34 exec_cmd_no_error(node, cmd, sudo=True)
35 self._namespaces.append(namespace_name)
38 def attach_interface_to_namespace(node, namespace, interface):
39 """Attach specific interface to namespace.
41 :param node: Node where to execute command.
42 :param namespace: Namespace to execute command on.
43 :param interface: Interface in namespace.
47 :raises RuntimeError: Interface could not be attached.
49 cmd = f"ip link set {interface} netns {namespace}"
51 ret_code, _, stderr = exec_cmd(node, cmd, timeout=5, sudo=True)
53 raise RuntimeError(f"Could not attach interface, reason:\n{stderr}")
55 cmd = f"ip netns exec {namespace} ip link set {interface} up"
57 ret_code, _, stderr = exec_cmd(node, cmd, timeout=5, sudo=True)
60 f"Could not set interface state, reason:\n{stderr}"
64 def create_bridge_for_int_in_namespace(
65 node, namespace, bridge_name, *interfaces):
66 """Setup bridge domain and add interfaces to it.
68 :param node: Node where to execute command.
69 :param namespace: Namespace to execute command on.
70 :param bridge_name: Name of the bridge to be created.
71 :param interfaces: List of interfaces to add to the namespace.
74 :type bridge_name: str
75 :type interfaces: list
77 cmd = f"ip netns exec {namespace} brctl addbr {bridge_name}"
78 exec_cmd_no_error(node, cmd, sudo=True)
80 for interface in interfaces:
81 cmd = f"ip netns exec {namespace} brctl addif {bridge_name} " \
83 exec_cmd_no_error(node, cmd, sudo=True)
85 cmd = f"ip netns exec {namespace} ip link set dev {bridge_name} up"
86 exec_cmd_no_error(node, cmd, sudo=True)
88 def clean_up_namespaces(self, node):
89 """Remove all old namespaces.
91 :param node: Node where to execute command.
93 :raises RuntimeError: Namespaces could not be cleaned properly.
95 for namespace in self._namespaces:
96 print(f"Cleaning namespace {namespace}")
97 cmd = f"ip netns delete {namespace}"
98 ret_code, _, _ = exec_cmd(node, cmd, timeout=5, sudo=True)
100 raise RuntimeError(u"Could not delete namespace")