multipathd: fix reservation_key check
[multipath-tools/.git] / libmultipath / switchgroup.c
1 /*
2  * Copyright (c) 2005 Christophe Varoqui
3  * Copyright (c) 2005 Edward Goggin, EMC
4  */
5 #include "checkers.h"
6 #include "vector.h"
7 #include "structs.h"
8 #include "switchgroup.h"
9
10 void path_group_prio_update(struct pathgroup *pgp)
11 {
12         int i;
13         int priority = 0;
14         struct path * pp;
15
16         pgp->enabled_paths = 0;
17         if (!pgp->paths) {
18                 pgp->priority = 0;
19                 return;
20         }
21         vector_foreach_slot (pgp->paths, pp, i) {
22                 if (pp->state == PATH_UP ||
23                     pp->state == PATH_GHOST) {
24                         priority += pp->priority;
25                         pgp->enabled_paths++;
26                 }
27         }
28         if (pgp->enabled_paths)
29                 pgp->priority = priority / pgp->enabled_paths;
30         else
31                 pgp->priority = 0;
32 }
33
34 int select_path_group(struct multipath *mpp)
35 {
36         int i;
37         int max_priority = 0;
38         int bestpg = 1;
39         int max_enabled_paths = 1;
40         struct pathgroup * pgp;
41
42         if (!mpp->pg)
43                 return 1;
44
45         vector_foreach_slot (mpp->pg, pgp, i) {
46                 if (!pgp->paths)
47                         continue;
48
49                 path_group_prio_update(pgp);
50                 if (pgp->enabled_paths) {
51                         if (pgp->priority > max_priority) {
52                                 max_priority = pgp->priority;
53                                 max_enabled_paths = pgp->enabled_paths;
54                                 bestpg = i + 1;
55                         } else if (pgp->priority == max_priority) {
56                                 if (pgp->enabled_paths > max_enabled_paths) {
57                                         max_enabled_paths = pgp->enabled_paths;
58                                         bestpg = i + 1;
59                                 }
60                         }
61                 }
62         }
63         return bestpg;
64 }