1# 2# Copyright (c) 2011 Thomas Graf <tgraf@suug.ch> 3# 4 5"""VLAN network link 6 7""" 8 9from __future__ import absolute_import 10 11 12from ... import core as netlink 13from .. import capi as capi 14class VLANLink(object): 15 def __init__(self, link): 16 self._link = link 17 18 @property 19 @netlink.nlattr(type=int) 20 def id(self): 21 """vlan identifier""" 22 return capi.rtnl_link_vlan_get_id(self._link) 23 24 @id.setter 25 def id(self, value): 26 capi.rtnl_link_vlan_set_id(self._link, int(value)) 27 28 @property 29 @netlink.nlattr(type=str) 30 def flags(self): 31 """ VLAN flags 32 Setting this property will *Not* reset flags to value you supply in 33 Examples: 34 link.flags = '+xxx' # add xxx flag 35 link.flags = 'xxx' # exactly the same 36 link.flags = '-xxx' # remove xxx flag 37 link.flags = [ '+xxx', '-yyy' ] # list operation 38 """ 39 flags = capi.rtnl_link_vlan_get_flags(self._link) 40 return capi.rtnl_link_vlan_flags2str(flags, 256)[0].split(',') 41 42 def _set_flag(self, flag): 43 if flag.startswith('-'): 44 i = capi.rtnl_link_vlan_str2flags(flag[1:]) 45 capi.rtnl_link_vlan_unset_flags(self._link, i) 46 elif flag.startswith('+'): 47 i = capi.rtnl_link_vlan_str2flags(flag[1:]) 48 capi.rtnl_link_vlan_set_flags(self._link, i) 49 else: 50 i = capi.rtnl_link_vlan_str2flags(flag) 51 capi.rtnl_link_vlan_set_flags(self._link, i) 52 53 @flags.setter 54 def flags(self, value): 55 if type(value) is list: 56 for flag in value: 57 self._set_flag(flag) 58 else: 59 self._set_flag(value) 60 61 ################################################################### 62 # TODO: 63 # - ingress map 64 # - egress map 65 66 def brief(self): 67 return 'vlan-id {0}'.format(self.id) 68 69def init(link): 70 link.vlan = VLANLink(link._rtnl_link) 71 return link.vlan 72