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 resources.libraries.python.ssh import exec_cmd_no_error, exec_cmd
20 """Linux namespace utilities."""
24 def create_namespace(node, namespace, delete_before_create=True):
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 for namespace.
29 :param delete_before_create: Delete namespace prior to create
32 :type delete_before_create: bool
34 if delete_before_create:
35 Namespaces.delete_namespace(node, namespace)
37 cmd = f"ip netns add {namespace}"
38 exec_cmd_no_error(node, cmd, sudo=True)
39 Namespaces.__namespaces.append(namespace)
42 def delete_namespace(node, namespace):
43 """Delete namespace from the node and list.
45 :param node: Where to delete namespace.
46 :param namespace: Name for namespace.
47 :param delete_before_create: Delete namespace prior to create
50 :type delete_before_create: bool
53 cmd = f"ip netns delete {namespace}"
54 (ret_code, _, delete_errmsg) = \
55 exec_cmd(node, cmd, timeout=cmd_timeout, sudo=True)
57 cmd = f"ip netns list {namespace}"
59 exec_cmd_no_error(node, cmd, timeout=cmd_timeout, sudo=True)
60 if stdout == namespace:
61 raise RuntimeError(f"Could not delete namespace "
62 f"({namespace}): {delete_errmsg}")
64 Namespaces.__namespaces.remove(namespace)
69 def attach_interface_to_namespace(node, namespace, interface):
70 """Attach specific interface to namespace.
72 :param node: Node where to execute command.
73 :param namespace: Namespace to execute command on.
74 :param interface: Interface in namespace.
78 :raises RuntimeError: Interface could not be attached.
80 cmd = f"ip link set {interface} netns {namespace}"
82 ret_code, _, stderr = exec_cmd(node, cmd, timeout=5, sudo=True)
84 raise RuntimeError(f"Could not attach interface, reason:\n{stderr}")
86 cmd = f"ip netns exec {namespace} ip link set {interface} up"
88 ret_code, _, stderr = exec_cmd(node, cmd, timeout=5, sudo=True)
91 f"Could not set interface state, reason:\n{stderr}"
95 def add_default_route_to_namespace(node, namespace, default_route):
96 """Add IPv4 default route to interface in namespace.
98 :param node: Node where to execute command.
99 :param namespace: Namespace to execute command on.
100 :param default_route: Default route address.
103 :type default_route: str
105 cmd = f"ip netns exec {namespace} ip route add default " \
106 f"via {default_route}"
107 exec_cmd_no_error(node, cmd, sudo=True)
110 def create_bridge_for_int_in_namespace(
111 node, namespace, bridge_name, *interfaces):
112 """Setup bridge domain and add interfaces to it.
114 :param node: Node where to execute command.
115 :param namespace: Namespace to execute command on.
116 :param bridge_name: Name of the bridge to be created.
117 :param interfaces: List of interfaces to add to the namespace.
120 :type bridge_name: str
121 :type interfaces: list
123 cmd = f"ip netns exec {namespace} brctl addbr {bridge_name}"
124 exec_cmd_no_error(node, cmd, sudo=True)
126 for interface in interfaces:
127 cmd = f"ip netns exec {namespace} brctl addif {bridge_name} " \
129 exec_cmd_no_error(node, cmd, sudo=True)
131 cmd = f"ip netns exec {namespace} ip link set dev {bridge_name} up"
132 exec_cmd_no_error(node, cmd, sudo=True)
135 def clean_up_namespaces(node, namespace=None):
136 """Delete all old namespaces.
138 :param node: Node where to execute command.
139 :param namespace: Namespace to delete, if None delete all namespaces
142 :raises RuntimeError: Namespaces could not be cleaned properly.
144 if namespace is not None:
145 Namespaces.delete_namespace(node, namespace)
148 for namespace_name in Namespaces.__namespaces:
149 Namespaces.delete_namespace(node, namespace_name)