1# 2# Copyright (c) 2013 Nicolas PLANEL <nicolas.planel@enovance.com> 3# 4 5"""BRIDGE network link 6 7""" 8 9from __future__ import absolute_import 10 11from ... import core as netlink 12from .. import capi as capi 13 14class BRIDGELink(object): 15 def __init__(self, link): 16 self._link = link 17 self._has_ext_info = capi.rtnl_link_bridge_has_ext_info(self._link) 18 self._port_state_values = ['disabled','listening','learning','forwarding','blocking'] 19 20 def bridge_assert_ext_info(self): 21 if self._has_ext_info == False: 22 print """ 23 Please update your kernel to be able to call this method. 24 Your current kernel bridge version is too old to support this extention. 25 """ 26 raise RuntimeWarning() 27 28 def port_state2str(self, state): 29 return self._port_state_values[state] 30 31 def str2port_state(self, str): 32 for value, port in enumerate(self._port_state_values): 33 if str.lower() == port: 34 return value 35 raise ValueError() 36 37 @property 38 @netlink.nlattr(type=int) 39 def port_state(self): 40 """bridge state : 41 %s 42 """ % (self.port_state) 43 return capi.rtnl_link_bridge_get_state(self._link) 44 45 @port_state.setter 46 def port_state(self, state): 47 capi.rtnl_link_bridge_set_state(self._link, int(state)) 48 49 @property 50 @netlink.nlattr(type=int) 51 def priority(self): 52 """bridge prio 53 """ 54 bridge_assert_ext_info() 55 return capi.rtnl_link_bridge_get_prio(self._link) 56 57 @priority.setter 58 def priority(self, prio): 59 bridge_assert_ext_info() 60 if prio < 0 or prio >= 2**16: 61 raise ValueError() 62 capi.rtnl_link_bridge_set_prio(self._link, int(prio)) 63 64 @property 65 @netlink.nlattr(type=int) 66 def cost(self): 67 """bridge prio 68 """ 69 bridge_assert_ext_info() 70 return capi.rtnl_link_bridge_get_cost(self._link) 71 72 @cost.setter 73 def cost(self, cost): 74 bridge_assert_ext_info() 75 if cost < 0 or cost >= 2**32: 76 raise ValueError() 77 capi.rtnl_link_bridge_set_cost(self._link, int(cost)) 78 79 @property 80 @netlink.nlattr(type=str) 81 def flags(self): 82 """ BRIDGE flags 83 Setting this property will *Not* reset flags to value you supply in 84 Examples: 85 link.flags = '+xxx' # add xxx flag 86 link.flags = 'xxx' # exactly the same 87 link.flags = '-xxx' # remove xxx flag 88 link.flags = [ '+xxx', '-yyy' ] # list operation 89 """ 90 self.bridge_assert_ext_info() 91 flags = capi.rtnl_link_bridge_get_flags(self._link) 92 return capi.rtnl_link_bridge_flags2str(flags, 256)[0].split(',') 93 94 def _set_flag(self, flag): 95 if flag.startswith('-'): 96 i = capi.rtnl_link_bridge_str2flags(flag[1:]) 97 capi.rtnl_link_bridge_unset_flags(self._link, i) 98 elif flag.startswith('+'): 99 i = capi.rtnl_link_bridge_str2flags(flag[1:]) 100 capi.rtnl_link_bridge_set_flags(self._link, i) 101 else: 102 i = capi.rtnl_link_bridge_str2flags(flag) 103 capi.rtnl_link_bridge_set_flags(self._link, i) 104 105 @flags.setter 106 def flags(self, value): 107 self.bridge_assert_ext_info() 108 if type(value) is list: 109 for flag in value: 110 self._set_flag(flag) 111 else: 112 self._set_flag(value) 113 114 def brief(self): 115 return 'bridge-has-ext-info {0}'.format(self._has_ext_info) 116 117def init(link): 118 link.bridge = BRIDGELink(link._rtnl_link) 119 return link.bridge 120