File: switchgroup.c

package info (click to toggle)
multipath-tools 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,992 kB
  • sloc: ansic: 63,788; perl: 1,622; makefile: 729; sh: 647; pascal: 150
file content (83 lines) | stat: -rw-r--r-- 1,753 bytes parent folder | download | duplicates (2)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (c) 2005 Christophe Varoqui
 * Copyright (c) 2005 Edward Goggin, EMC
 */
#include "checkers.h"
#include "vector.h"
#include "structs.h"
#include "switchgroup.h"

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

	pgp->enabled_paths = 0;
	if (!pgp->paths) {
		pgp->priority = 0;
		return;
	}
	vector_foreach_slot (pgp->paths, pp, i) {
		if (pp->marginal)
			marginal++;
		if (pp->state == PATH_UP ||
		    pp->state == PATH_GHOST) {
			if (pp->priority != PRIO_UNDEF) {
				defined_prios++;
				priority += pp->priority;
			}
			pgp->enabled_paths++;
		}
	}
	if (defined_prios)
		pgp->priority = priority / defined_prios;
	else if (pgp->enabled_paths)
		pgp->priority = PRIO_UNDEF;
	else
		pgp->priority = 0;
	if (marginal && marginal == i)
		pgp->marginal = 1;
}

int select_path_group(struct multipath *mpp)
{
	int i;
	int normal_pgp = 0;
	int max_priority = 0;
	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->marginal && normal_pgp)
			continue;
		if (pgp->enabled_paths) {
			if (!pgp->marginal && !normal_pgp) {
				normal_pgp = 1;
				max_priority = pgp->priority;
				max_enabled_paths = pgp->enabled_paths;
				bestpg = i + 1;
			} else if (pgp->priority > max_priority) {
				max_priority = pgp->priority;
				max_enabled_paths = pgp->enabled_paths;
				bestpg = i + 1;
			} else if (pgp->priority == max_priority) {
				if (pgp->enabled_paths > max_enabled_paths) {
					max_enabled_paths = pgp->enabled_paths;
					bestpg = i + 1;
				}
			}
		}
	}
	return bestpg;
}