1 # Copyright (c) 2020 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 copy import deepcopy
18 from resources.libraries.python.ssh import exec_cmd_no_error, exec_cmd
22 """Linux namespace utilities."""
26 def create_namespace(node, namespace, delete_before_create=True):
27 """Create namespace and add the name to the list for later clean-up.
29 :param node: Where to create namespace.
30 :param namespace: Name for namespace.
31 :param delete_before_create: Delete namespace prior to create
34 :type delete_before_create: bool
36 if delete_before_create:
37 Namespaces.delete_namespace(node, namespace)
39 cmd = f"ip netns add {namespace}"
40 exec_cmd_no_error(node, cmd, sudo=True)
41 Namespaces.__namespaces.append(namespace)
44 def delete_namespace(node, namespace):
45 """Delete namespace from the node and list.
47 :param node: Where to delete namespace.
48 :param namespace: Name for namespace.
49 :param delete_before_create: Delete namespace prior to create
52 :type delete_before_create: bool
55 cmd = f"ip netns delete {namespace}"
56 (ret_code, _, delete_errmsg) = \
57 exec_cmd(node, cmd, timeout=cmd_timeout, sudo=True)
59 cmd = f"ip netns list {namespace}"
61 exec_cmd_no_error(node, cmd, timeout=cmd_timeout, sudo=True)
62 if stdout == namespace:
63 raise RuntimeError(f"Could not delete namespace "
64 f"({namespace}): {delete_errmsg}")
66 Namespaces.__namespaces.remove(namespace)
71 def attach_interface_to_namespace(node, namespace, interface):
72 """Attach specific interface to namespace.
74 :param node: Node where to execute command.
75 :param namespace: Namespace to execute command on.
76 :param interface: Interface in namespace.
80 :raises RuntimeError: Interface could not be attached.
82 cmd = f"ip link set {interface} netns {namespace}"
84 ret_code, _, stderr = exec_cmd(node, cmd, timeout=5, sudo=True)
86 raise RuntimeError(f"Could not attach interface, reason:\n{stderr}")
88 cmd = f"ip netns exec {namespace} ip link set {interface} up"
90 ret_code, _, stderr = exec_cmd(node, cmd, timeout=5, sudo=True)
93 f"Could not set interface state, reason:\n{stderr}"
97 def add_default_route_to_namespace(node, namespace, default_route):
98 """Add IPv4 default route to interface in namespace.
100 :param node: Node where to execute command.
101 :param namespace: Namespace to execute command on.
102 :param default_route: Default route address.
105 :type default_route: str
107 cmd = f"ip netns exec {namespace} ip route add default " \
108 f"via {default_route}"
109 exec_cmd_no_error(node, cmd, sudo=True)
112 def create_bridge_for_int_in_namespace(
113 node, namespace, bridge_name, *interfaces):
114 """Setup bridge domain and add interfaces to it.
116 :param node: Node where to execute command.
117 :param namespace: Namespace to execute command on.
118 :param bridge_name: Name of the bridge to be created.
119 :param interfaces: List of interfaces to add to the namespace.
122 :type bridge_name: str
123 :type interfaces: list
125 cmd = f"ip netns exec {namespace} brctl addbr {bridge_name}"
126 exec_cmd_no_error(node, cmd, sudo=True)
128 for interface in interfaces:
129 cmd = f"ip netns exec {namespace} brctl addif {bridge_name} " \
131 exec_cmd_no_error(node, cmd, sudo=True)
133 cmd = f"ip netns exec {namespace} ip link set dev {bridge_name} up"
134 exec_cmd_no_error(node, cmd, sudo=True)
137 def clean_up_namespaces(node, namespace=None):
138 """Delete all old namespaces.
140 :param node: Node where to execute command.
141 :param namespace: Namespace to delete, if None delete all namespaces
144 :raises RuntimeError: Namespaces could not be cleaned properly.
146 if namespace is not None:
147 Namespaces.delete_namespace(node, namespace)
150 namespace_copy = deepcopy(Namespaces.__namespaces)
151 for namespace_name in namespace_copy:
152 Namespaces.delete_namespace(node, namespace_name)