| 1 |
# Copyright 2016 Orange |
| 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 |
from oslo_log import log as logging |
| 16 |
|
| 17 |
LOG = logging.getLogger(__name__) |
| 18 |
|
| 19 |
|
| 20 |
def get_device_mac(run_command_fn, dev_name, netns_name=None): |
| 21 |
"""Find device MAC address""" |
| 22 |
if netns_name: |
| 23 |
command_prefix = "ip netns exec %s " % netns_name |
| 24 |
else: |
| 25 |
command_prefix = "" |
| 26 |
|
| 27 |
(output, _) = run_command_fn("%scat /sys/class/net/%s/address" % |
| 28 |
(command_prefix, dev_name)) |
| 29 |
return output[0] |
| 30 |
|
| 31 |
|
| 32 |
def set_device_mac(run_command_fn, dev_name, mac_address, netns_name=None): |
| 33 |
"""Set device MAC address""" |
| 34 |
if netns_name: |
| 35 |
command_prefix = "ip netns exec %s " % netns_name |
| 36 |
else: |
| 37 |
command_prefix = "" |
| 38 |
|
| 39 |
run_command_fn("{} ip link set {} address {}".format( |
| 40 |
command_prefix, |
| 41 |
dev_name, mac_address), run_as_root=True) |