multipath-tools: add alias_prefix to multipath.conf.5
[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_UP ||
24                     pp->state == PATH_GHOST) {
25                         priority += pp->priority;
26                         pgp->enabled_paths++;
27                 }
28         }
29         if (pgp->enabled_paths)
30                 pgp->priority = priority / pgp->enabled_paths;
31         else
32                 pgp->priority = 0;
33 }
34
35 extern int
36 select_path_group (struct multipath * mpp)
37 {
38         int i;
39         int max_priority = 0;
40         int bestpg = 1;
41         int max_enabled_paths = 1;
42         struct pathgroup * pgp;
43
44         if (!mpp->pg)
45                 return 1;
46
47         vector_foreach_slot (mpp->pg, pgp, i) {
48                 if (!pgp->paths)
49                         continue;
50
51                 path_group_prio_update(pgp);
52                 if (pgp->enabled_paths) {
53                         if (pgp->priority > max_priority) {
54                                 max_priority = pgp->priority;
55                                 max_enabled_paths = pgp->enabled_paths;
56                                 bestpg = i + 1;
57                         } else if (pgp->priority == max_priority) {
58                                 if (pgp->enabled_paths > max_enabled_paths) {
59                                         max_enabled_paths = pgp->enabled_paths;
60                                         bestpg = i + 1;
61                                 }
62                         }
63                 }
64         }
65         return bestpg;
66 }