1b52b8ea7856ebaa969675545a5872653d402b05
[multipath-tools/.git] / libmultipath / prioritizers / alua.c
1 /*
2  * (C) Copyright IBM Corp. 2004, 2005   All Rights Reserved.
3  *
4  * main.c
5  *
6  * Tool to make use of a SCSI-feature called Asymmetric Logical Unit Access.
7  * It determines the ALUA state of a device and prints a priority value to
8  * stdout.
9  *
10  * Author(s): Jan Kunigk
11  *            S. Bader <shbader@de.ibm.com>
12  * 
13  * This file is released under the GPL.
14  */
15 #include <stdio.h>
16
17 #include <debug.h>
18 #include <prio.h>
19
20 #include "alua.h"
21
22 #define ALUA_PRIO_NOT_SUPPORTED                 1
23 #define ALUA_PRIO_RTPG_FAILED                   2
24 #define ALUA_PRIO_GETAAS_FAILED                 3
25 #define ALUA_PRIO_TPGS_FAILED                   4
26
27 int
28 get_alua_info(int fd)
29 {
30         char *  aas_string[] = {
31                 [AAS_OPTIMIZED]         = "active/optimized",
32                 [AAS_NON_OPTIMIZED]     = "active/non-optimized",
33                 [AAS_STANDBY]           = "standby",
34                 [AAS_UNAVAILABLE]       = "unavailable",
35                 [AAS_TRANSITIONING]     = "transitioning between states",
36         };
37         int     rc;
38         int     tpg;
39
40         rc = get_target_port_group_support(fd);
41         if (rc < 0)
42                 return -ALUA_PRIO_TPGS_FAILED;
43
44         if (rc == TPGS_NONE)
45                 return -ALUA_PRIO_NOT_SUPPORTED;
46
47         tpg = get_target_port_group(fd);
48         if (tpg < 0)
49                 return -ALUA_PRIO_RTPG_FAILED;
50
51         condlog(3, "reported target port group is %i", tpg);
52         rc = get_asymmetric_access_state(fd, tpg);
53         if (rc < 0)
54                 return -ALUA_PRIO_GETAAS_FAILED;
55
56         condlog(3, "aas = [%s]",
57                 (aas_string[rc]) ? aas_string[rc] : "invalid/reserved");
58         return rc;
59 }
60
61 int getprio (struct path * pp)
62 {
63         int rc = get_alua_info(pp->fd);
64         if (rc >= 0) {
65                 switch(rc) {
66                         case AAS_OPTIMIZED:
67                                 rc = 50;
68                                 break;
69                         case AAS_NON_OPTIMIZED:
70                                 rc = 10;
71                                 break;
72                         case AAS_STANDBY:
73                                 rc = 1;
74                                 break;
75                         default:
76                                 rc = 0;
77                 }
78         } else {
79                 switch(-rc) {
80                         case ALUA_PRIO_NOT_SUPPORTED:
81                                 condlog(0, "%s: alua not supported", pp->dev);
82                                 break;
83                         case ALUA_PRIO_RTPG_FAILED:
84                                 condlog(0, "%s: couldn't get target port group", pp->dev);
85                                 break;
86                         case ALUA_PRIO_GETAAS_FAILED:
87                                 condlog(0, "%s: couln't get asymmetric access state", pp->dev);
88                                 break;
89                         case ALUA_PRIO_TPGS_FAILED:
90                                 condlog(3, "%s: couln't get supported alua states", pp->dev);
91                                 break;
92                 }
93         }
94         return rc;
95 }