summaryrefslogtreecommitdiff
path: root/libmultipath/switchgroup.c
blob: d995cd3f241a6dce69521ca3686d509c9e6bc176 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * Copyright (c) 2005 Christophe Varoqui
 * Copyright (c) 2005 Edward Goggin, EMC
 */
#include "checkers.h"
#include "vector.h"
#include "structs.h"
#include "switchgroup.h"

extern void
path_group_prio_update (struct pathgroup * pgp)
{
	int i;
	int priority = 0;
	struct path * pp;

	pgp->enabled_paths = 0;
	if (!pgp->paths) {
		pgp->priority = 0;
		return;
	}
	vector_foreach_slot (pgp->paths, pp, i) {
		if (pp->state != PATH_DOWN) {
			priority += pp->priority;
			pgp->enabled_paths++;
		}
	}
	pgp->priority = priority;
}

extern int
select_path_group (struct multipath * mpp)
{
	int i;
	int max_priority = 0, avg_priority;
	int bestpg = 1;
	int max_enabled_paths = 1;
	struct pathgroup * pgp;

	if (!mpp->pg)
		return 1;

	vector_foreach_slot (mpp->pg, pgp, i) {
		if (!pgp->paths)
			continue;

		path_group_prio_update(pgp);
		if (pgp->enabled_paths) {
			avg_priority = pgp->priority / pgp->enabled_paths;
			if (avg_priority > max_priority) {
				max_priority = avg_priority;
				max_enabled_paths = pgp->enabled_paths;
				bestpg = i + 1;
			} else if (avg_priority == max_priority) {
				if (pgp->enabled_paths > max_enabled_paths) {
					max_enabled_paths = pgp->enabled_paths;
					bestpg = i + 1;
				}
			}
		}
	}
	return bestpg;
}