025a95d540a7c5e0bc31ef3274fd7a1cc630b6c7
[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 extern void
11 path_group_prio_update (struct pathgroup * pgp)
12 {
13         int i;
14         int priority = 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->state != PATH_DOWN) {
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 extern int
35 select_path_group (struct multipath * mpp)
36 {
37         int i;
38         int max_priority = 0;
39         int bestpg = 1;
40         int max_enabled_paths = 1;
41         struct pathgroup * pgp;
42
43         if (!mpp->pg)
44                 return 1;
45
46         vector_foreach_slot (mpp->pg, pgp, i) {
47                 if (!pgp->paths)
48                         continue;
49
50                 path_group_prio_update(pgp);
51                 if (pgp->enabled_paths) {
52                         if (pgp->priority > max_priority) {
53                                 max_priority = pgp->priority;
54                                 max_enabled_paths = pgp->enabled_paths;
55                                 bestpg = i + 1;
56                         } else if (pgp->priority == max_priority) {
57                                 if (pgp->enabled_paths > max_enabled_paths) {
58                                         max_enabled_paths = pgp->enabled_paths;
59                                         bestpg = i + 1;
60                                 }
61                         }
62                 }
63         }
64         return bestpg;
65 }