@@ -33,16 +33,16 @@ def set_vf_admin_state(self, pf_ifname, vf_idx, state):
33
33
:param state: desired admin state as defined in
34
34
networking_mlnx.internal.netdev_ops.constants
35
35
"""
36
- try :
37
- ip = pyroute2 . IPRoute ()
38
- link_idx = ip .link_lookup (ifname = pf_ifname )[0 ]
39
- ip .link (
40
- 'set' , index = link_idx , vf = {'vf' : int (vf_idx ),
41
- 'link_state' : state })
42
- except IndexError :
43
- raise exceptions .NetworkInterfaceNotFound (pf_ifname )
44
- except pyroute2 .NetlinkError as e :
45
- raise exceptions .NetlinkRuntimeError (e )
36
+ with pyroute2 . IPRoute () as ip :
37
+ try :
38
+ link_idx = ip .link_lookup (ifname = pf_ifname )[0 ]
39
+ ip .link (
40
+ 'set' , index = link_idx , vf = {'vf' : int (vf_idx ),
41
+ 'link_state' : state })
42
+ except IndexError :
43
+ raise exceptions .NetworkInterfaceNotFound (pf_ifname )
44
+ except pyroute2 .NetlinkError as e :
45
+ raise exceptions .NetlinkRuntimeError (e )
46
46
47
47
def set_link_state (self , ifname , state ):
48
48
"""Set net device link state
@@ -51,14 +51,14 @@ def set_link_state(self, ifname, state):
51
51
:param state: desired link state as defined in
52
52
networking_mlnx.internal.netdev_ops.constants
53
53
"""
54
- try :
55
- ip = pyroute2 . IPRoute ()
56
- link_idx = ip .link_lookup (ifname = ifname )[0 ]
57
- ip .link ('set' , index = link_idx , state = state )
58
- except IndexError :
59
- raise exceptions .NetworkInterfaceNotFound (ifname )
60
- except pyroute2 .NetlinkError as e :
61
- raise exceptions .NetlinkRuntimeError (e )
54
+ with pyroute2 . IPRoute () as ip :
55
+ try :
56
+ link_idx = ip .link_lookup (ifname = ifname )[0 ]
57
+ ip .link ('set' , index = link_idx , state = state )
58
+ except IndexError :
59
+ raise exceptions .NetworkInterfaceNotFound (ifname )
60
+ except pyroute2 .NetlinkError as e :
61
+ raise exceptions .NetlinkRuntimeError (e )
62
62
63
63
def set_vf_guid (self , pf_ifname , vf_idx , guid ):
64
64
"""Set vf administrative port and node GUID
@@ -68,17 +68,17 @@ def set_vf_guid(self, pf_ifname, vf_idx, guid):
68
68
:param guid: 64bit guid str in xx:xx:xx:xx:xx:xx:xx:xx format
69
69
where x is a hexadecimal digit.
70
70
"""
71
- try :
72
- ip = pyroute2 . IPRoute ()
73
- link_idx = ip .link_lookup (ifname = pf_ifname )[0 ]
74
- ip .link ('set' , index = link_idx , vf = {'vf' : int (vf_idx ),
75
- 'ib_port_guid' : guid })
76
- ip .link ('set' , index = link_idx ,
77
- vf = {'vf' : int (vf_idx ), 'ib_node_guid' : guid })
78
- except IndexError :
79
- raise exceptions .NetworkInterfaceNotFound (pf_ifname )
80
- except pyroute2 .NetlinkError as e :
81
- raise exceptions .NetlinkRuntimeError (e )
71
+ with pyroute2 . IPRoute () as ip :
72
+ try :
73
+ link_idx = ip .link_lookup (ifname = pf_ifname )[0 ]
74
+ ip .link ('set' , index = link_idx , vf = {'vf' : int (vf_idx ),
75
+ 'ib_port_guid' : guid })
76
+ ip .link ('set' , index = link_idx ,
77
+ vf = {'vf' : int (vf_idx ), 'ib_node_guid' : guid })
78
+ except IndexError :
79
+ raise exceptions .NetworkInterfaceNotFound (pf_ifname )
80
+ except pyroute2 .NetlinkError as e :
81
+ raise exceptions .NetlinkRuntimeError (e )
82
82
83
83
def get_vf_guid (self , pf_ifname , vf_idx ):
84
84
"""Get vf administrative GUID
@@ -91,14 +91,14 @@ def get_vf_guid(self, pf_ifname, vf_idx):
91
91
NOTE: while there are two GUIDs assigned per VF (port and node GUID)
92
92
we assume they are the same and return just one value.
93
93
"""
94
- try :
95
- ip = pyroute2 . IPRoute ()
96
- link_idx = ip .link_lookup (ifname = pf_ifname )[0 ]
97
- attrs = ip .link ('get' , index = link_idx , ext_mask = 1 )[0 ]
98
- except IndexError :
99
- raise exceptions .NetworkInterfaceNotFound (pf_ifname )
100
- except pyroute2 .NetlinkError as e :
101
- raise exceptions .NetlinkRuntimeError (e )
94
+ with pyroute2 . IPRoute () as ip :
95
+ try :
96
+ link_idx = ip .link_lookup (ifname = pf_ifname )[0 ]
97
+ attrs = ip .link ('get' , index = link_idx , ext_mask = 1 )[0 ]
98
+ except IndexError :
99
+ raise exceptions .NetworkInterfaceNotFound (pf_ifname )
100
+ except pyroute2 .NetlinkError as e :
101
+ raise exceptions .NetlinkRuntimeError (e )
102
102
103
103
vf_attr = (attrs .get_attr ('IFLA_VFINFO_LIST' ).
104
104
get_attrs ("IFLA_VF_INFO" ))[int (vf_idx )]
0 commit comments