Blame libmultipath/switchgroup.c

Packit Service 0af388
/*
Packit Service 0af388
 * Copyright (c) 2005 Christophe Varoqui
Packit Service 0af388
 * Copyright (c) 2005 Edward Goggin, EMC
Packit Service 0af388
 */
Packit Service 0af388
#include "checkers.h"
Packit Service 0af388
#include "vector.h"
Packit Service 0af388
#include "structs.h"
Packit Service 0af388
#include "switchgroup.h"
Packit Service 0af388
Packit Service 0af388
void path_group_prio_update(struct pathgroup *pgp)
Packit Service 0af388
{
Packit Service 0af388
	int i;
Packit Service 0af388
	int priority = 0;
Packit Service 0af388
	int marginal = 0;
Packit Service 0af388
	struct path * pp;
Packit Service 0af388
Packit Service 0af388
	pgp->enabled_paths = 0;
Packit Service 0af388
	if (!pgp->paths) {
Packit Service 0af388
		pgp->priority = 0;
Packit Service 0af388
		return;
Packit Service 0af388
	}
Packit Service 0af388
	vector_foreach_slot (pgp->paths, pp, i) {
Packit Service 0af388
		if (pp->marginal)
Packit Service 0af388
			marginal++;
Packit Service 0af388
		if (pp->state == PATH_UP ||
Packit Service 0af388
		    pp->state == PATH_GHOST) {
Packit Service 0af388
			priority += pp->priority;
Packit Service 0af388
			pgp->enabled_paths++;
Packit Service 0af388
		}
Packit Service 0af388
	}
Packit Service 0af388
	if (pgp->enabled_paths)
Packit Service 0af388
		pgp->priority = priority / pgp->enabled_paths;
Packit Service 0af388
	else
Packit Service 0af388
		pgp->priority = 0;
Packit Service 0af388
	if (marginal && marginal == i)
Packit Service 0af388
		pgp->marginal = 1;
Packit Service 0af388
}
Packit Service 0af388
Packit Service 0af388
int select_path_group(struct multipath *mpp)
Packit Service 0af388
{
Packit Service 0af388
	int i;
Packit Service 0af388
	int normal_pgp = 0;
Packit Service 0af388
	int max_priority = 0;
Packit Service 0af388
	int bestpg = 1;
Packit Service 0af388
	int max_enabled_paths = 1;
Packit Service 0af388
	struct pathgroup * pgp;
Packit Service 0af388
Packit Service 0af388
	if (!mpp->pg)
Packit Service 0af388
		return 1;
Packit Service 0af388
Packit Service 0af388
	vector_foreach_slot (mpp->pg, pgp, i) {
Packit Service 0af388
		if (!pgp->paths)
Packit Service 0af388
			continue;
Packit Service 0af388
Packit Service 0af388
		path_group_prio_update(pgp);
Packit Service 0af388
		if (pgp->marginal && normal_pgp)
Packit Service 0af388
			continue;
Packit Service 0af388
		if (pgp->enabled_paths) {
Packit Service 0af388
			if (!pgp->marginal && !normal_pgp) {
Packit Service 0af388
				normal_pgp = 1;
Packit Service 0af388
				max_priority = pgp->priority;
Packit Service 0af388
				max_enabled_paths = pgp->enabled_paths;
Packit Service 0af388
				bestpg = i + 1;
Packit Service 0af388
			} else if (pgp->priority > max_priority) {
Packit Service 0af388
				max_priority = pgp->priority;
Packit Service 0af388
				max_enabled_paths = pgp->enabled_paths;
Packit Service 0af388
				bestpg = i + 1;
Packit Service 0af388
			} else if (pgp->priority == max_priority) {
Packit Service 0af388
				if (pgp->enabled_paths > max_enabled_paths) {
Packit Service 0af388
					max_enabled_paths = pgp->enabled_paths;
Packit Service 0af388
					bestpg = i + 1;
Packit Service 0af388
				}
Packit Service 0af388
			}
Packit Service 0af388
		}
Packit Service 0af388
	}
Packit Service 0af388
	return bestpg;
Packit Service 0af388
}