libmultipath: make group_paths handle marginal paths
[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         int marginal = 0;
15         struct path * pp;
16
17         pgp->enabled_paths = 0;
18         if (!pgp->paths) {
19                 pgp->priority = 0;
20                 return;
21         }
22         vector_foreach_slot (pgp->paths, pp, i) {
23                 if (pp->marginal)
24                         marginal++;
25                 if (pp->state == PATH_UP ||
26                     pp->state == PATH_GHOST) {
27                         priority += pp->priority;
28                         pgp->enabled_paths++;
29                 }
30         }
31         if (pgp->enabled_paths)
32                 pgp->priority = priority / pgp->enabled_paths;
33         else
34                 pgp->priority = 0;
35         if (marginal && marginal == i)
36                 pgp->marginal = 1;
37 }
38
39 int select_path_group(struct multipath *mpp)
40 {
41         int i;
42         int normal_pgp = 0;
43         int max_priority = 0;
44         int bestpg = 1;
45         int max_enabled_paths = 1;
46         struct pathgroup * pgp;
47
48         if (!mpp->pg)
49                 return 1;
50
51         vector_foreach_slot (mpp->pg, pgp, i) {
52                 if (!pgp->paths)
53                         continue;
54
55                 path_group_prio_update(pgp);
56                 if (pgp->marginal && normal_pgp)
57                         continue;
58                 if (pgp->enabled_paths) {
59                         if (!pgp->marginal && !normal_pgp) {
60                                 normal_pgp = 1;
61                                 max_priority = pgp->priority;
62                                 max_enabled_paths = pgp->enabled_paths;
63                                 bestpg = i + 1;
64                         } else if (pgp->priority > max_priority) {
65                                 max_priority = pgp->priority;
66                                 max_enabled_paths = pgp->enabled_paths;
67                                 bestpg = i + 1;
68                         } else if (pgp->priority == max_priority) {
69                                 if (pgp->enabled_paths > max_enabled_paths) {
70                                         max_enabled_paths = pgp->enabled_paths;
71                                         bestpg = i + 1;
72                                 }
73                         }
74                 }
75         }
76         return bestpg;
77 }