summaryrefslogtreecommitdiff
path: root/rtslib/alua.py
blob: 86a4dd361eefdb3975bfd070d9822495e83829f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
'''
Implements the RTS ALUA Target Port Group class.

This file is part of RTSLib.
Copyright (c) 2016 by Red Hat, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    License for the specific language governing permissions and limitations
    under the License.
'''

from .node import CFSNode
from .utils import RTSLibError, fread, fwrite

alua_rw_params = ['alua_access_state', 'alua_access_status',
                  'alua_write_metadata', 'alua_access_type', 'preferred',
                  'nonop_delay_msecs', 'trans_delay_msecs',
                  'implicit_trans_secs', 'alua_support_offline',
                  'alua_support_standby', 'alua_support_transitioning',
                  'alua_support_active_nonoptimized',
                  'alua_support_unavailable', 'alua_support_active_optimized']
alua_ro_params = ['tg_pt_gp_id', 'members', 'alua_support_lba_dependent']
alua_types = ['None', 'Implicit', 'Explicit', 'Implicit and Explicit']
alua_statuses = ['None', 'Altered by Explicit STPG', 'Altered by Implicit ALUA']

class ALUATargetPortGroup(CFSNode):
    """
    ALUA Target Port Group interface
    """

    def __repr__(self):
        return "<ALUA TPG %s>" % self.name

    def __init__(self, storage_object, name, tag=None):
        """
        @param storage_object: backstore storage object to create ALUA group for
        @param name: name of ALUA group
        @param tag: target port group id. If not passed in, try to look
                    up existing ALUA TPG with the same name
        """

        # default_tg_pt_gp takes tag 1
        if tag is not None and (tag > 65535 or tag < 1):
            raise RTSLibError("The TPG Tag must be between 1 and 65535")

        super(ALUATargetPortGroup, self).__init__()
        self.name = name
        self.storage_object = storage_object

        self._path = "%s/alua/%s" % (storage_object.path, name)

        if tag is not None:
            try:
                self._create_in_cfs_ine('create')
            except OSError as msg:
                raise RTSLibError(msg)

            try:
                fwrite("%s/tg_pt_gp_id" % self._path, tag)
            except IOError as msg:
                self.delete()
                raise RTSLibError("Cannot set id to %d: %s" % (tag, str(msg)))
        else:
            try:
                self._create_in_cfs_ine('lookup')
            except OSError as msg:
                raise RTSLibError(msg)

    # Public

    def delete(self):
        """
        Delete ALUA TPG and unmap from LUNs
        """
        self._check_self()

        # default_tg_pt_gp created by the kernel and cannot be deleted
        if self.name == "default_tg_pt_gp":
            raise RTSLibError("Can not delete default_tg_pt_gp")

        # This will reset the ALUA tpg to default_tg_pt_gp
        super(ALUATargetPortGroup, self).delete()

    def _get_alua_access_state(self):
        self._check_self()
        path = "%s/alua_access_state" % self.path
        return int(fread(path))

    def _set_alua_access_state(self, newstate):
        self._check_self()
        path = "%s/alua_access_state" % self.path
        try:
            fwrite(path, str(int(newstate)))
        except IOError as e:
            raise RTSLibError("Cannot change ALUA state: %s" % e)

    def _get_alua_access_status(self):
        self._check_self()
        path = "%s/alua_access_status" % self.path
        status = fread(path)
        return alua_statuses.index(status)

    def _set_alua_access_status(self, newstatus):
        self._check_self()
        path = "%s/alua_access_status" % self.path
        try:
            fwrite(path, str(int(newstatus)))
        except IOError as e:
            raise RTSLibError("Cannot change ALUA status: %s" % e)

    def _get_alua_access_type(self):
        self._check_self()
        path = "%s/alua_access_type" % self.path
        alua_type = fread(path)
        return alua_types.index(alua_type)

    def _set_alua_access_type(self, access_type):
        self._check_self()
        path = "%s/alua_access_type" % self.path
        try:
            fwrite(path, str(int(access_type)))
        except IOError as e:
            raise RTSLibError("Cannot change ALUA access type: %s" % e)

    def _get_preferred(self):
        self._check_self()
        path = "%s/preferred" % self.path
        return int(fread(path))

    def _set_preferred(self, pref):
        self._check_self()
        path = "%s/preferred" % self.path
        try:
            fwrite(path, str(int(pref)))
        except IOError as e:
            raise RTSLibError("Cannot set preferred: %s" % e)

    def _get_alua_write_metadata(self):
        self._check_self()
        path = "%s/alua_write_metadata" % self.path
        return int(fread(path))

    def _set_alua_write_metadata(self, pref):
        self._check_self()
        path = "%s/alua_write_metadata" % self.path
        try:
            fwrite(path, str(int(pref)))
        except IOError as e:
            raise RTSLibError("Cannot set alua_write_metadata: %s" % e)

    def _get_alua_support_active_nonoptimized(self):
        self._check_self()
        path = "%s/alua_support_active_nonoptimized" % self.path
        return int(fread(path))

    def _set_alua_support_active_nonoptimized(self, enabled):
        self._check_self()
        path = "%s/alua_support_active_nonoptimized" % self.path
        try:
            fwrite(path, str(int(enabled)))
        except IOError as e:
            raise RTSLibError("Cannot set alua_support_active_nonoptimized: %s" % e)

    def _get_alua_support_active_optimized(self):
        self._check_self()
        path = "%s/alua_support_active_optimized" % self.path
        return int(fread(path))

    def _set_alua_support_active_optimized(self, enabled):
        self._check_self()
        path = "%s/alua_support_active_optimized" % self.path
        try:
            fwrite(path, str(int(enabled)))
        except IOError as e:
            raise RTSLibError("Cannot set alua_support_active_optimized: %s" % e)

    def _get_alua_support_offline(self):
        self._check_self()
        path = "%s/alua_support_offline" % self.path
        return int(fread(path))

    def _set_alua_support_offline(self, enabled):
        self._check_self()
        path = "%s/alua_support_offline" % self.path
        try:
            fwrite(path, str(int(enabled)))
        except IOError as e:
            raise RTSLibError("Cannot set alua_support_offline: %s" % e)

    def _get_alua_support_unavailable(self):
        self._check_self()
        path = "%s/alua_support_unavailable" % self.path
        return int(fread(path))

    def _set_alua_support_unavailable(self, enabled):
        self._check_self()
        path = "%s/alua_support_unavailable" % self.path
        try:
            fwrite(path, str(int(enabled)))
        except IOError as e:
            raise RTSLibError("Cannot set alua_support_unavailable: %s" % e)

    def _get_alua_support_standby(self):
        self._check_self()
        path = "%s/alua_support_standby" % self.path
        return int(fread(path))

    def _set_alua_support_standby(self, enabled):
        self._check_self()
        path = "%s/alua_support_standby" % self.path
        try:
            fwrite(path, str(int(enabled)))
        except IOError as e:
            raise RTSLibError("Cannot set alua_support_standby: %s" % e)

    def _get_alua_support_transitioning(self):
        self._check_self()
        path = "%s/alua_support_transitioning" % self.path
        return int(fread(path))

    def _set_alua_support_transitioning(self, enabled):
        self._check_self()
        path = "%s/alua_support_transitioning" % self.path
        try:
            fwrite(path, str(int(enabled)))
        except IOError as e:
            raise RTSLibError("Cannot set alua_support_transitioning: %s" % e)

    def _get_alua_support_lba_dependent(self):
        self._check_self()
        path = "%s/alua_support_lba_dependent" % self.path
        return int(fread(path))

    def _get_members(self):
        self._check_self()
        path = "%s/members" % self.path
        return fread(path)

    def _get_tg_pt_gp_id(self):
        self._check_self()
        path = "%s/tg_pt_gp_id" % self.path
        return int(fread(path))

    def _get_trans_delay_msecs(self):
        self._check_self()
        path = "%s/trans_delay_msecs" % self.path
        return int(fread(path))

    def _set_trans_delay_msecs(self, secs):
        self._check_self()
        path = "%s/trans_delay_msecs" % self.path
        try:
            fwrite(path, str(int(secs)))
        except IOError as e:
            raise RTSLibError("Cannot set trans_delay_msecs: %s" % e)

    def _get_implicit_trans_secs(self):
        self._check_self()
        path = "%s/implicit_trans_secs" % self.path
        return int(fread(path))

    def _set_implicit_trans_secs(self, secs):
        self._check_self()
        path = "%s/implicit_trans_secs" % self.path
        try:
            fwrite(path, str(int(secs)))
        except IOError as e:
            raise RTSLibError("Cannot set implicit_trans_secs: %s" % e)

    def _get_nonop_delay_msecs(self):
        self._check_self()
        path = "%s/nonop_delay_msecs" % self.path
        return int(fread(path))

    def _set_nonop_delay_msecs(self, delay):
        self._check_self()
        path = "%s/nonop_delay_msecs" % self.path
        try:
            fwrite(path, str(int(delay)))
        except IOError as e:
            raise RTSLibError("Cannot set nonop_delay_msecs: %s" % e)

    def dump(self):
        d = super(ALUATargetPortGroup, self).dump()
        d['name'] = self.name
        d['tg_pt_gp_id'] = self.tg_pt_gp_id
        for param in alua_rw_params:
            d[param] = getattr(self, param, None)
        return d

    alua_access_state = property(_get_alua_access_state, _set_alua_access_state,
                                 doc="Get or set ALUA state. "
                                     "0 = Active/optimized, "
                                     "1 = Active/non-optimized, "
                                     "2 = Standby, "
                                     "3 = Unavailable, "
                                     "4 = LBA Dependent, "
                                     "14 = Offline, "
                                     "15 = Transitioning")

    alua_access_type = property(_get_alua_access_type, _set_alua_access_type,
                                doc="Get or set ALUA access type. "
                                    "1 = Implicit, 2 = Explicit, 3 = Both")

    alua_access_status = property(_get_alua_access_status,
                                  _set_alua_access_status,
                                  doc="Get or set ALUA access status. "
                                      "0 = None, "
                                      "1 = Altered by Explicit STPG, "
                                      "2 = Altered by Implicit ALUA")

    preferred = property(_get_preferred, _set_preferred,
                         doc="Get or set preferred bit. 1 = Pref, 0 Not-Pre")

    alua_write_metadata = property(_get_alua_write_metadata,
                                   _set_alua_write_metadata,
                                   doc="Get or set alua_write_metadata flag. "
                                       "enable (1) or disable (0)")

    tg_pt_gp_id = property(_get_tg_pt_gp_id, doc="Get ALUA Target Port Group ID")

    members = property(_get_members, doc="Get LUNs in Target Port Group")

    alua_support_active_nonoptimized = property(_get_alua_support_active_nonoptimized,
                                                _set_alua_support_active_nonoptimized,
                                                doc="Enable (1) or disable (0) "
                                                    "Active/non-optimized support")

    alua_support_active_optimized = property(_get_alua_support_active_optimized,
                                             _set_alua_support_active_optimized,
                                             doc="Enable (1) or disable (0) "
                                                 "Active/optimized support")

    alua_support_offline = property(_get_alua_support_offline,
                                    _set_alua_support_offline,
                                    doc="Enable (1) or disable (0) "
                                        "offline support")

    alua_support_unavailable = property(_get_alua_support_unavailable,
                                        _set_alua_support_unavailable,
                                        doc="enable (1) or disable (0) "
                                            "unavailable support")

    alua_support_standby = property(_get_alua_support_standby,
                                    _set_alua_support_standby,
                                    doc="enable (1) or disable (0) "
                                        "standby support")

    alua_support_lba_dependent = property(_get_alua_support_lba_dependent,
                                          doc="show lba_dependent support "
                                              "enabled (1) or disabled (0)")

    alua_support_transitioning = property(_get_alua_support_transitioning,
                                          _set_alua_support_transitioning,
                                          doc="enable (1) or disable (0) "
                                          "transitioning support")

    trans_delay_msecs = property(_get_trans_delay_msecs,
                                 _set_trans_delay_msecs,
                                 doc="msecs to delay state transition")

    implicit_trans_secs = property(_get_implicit_trans_secs,
                                   _set_implicit_trans_secs,
                                   doc="implicit transition time limit")

    nonop_delay_msecs = property(_get_nonop_delay_msecs, _set_nonop_delay_msecs,
                                 doc="msecs to delay IO when non-optimized")

    @classmethod
    def setup(cls, storage_obj, alua_tpg, err_func):
        name = alua_tpg['name']
        if name == 'default_tg_pt_gp':
            return

        alua_tpg_obj = cls(storage_obj, name, alua_tpg['tg_pt_gp_id'])
        for param in alua_rw_params:
            setattr(alua_tpg_obj, param, alua_tpg[param])