Blame SOURCES/0090-UPBZ-1080038-reorder-paths-for-round-robin.patch

4ae388
---
4ae388
 libmultipath/configure.c |  229 +++++++++++++++++++++++++++++++++++++++++++++++
4ae388
 libmultipath/configure.h |    2 
4ae388
 libmultipath/discovery.c |   87 +++++++++++++++++
4ae388
 libmultipath/discovery.h |    2 
4ae388
 libmultipath/structs.c   |   84 +++++++++++++++++
4ae388
 libmultipath/structs.h   |   25 ++++-
4ae388
 6 files changed, 427 insertions(+), 2 deletions(-)
4ae388
4ae388
Index: multipath-tools-130222/libmultipath/configure.c
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/configure.c
4ae388
+++ multipath-tools-130222/libmultipath/configure.c
4ae388
@@ -39,6 +39,219 @@
4ae388
 #include "uxsock.h"
4ae388
 #include "wwids.h"
4ae388
 
4ae388
+/* group paths in pg by host adapter
4ae388
+ */
4ae388
+int group_by_host_adapter(struct pathgroup *pgp, vector adapters)
4ae388
+{
4ae388
+	struct adapter_group *agp;
4ae388
+	struct host_group *hgp;
4ae388
+	struct path *pp, *pp1;
4ae388
+	char adapter_name1[SLOT_NAME_SIZE];
4ae388
+	char adapter_name2[SLOT_NAME_SIZE];
4ae388
+	int i, j;
4ae388
+	int found_hostgroup = 0;
4ae388
+
4ae388
+	while (VECTOR_SIZE(pgp->paths) > 0) {
4ae388
+
4ae388
+		pp = VECTOR_SLOT(pgp->paths, 0);
4ae388
+
4ae388
+		if (sysfs_get_host_adapter_name(pp, adapter_name1))
4ae388
+			goto out;
4ae388
+		/* create a new host adapter group
4ae388
+		 */
4ae388
+		agp = alloc_adaptergroup();
4ae388
+		if (!agp)
4ae388
+			goto out;
4ae388
+		agp->pgp = pgp;
4ae388
+
4ae388
+		strncpy(agp->adapter_name, adapter_name1, SLOT_NAME_SIZE);
4ae388
+		store_adaptergroup(adapters, agp);
4ae388
+
4ae388
+		/* create a new host port group
4ae388
+		 */
4ae388
+		hgp = alloc_hostgroup();
4ae388
+		if (!hgp)
4ae388
+			goto out;
4ae388
+		if (store_hostgroup(agp->host_groups, hgp))
4ae388
+			goto out;
4ae388
+
4ae388
+		hgp->host_no = pp->sg_id.host_no;
4ae388
+		agp->num_hosts++;
4ae388
+		if (store_path(hgp->paths, pp))
4ae388
+			goto out;
4ae388
+
4ae388
+		hgp->num_paths++;
4ae388
+		/* delete path from path group
4ae388
+		 */
4ae388
+		vector_del_slot(pgp->paths, 0);
4ae388
+
4ae388
+		/* add all paths belonging to same host adapter
4ae388
+		 */
4ae388
+		vector_foreach_slot(pgp->paths, pp1, i) {
4ae388
+			if (sysfs_get_host_adapter_name(pp1, adapter_name2))
4ae388
+				goto out;
4ae388
+			if (strcmp(adapter_name1, adapter_name2) == 0) {
4ae388
+				found_hostgroup = 0;
4ae388
+				vector_foreach_slot(agp->host_groups, hgp, j) {
4ae388
+					if (hgp->host_no == pp1->sg_id.host_no) {
4ae388
+						if (store_path(hgp->paths, pp1))
4ae388
+							goto out;
4ae388
+						hgp->num_paths++;
4ae388
+						found_hostgroup = 1;
4ae388
+						break;
4ae388
+					}
4ae388
+				}
4ae388
+				if (!found_hostgroup) {
4ae388
+					/* this path belongs to new host port
4ae388
+					 * within this adapter
4ae388
+					 */
4ae388
+					hgp = alloc_hostgroup();
4ae388
+					if (!hgp)
4ae388
+						goto out;
4ae388
+
4ae388
+					if (store_hostgroup(agp->host_groups, hgp))
4ae388
+						goto out;
4ae388
+
4ae388
+					agp->num_hosts++;
4ae388
+					if (store_path(hgp->paths, pp1))
4ae388
+						goto out;
4ae388
+
4ae388
+					hgp->host_no = pp1->sg_id.host_no;
4ae388
+					hgp->num_paths++;
4ae388
+				}
4ae388
+				/* delete paths from original path_group
4ae388
+				 * as they are added into adapter group now
4ae388
+				 */
4ae388
+				vector_del_slot(pgp->paths, i);
4ae388
+				i--;
4ae388
+			}
4ae388
+		}
4ae388
+	}
4ae388
+	return 0;
4ae388
+
4ae388
+out:	/* add back paths into pg as re-ordering failed
4ae388
+	 */
4ae388
+	vector_foreach_slot(adapters, agp, i) {
4ae388
+			vector_foreach_slot(agp->host_groups, hgp, j) {
4ae388
+				while (VECTOR_SIZE(hgp->paths) > 0) {
4ae388
+					pp = VECTOR_SLOT(hgp->paths, 0);
4ae388
+					if (store_path(pgp->paths, pp))
4ae388
+						condlog(3, "failed to restore "
4ae388
+						"path %s into path group",
4ae388
+						 pp->dev);
4ae388
+					vector_del_slot(hgp->paths, 0);
4ae388
+				}
4ae388
+			}
4ae388
+		}
4ae388
+	free_adaptergroup(adapters);
4ae388
+	return 1;
4ae388
+}
4ae388
+
4ae388
+/* re-order paths in pg by alternating adapters and host ports
4ae388
+ * for optimized selection
4ae388
+ */
4ae388
+int order_paths_in_pg_by_alt_adapters(struct pathgroup *pgp, vector adapters,
4ae388
+		 int total_paths)
4ae388
+{
4ae388
+	int next_adapter_index = 0;
4ae388
+	struct adapter_group *agp;
4ae388
+	struct host_group *hgp;
4ae388
+	struct path *pp;
4ae388
+
4ae388
+	while (total_paths > 0) {
4ae388
+		agp = VECTOR_SLOT(adapters, next_adapter_index);
4ae388
+		if (!agp) {
4ae388
+			condlog(0, "can't get adapter group %d", next_adapter_index);
4ae388
+			return 1;
4ae388
+		}
4ae388
+
4ae388
+		hgp = VECTOR_SLOT(agp->host_groups, agp->next_host_index);
4ae388
+		if (!hgp) {
4ae388
+			condlog(0, "can't get host group %d of adapter group %d", next_adapter_index, agp->next_host_index);
4ae388
+			return 1;
4ae388
+		}
4ae388
+
4ae388
+		if (!hgp->num_paths) {
4ae388
+			agp->next_host_index++;
4ae388
+			agp->next_host_index %= agp->num_hosts;
4ae388
+			next_adapter_index++;
4ae388
+			next_adapter_index %= VECTOR_SIZE(adapters);
4ae388
+			continue;
4ae388
+		}
4ae388
+
4ae388
+		pp  = VECTOR_SLOT(hgp->paths, 0);
4ae388
+
4ae388
+		if (store_path(pgp->paths, pp))
4ae388
+			return 1;
4ae388
+
4ae388
+		total_paths--;
4ae388
+
4ae388
+		vector_del_slot(hgp->paths, 0);
4ae388
+
4ae388
+		hgp->num_paths--;
4ae388
+
4ae388
+		agp->next_host_index++;
4ae388
+		agp->next_host_index %= agp->num_hosts;
4ae388
+		next_adapter_index++;
4ae388
+		next_adapter_index %= VECTOR_SIZE(adapters);
4ae388
+	}
4ae388
+
4ae388
+	/* all paths are added into path_group
4ae388
+	 * in crafted child order
4ae388
+	 */
4ae388
+	return 0;
4ae388
+}
4ae388
+
4ae388
+/* round-robin: order paths in path group to alternate
4ae388
+ * between all host adapters
4ae388
+ */
4ae388
+int rr_optimize_path_order(struct pathgroup *pgp)
4ae388
+{
4ae388
+	vector adapters;
4ae388
+	struct path *pp;
4ae388
+	int total_paths;
4ae388
+	int i;
4ae388
+
4ae388
+	total_paths = VECTOR_SIZE(pgp->paths);
4ae388
+	vector_foreach_slot(pgp->paths, pp, i) {
4ae388
+		if (pp->sg_id.proto_id != SCSI_PROTOCOL_FCP &&
4ae388
+			pp->sg_id.proto_id != SCSI_PROTOCOL_SAS &&
4ae388
+			pp->sg_id.proto_id != SCSI_PROTOCOL_ISCSI &&
4ae388
+			pp->sg_id.proto_id != SCSI_PROTOCOL_SRP) {
4ae388
+			/* return success as default path order
4ae388
+			 * is maintained in path group
4ae388
+			 */
4ae388
+			return 0;
4ae388
+		}
4ae388
+	}
4ae388
+	adapters = vector_alloc();
4ae388
+	if (!adapters)
4ae388
+		return 0;
4ae388
+
4ae388
+	/* group paths in path group by host adapters
4ae388
+	 */
4ae388
+	if (group_by_host_adapter(pgp, adapters)) {
4ae388
+		/* already freed adapters */
4ae388
+		condlog(3, "Failed to group paths by adapters");
4ae388
+		return 0;
4ae388
+	}
4ae388
+
4ae388
+	/* re-order paths in pg to alternate between adapters and host ports
4ae388
+	 */
4ae388
+	if (order_paths_in_pg_by_alt_adapters(pgp, adapters, total_paths)) {
4ae388
+		condlog(3, "Failed to re-order paths in pg by adapters "
4ae388
+			"and host ports");
4ae388
+		free_adaptergroup(adapters);
4ae388
+		/* return failure as original paths are
4ae388
+		 * removed form pgp
4ae388
+		 */
4ae388
+		return 1;
4ae388
+	}
4ae388
+
4ae388
+	free_adaptergroup(adapters);
4ae388
+	return 0;
4ae388
+}
4ae388
+
4ae388
 extern int
4ae388
 setup_map (struct multipath * mpp, char * params, int params_size)
4ae388
 {
4ae388
@@ -101,6 +314,22 @@ setup_map (struct multipath * mpp, char
4ae388
 	 */
4ae388
 	mpp->bestpg = select_path_group(mpp);
4ae388
 
4ae388
+	/* re-order paths in all path groups in an optimized way
4ae388
+	 * for round-robin path selectors to get maximum throughput.
4ae388
+	 */
4ae388
+	if (!strncmp(mpp->selector, "round-robin", 11)) {
4ae388
+		vector_foreach_slot(mpp->pg, pgp, i) {
4ae388
+			if (VECTOR_SIZE(pgp->paths) <= 2)
4ae388
+				continue;
4ae388
+			if (rr_optimize_path_order(pgp)) {
4ae388
+				condlog(2, "cannot re-order paths for "
4ae388
+					"optimization: %s",
4ae388
+					mpp->alias);
4ae388
+				return 1;
4ae388
+			}
4ae388
+		}
4ae388
+	}
4ae388
+
4ae388
 	/*
4ae388
 	 * transform the mp->pg vector of vectors of paths
4ae388
 	 * into a mp->params strings to feed the device-mapper
4ae388
Index: multipath-tools-130222/libmultipath/configure.h
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/configure.h
4ae388
+++ multipath-tools-130222/libmultipath/configure.h
4ae388
@@ -29,4 +29,4 @@ int reinstate_paths (struct multipath *m
4ae388
 int coalesce_paths (struct vectors *vecs, vector curmp, char * refwwid, int force_reload);
4ae388
 int get_refwwid (char * dev, enum devtypes dev_type, vector pathvec, char **wwid);
4ae388
 int reload_map(struct vectors *vecs, struct multipath *mpp, int refresh);
4ae388
-
4ae388
+int sysfs_get_host_adapter_name(struct path *pp, char *adapter_name);
4ae388
Index: multipath-tools-130222/libmultipath/discovery.c
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/discovery.c
4ae388
+++ multipath-tools-130222/libmultipath/discovery.c
4ae388
@@ -310,6 +310,93 @@ sysfs_get_tgt_nodename (struct path *pp,
4ae388
 	return 1;
4ae388
 }
4ae388
 
4ae388
+int sysfs_get_host_adapter_name(struct path *pp, char *adapter_name)
4ae388
+{
4ae388
+	int proto_id;
4ae388
+
4ae388
+	if (!pp || !adapter_name)
4ae388
+		return 1;
4ae388
+
4ae388
+	proto_id = pp->sg_id.proto_id;
4ae388
+
4ae388
+	if (proto_id != SCSI_PROTOCOL_FCP &&
4ae388
+	    proto_id != SCSI_PROTOCOL_SAS &&
4ae388
+	    proto_id != SCSI_PROTOCOL_ISCSI &&
4ae388
+	    proto_id != SCSI_PROTOCOL_SRP) {
4ae388
+		return 1;
4ae388
+	}
4ae388
+	/* iscsi doesn't have adapter info in sysfs
4ae388
+	 * get ip_address for grouping paths
4ae388
+	 */
4ae388
+	if (pp->sg_id.proto_id == SCSI_PROTOCOL_ISCSI)
4ae388
+		return sysfs_get_iscsi_ip_address(pp, adapter_name);
4ae388
+
4ae388
+	/* fetch adapter pci name for other protocols
4ae388
+	 */
4ae388
+	return sysfs_get_host_pci_name(pp, adapter_name);
4ae388
+}
4ae388
+
4ae388
+int sysfs_get_host_pci_name(struct path *pp, char *pci_name)
4ae388
+{
4ae388
+	struct udev_device *hostdev, *parent;
4ae388
+	char host_name[HOST_NAME_LEN];
4ae388
+	const char *driver_name, *value;
4ae388
+
4ae388
+	if (!pp || !pci_name)
4ae388
+		return 1;
4ae388
+
4ae388
+	sprintf(host_name, "host%d", pp->sg_id.host_no);
4ae388
+	hostdev = udev_device_new_from_subsystem_sysname(conf->udev,
4ae388
+			"scsi_host", host_name);
4ae388
+	if (!hostdev)
4ae388
+		return 1;
4ae388
+
4ae388
+	parent = udev_device_get_parent(hostdev);
4ae388
+	while (parent) {
4ae388
+		driver_name = udev_device_get_driver(parent);
4ae388
+		if (!driver_name) {
4ae388
+			parent = udev_device_get_parent(parent);
4ae388
+			continue;
4ae388
+		}
4ae388
+		if (!strcmp(driver_name, "pcieport"))
4ae388
+			break;
4ae388
+		parent = udev_device_get_parent(parent);
4ae388
+	}
4ae388
+	if (parent) {
4ae388
+		/* pci_device found
4ae388
+		 */
4ae388
+		value = udev_device_get_sysname(parent);
4ae388
+
4ae388
+		strncpy(pci_name, value, SLOT_NAME_SIZE);
4ae388
+		udev_device_unref(hostdev);
4ae388
+		return 0;
4ae388
+	}
4ae388
+	udev_device_unref(hostdev);
4ae388
+	return 1;
4ae388
+}
4ae388
+
4ae388
+int sysfs_get_iscsi_ip_address(struct path *pp, char *ip_address)
4ae388
+{
4ae388
+	struct udev_device *hostdev;
4ae388
+	char host_name[HOST_NAME_LEN];
4ae388
+	const char *value;
4ae388
+
4ae388
+	sprintf(host_name, "host%d", pp->sg_id.host_no);
4ae388
+	hostdev = udev_device_new_from_subsystem_sysname(conf->udev,
4ae388
+			"iscsi_host", host_name);
4ae388
+	if (hostdev) {
4ae388
+		value = udev_device_get_sysattr_value(hostdev,
4ae388
+				"ipaddress");
4ae388
+		if (value) {
4ae388
+			strncpy(ip_address, value, SLOT_NAME_SIZE);
4ae388
+			udev_device_unref(hostdev);
4ae388
+			return 0;
4ae388
+		} else
4ae388
+			udev_device_unref(hostdev);
4ae388
+	}
4ae388
+	return 1;
4ae388
+}
4ae388
+
4ae388
 static void
4ae388
 sysfs_set_rport_tmo(struct multipath *mpp, struct path *pp)
4ae388
 {
4ae388
Index: multipath-tools-130222/libmultipath/discovery.h
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/discovery.h
4ae388
+++ multipath-tools-130222/libmultipath/discovery.h
4ae388
@@ -38,6 +38,8 @@ int store_pathinfo (vector pathvec, vect
4ae388
 		    struct path **pp_ptr);
4ae388
 int sysfs_set_scsi_tmo (struct multipath *mpp);
4ae388
 int sysfs_get_timeout(struct path *pp, unsigned int *timeout);
4ae388
+int sysfs_get_host_pci_name(struct path *pp, char *pci_name);
4ae388
+int sysfs_get_iscsi_ip_address(struct path *pp, char *ip_address);
4ae388
 
4ae388
 /*
4ae388
  * discovery bitmask
4ae388
Index: multipath-tools-130222/libmultipath/structs.c
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/structs.c
4ae388
+++ multipath-tools-130222/libmultipath/structs.c
4ae388
@@ -18,6 +18,70 @@
4ae388
 #include "blacklist.h"
4ae388
 #include "prio.h"
4ae388
 
4ae388
+struct adapter_group *
4ae388
+alloc_adaptergroup(void)
4ae388
+{
4ae388
+	struct adapter_group *agp;
4ae388
+
4ae388
+	agp = (struct adapter_group *)MALLOC(sizeof(struct adapter_group));
4ae388
+
4ae388
+	if (!agp)
4ae388
+		return NULL;
4ae388
+
4ae388
+	agp->host_groups = vector_alloc();
4ae388
+	if (!agp->host_groups) {
4ae388
+		FREE(agp);
4ae388
+		agp = NULL;
4ae388
+	}
4ae388
+	return agp;
4ae388
+}
4ae388
+
4ae388
+void free_adaptergroup(vector adapters)
4ae388
+{
4ae388
+	int i;
4ae388
+	struct adapter_group *agp;
4ae388
+
4ae388
+	vector_foreach_slot(adapters, agp, i) {
4ae388
+		free_hostgroup(agp->host_groups);
4ae388
+		FREE(agp);
4ae388
+	}
4ae388
+	vector_free(adapters);
4ae388
+}
4ae388
+
4ae388
+void free_hostgroup(vector hostgroups)
4ae388
+{
4ae388
+	int i;
4ae388
+	struct host_group *hgp;
4ae388
+
4ae388
+	if (!hostgroups)
4ae388
+		return;
4ae388
+
4ae388
+	vector_foreach_slot(hostgroups, hgp, i) {
4ae388
+		vector_free(hgp->paths);
4ae388
+		FREE(hgp);
4ae388
+	}
4ae388
+	vector_free(hostgroups);
4ae388
+}
4ae388
+
4ae388
+struct host_group *
4ae388
+alloc_hostgroup(void)
4ae388
+{
4ae388
+	struct host_group *hgp;
4ae388
+
4ae388
+	hgp = (struct host_group *)MALLOC(sizeof(struct host_group));
4ae388
+
4ae388
+	if (!hgp)
4ae388
+		return NULL;
4ae388
+
4ae388
+	hgp->paths = vector_alloc();
4ae388
+
4ae388
+	if (!hgp->paths) {
4ae388
+		FREE(hgp);
4ae388
+		hgp = NULL;
4ae388
+	}
4ae388
+	return hgp;
4ae388
+}
4ae388
+
4ae388
 struct path *
4ae388
 alloc_path (void)
4ae388
 {
4ae388
@@ -242,6 +306,26 @@ store_pathgroup (vector pgvec, struct pa
4ae388
 	return 0;
4ae388
 }
4ae388
 
4ae388
+int
4ae388
+store_hostgroup(vector hostgroupvec, struct host_group * hgp)
4ae388
+{
4ae388
+	if (!vector_alloc_slot(hostgroupvec))
4ae388
+		return 1;
4ae388
+
4ae388
+	vector_set_slot(hostgroupvec, hgp);
4ae388
+	return 0;
4ae388
+}
4ae388
+
4ae388
+int
4ae388
+store_adaptergroup(vector adapters, struct adapter_group * agp)
4ae388
+{
4ae388
+	if (!vector_alloc_slot(adapters))
4ae388
+		return 1;
4ae388
+
4ae388
+	vector_set_slot(adapters, agp);
4ae388
+	return 0;
4ae388
+}
4ae388
+
4ae388
 struct multipath *
4ae388
 find_mp_by_minor (vector mpvec, int minor)
4ae388
 {
4ae388
Index: multipath-tools-130222/libmultipath/structs.h
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/structs.h
4ae388
+++ multipath-tools-130222/libmultipath/structs.h
4ae388
@@ -15,7 +15,8 @@
4ae388
 #define BLK_DEV_SIZE		33
4ae388
 #define PATH_SIZE		512
4ae388
 #define NAME_SIZE		512
4ae388
-
4ae388
+#define HOST_NAME_LEN		8
4ae388
+#define SLOT_NAME_SIZE		40
4ae388
 
4ae388
 #define SCSI_VENDOR_SIZE	9
4ae388
 #define SCSI_PRODUCT_SIZE	17
4ae388
@@ -251,6 +252,20 @@ struct pathgroup {
4ae388
 	char * selector;
4ae388
 };
4ae388
 
4ae388
+struct adapter_group {
4ae388
+	char adapter_name[SLOT_NAME_SIZE];
4ae388
+	struct pathgroup *pgp;
4ae388
+	int num_hosts;
4ae388
+	vector host_groups;
4ae388
+	int next_host_index;
4ae388
+};
4ae388
+
4ae388
+struct host_group {
4ae388
+	int host_no;
4ae388
+	int num_paths;
4ae388
+	vector paths;
4ae388
+};
4ae388
+
4ae388
 struct path * alloc_path (void);
4ae388
 struct pathgroup * alloc_pathgroup (void);
4ae388
 struct multipath * alloc_multipath (void);
4ae388
@@ -263,6 +278,14 @@ void free_multipath_attributes (struct m
4ae388
 void drop_multipath (vector mpvec, char * wwid, enum free_path_mode free_paths);
4ae388
 void free_multipathvec (vector mpvec, enum free_path_mode free_paths);
4ae388
 
4ae388
+struct adapter_group * alloc_adaptergroup(void);
4ae388
+struct host_group * alloc_hostgroup(void);
4ae388
+void free_adaptergroup(vector adapters);
4ae388
+void free_hostgroup(vector hostgroups);
4ae388
+
4ae388
+int store_adaptergroup(vector adapters, struct adapter_group *agp);
4ae388
+int store_hostgroup(vector hostgroupvec, struct host_group *hgp);
4ae388
+
4ae388
 int store_path (vector pathvec, struct path * pp);
4ae388
 int store_pathgroup (vector pgvec, struct pathgroup * pgp);
4ae388