Blame libvendor/osm_pkt_randomizer.c

Packit 13e616
/*
Packit 13e616
 * Copyright (c) 2004-2008 Voltaire, Inc. All rights reserved.
Packit 13e616
 * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
Packit 13e616
 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
Packit 13e616
 *
Packit 13e616
 * This software is available to you under a choice of one of two
Packit 13e616
 * licenses.  You may choose to be licensed under the terms of the GNU
Packit 13e616
 * General Public License (GPL) Version 2, available from the file
Packit 13e616
 * COPYING in the main directory of this source tree, or the
Packit 13e616
 * OpenIB.org BSD license below:
Packit 13e616
 *
Packit 13e616
 *     Redistribution and use in source and binary forms, with or
Packit 13e616
 *     without modification, are permitted provided that the following
Packit 13e616
 *     conditions are met:
Packit 13e616
 *
Packit 13e616
 *      - Redistributions of source code must retain the above
Packit 13e616
 *        copyright notice, this list of conditions and the following
Packit 13e616
 *        disclaimer.
Packit 13e616
 *
Packit 13e616
 *      - Redistributions in binary form must reproduce the above
Packit 13e616
 *        copyright notice, this list of conditions and the following
Packit 13e616
 *        disclaimer in the documentation and/or other materials
Packit 13e616
 *        provided with the distribution.
Packit 13e616
 *
Packit 13e616
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 13e616
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 13e616
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit 13e616
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
Packit 13e616
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit 13e616
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit 13e616
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit 13e616
 * SOFTWARE.
Packit 13e616
 *
Packit 13e616
 */
Packit 13e616
Packit 13e616
/*
Packit 13e616
 * Abstract:
Packit 13e616
 *    Implementation of osm_pkt_randomizer_t.
Packit 13e616
 *
Packit 13e616
 */
Packit 13e616
Packit 13e616
#if HAVE_CONFIG_H
Packit 13e616
#  include <config.h>
Packit 13e616
#endif				/* HAVE_CONFIG_H */
Packit 13e616
Packit 13e616
#include <vendor/osm_pkt_randomizer.h>
Packit 13e616
#include <stdlib.h>
Packit 13e616
#include <string.h>
Packit 13e616
Packit 13e616
#ifndef __WIN__
Packit 13e616
#include <sys/time.h>
Packit 13e616
#include <unistd.h>
Packit 13e616
#endif
Packit 13e616
Packit 13e616
/**********************************************************************
Packit 13e616
 * Return TRUE if the path is in a fault path, and FALSE otherwise.
Packit 13e616
 * By in a fault path the meaning is that there is a path in the fault
Packit 13e616
 * paths that the given path includes it.
Packit 13e616
 * E.g: if there is a fault path: 0,1,4
Packit 13e616
 * For the given path: 0,1,4,7 the return value will be TRUE, also for
Packit 13e616
 * the given path: 0,1,4 the return value will be TRUE, but for
Packit 13e616
 * the given paths: 0,1 or 0,3,1,4 - the return value will be FALSE.
Packit 13e616
 **********************************************************************/
Packit 13e616
boolean_t
Packit 13e616
__osm_pkt_randomizer_is_path_in_fault_paths(IN osm_log_t * p_log,
Packit 13e616
					    IN osm_dr_path_t * p_dr_path,
Packit 13e616
					    IN osm_pkt_randomizer_t *
Packit 13e616
					    p_pkt_rand)
Packit 13e616
{
Packit 13e616
	boolean_t res = FALSE, found_path;
Packit 13e616
	osm_dr_path_t *p_found_dr_path;
Packit 13e616
	uint8_t ind1, ind2;
Packit 13e616
Packit 13e616
	OSM_LOG_ENTER(p_log);
Packit 13e616
Packit 13e616
	for (ind1 = 0; ind1 < p_pkt_rand->num_paths_initialized; ind1++) {
Packit 13e616
		found_path = TRUE;
Packit 13e616
		p_found_dr_path = &(p_pkt_rand->fault_dr_paths[ind1]);
Packit 13e616
		/* if the hop count of the found path is greater than the
Packit 13e616
		   hop count of the input path - then it is not part of it.
Packit 13e616
		   Check the next path. */
Packit 13e616
		if (p_found_dr_path->hop_count > p_dr_path->hop_count)
Packit 13e616
			continue;
Packit 13e616
Packit 13e616
		/* go over all the ports in the found path and see if they match
Packit 13e616
		   the ports in the input path */
Packit 13e616
		for (ind2 = 0; ind2 <= p_found_dr_path->hop_count; ind2++)
Packit 13e616
			if (p_found_dr_path->path[ind2] !=
Packit 13e616
			    p_dr_path->path[ind2])
Packit 13e616
				found_path = FALSE;
Packit 13e616
Packit 13e616
		/* If found_path is TRUE  then there is a full match of the path */
Packit 13e616
		if (found_path == TRUE) {
Packit 13e616
			OSM_LOG(p_log, OSM_LOG_VERBOSE,
Packit 13e616
				"Given path is in a fault path\n");
Packit 13e616
			res = TRUE;
Packit 13e616
			break;
Packit 13e616
		}
Packit 13e616
	}
Packit 13e616
Packit 13e616
	OSM_LOG_EXIT(p_log);
Packit 13e616
	return res;
Packit 13e616
}
Packit 13e616
Packit 13e616
/**********************************************************************
Packit 13e616
 * For a given dr_path - return TRUE if the path should be dropped,
Packit 13e616
 * return FALSE otherwise.
Packit 13e616
 * The check uses random criteria in order to determine whether or not
Packit 13e616
 * the path should be dropped.
Packit 13e616
 * First - if not all paths are initialized, it randomally chooses if
Packit 13e616
 * to use this path as a fault path or not.
Packit 13e616
 * Second - if the path is in the fault paths (meaning - it is equal
Packit 13e616
 * to or includes one of the fault paths) - then it randomally chooses
Packit 13e616
 * if to drop it or not.
Packit 13e616
 **********************************************************************/
Packit 13e616
boolean_t
Packit 13e616
__osm_pkt_randomizer_process_path(IN osm_log_t * p_log,
Packit 13e616
				  IN osm_pkt_randomizer_t * p_pkt_rand,
Packit 13e616
				  IN osm_dr_path_t * p_dr_path)
Packit 13e616
{
Packit 13e616
	boolean_t res = FALSE;
Packit 13e616
	static boolean_t rand_value_init = FALSE;
Packit 13e616
	static int rand_value;
Packit 13e616
	boolean_t in_fault_paths;
Packit 13e616
	uint8_t i;
Packit 13e616
	char buf[BUF_SIZE];
Packit 13e616
	char line[BUF_SIZE];
Packit 13e616
Packit 13e616
	OSM_LOG_ENTER(p_log);
Packit 13e616
Packit 13e616
	if (rand_value_init == FALSE) {
Packit 13e616
		int seed;
Packit 13e616
#ifdef __WIN__
Packit 13e616
		SYSTEMTIME st;
Packit 13e616
#else
Packit 13e616
		struct timeval tv;
Packit 13e616
		struct timezone tz;
Packit 13e616
#endif				/*  __WIN__ */
Packit 13e616
Packit 13e616
		/* initiate the rand_value according to timeofday */
Packit 13e616
		rand_value_init = TRUE;
Packit 13e616
Packit 13e616
#ifdef __WIN__
Packit 13e616
		GetLocalTime(&st);
Packit 13e616
		seed = st.wMilliseconds;
Packit 13e616
#else
Packit 13e616
		gettimeofday(&tv, &tz;;
Packit 13e616
		seed = tv.tv_usec;
Packit 13e616
#endif				/*  __WIN__ */
Packit 13e616
Packit 13e616
		srand(seed);
Packit 13e616
	}
Packit 13e616
Packit 13e616
	/* If the hop_count is 1 - then this is a mad down to our local port - don't drop it */
Packit 13e616
	if (p_dr_path->hop_count <= 1)
Packit 13e616
		goto Exit;
Packit 13e616
Packit 13e616
	rand_value = rand();
Packit 13e616
Packit 13e616
	sprintf(buf, "Path: ");
Packit 13e616
	/* update the dr_path into the buf */
Packit 13e616
	for (i = 0; i <= p_dr_path->hop_count; i++) {
Packit 13e616
		sprintf(line, "[%X]", p_dr_path->path[i]);
Packit 13e616
		strcat(buf, line);
Packit 13e616
	}
Packit 13e616
Packit 13e616
	/* Check if the path given is in one of the fault paths */
Packit 13e616
	in_fault_paths =
Packit 13e616
	    __osm_pkt_randomizer_is_path_in_fault_paths(p_log, p_dr_path,
Packit 13e616
							p_pkt_rand);
Packit 13e616
Packit 13e616
	/* Check if all paths are initialized */
Packit 13e616
	if (p_pkt_rand->num_paths_initialized <
Packit 13e616
	    p_pkt_rand->osm_pkt_num_unstable_links) {
Packit 13e616
		/* Not all packets are initialized. */
Packit 13e616
		if (in_fault_paths == FALSE) {
Packit 13e616
			/* the path is not in the false paths. Check using the rand value
Packit 13e616
			   if to update it there or not. */
Packit 13e616
			if (rand_value %
Packit 13e616
			    (p_pkt_rand->osm_pkt_unstable_link_rate) == 0) {
Packit 13e616
				OSM_LOG(p_log, OSM_LOG_VERBOSE,
Packit 13e616
					"%s added to the fault_dr_paths list\n"
Packit 13e616
					"\t\t\t rand_value:%u, unstable_link_rate:%u \n",
Packit 13e616
					buf, rand_value,
Packit 13e616
					p_pkt_rand->osm_pkt_unstable_link_rate);
Packit 13e616
Packit 13e616
				/* update the path in the fault paths */
Packit 13e616
				memcpy(&
Packit 13e616
				       (p_pkt_rand->
Packit 13e616
					fault_dr_paths[p_pkt_rand->
Packit 13e616
						       num_paths_initialized]),
Packit 13e616
				       p_dr_path, sizeof(osm_dr_path_t));
Packit 13e616
				p_pkt_rand->num_paths_initialized++;
Packit 13e616
				in_fault_paths = TRUE;
Packit 13e616
			}
Packit 13e616
		}
Packit 13e616
	}
Packit 13e616
Packit 13e616
	if (in_fault_paths == FALSE) {
Packit 13e616
		/* If in_fault_paths is FALSE - just ignore the path */
Packit 13e616
		OSM_LOG(p_log, OSM_LOG_VERBOSE, "%s not in fault paths\n", buf);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	/* The path is in the fault paths. Need to choose (randomally if to drop it
Packit 13e616
	   or not. */
Packit 13e616
	rand_value = rand();
Packit 13e616
Packit 13e616
	if (rand_value % (p_pkt_rand->osm_pkt_drop_rate) == 0) {
Packit 13e616
		/* drop the current packet */
Packit 13e616
		res = TRUE;
Packit 13e616
		OSM_LOG(p_log, OSM_LOG_VERBOSE, "Dropping path:%s\n", buf);
Packit 13e616
	}
Packit 13e616
Packit 13e616
Exit:
Packit 13e616
	OSM_LOG_EXIT(p_log);
Packit 13e616
	return res;
Packit 13e616
}
Packit 13e616
Packit 13e616
boolean_t
Packit 13e616
osm_pkt_randomizer_mad_drop(IN osm_log_t * p_log,
Packit 13e616
			    IN osm_pkt_randomizer_t * p_pkt_randomizer,
Packit 13e616
			    IN const ib_mad_t * p_mad)
Packit 13e616
{
Packit 13e616
	const ib_smp_t *p_smp;
Packit 13e616
	boolean_t res = FALSE;
Packit 13e616
	osm_dr_path_t dr_path;
Packit 13e616
Packit 13e616
	OSM_LOG_ENTER(p_log);
Packit 13e616
Packit 13e616
	p_smp = (ib_smp_t *) p_mad;
Packit 13e616
Packit 13e616
	if (p_smp->mgmt_class != IB_MCLASS_SUBN_DIR)
Packit 13e616
		/* This is a lid route mad. Don't drop it */
Packit 13e616
		goto Exit;
Packit 13e616
Packit 13e616
	osm_dr_path_init(&dr_path, p_smp->hop_count, p_smp->initial_path);
Packit 13e616
Packit 13e616
	if (__osm_pkt_randomizer_process_path
Packit 13e616
	    (p_log, p_pkt_randomizer, &dr_path)) {
Packit 13e616
		/* the mad should be dropped o */
Packit 13e616
		OSM_LOG(p_log, OSM_LOG_VERBOSE,
Packit 13e616
			"mad TID: 0x%" PRIx64 " is being dropped\n",
Packit 13e616
			cl_ntoh64(p_smp->trans_id));
Packit 13e616
		res = TRUE;
Packit 13e616
	}
Packit 13e616
Packit 13e616
Exit:
Packit 13e616
	OSM_LOG_EXIT(p_log);
Packit 13e616
	return res;
Packit 13e616
}
Packit 13e616
Packit 13e616
ib_api_status_t
Packit 13e616
osm_pkt_randomizer_init(IN OUT osm_pkt_randomizer_t ** pp_pkt_randomizer,
Packit 13e616
			IN osm_log_t * p_log)
Packit 13e616
{
Packit 13e616
	uint8_t tmp;
Packit 13e616
	ib_api_status_t res = IB_SUCCESS;
Packit 13e616
Packit 13e616
	OSM_LOG_ENTER(p_log);
Packit 13e616
Packit 13e616
	*pp_pkt_randomizer = malloc(sizeof(osm_pkt_randomizer_t));
Packit 13e616
	if (*pp_pkt_randomizer == NULL) {
Packit 13e616
		res = IB_INSUFFICIENT_MEMORY;
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
	memset(*pp_pkt_randomizer, 0, sizeof(osm_pkt_randomizer_t));
Packit 13e616
	(*pp_pkt_randomizer)->num_paths_initialized = 0;
Packit 13e616
Packit 13e616
	tmp = atol(getenv("OSM_PKT_DROP_RATE"));
Packit 13e616
	(*pp_pkt_randomizer)->osm_pkt_drop_rate = tmp;
Packit 13e616
Packit 13e616
	if (getenv("OSM_PKT_NUM_UNSTABLE_LINKS") != NULL
Packit 13e616
	    && (tmp = atol(getenv("OSM_PKT_NUM_UNSTABLE_LINKS"))) > 0)
Packit 13e616
		(*pp_pkt_randomizer)->osm_pkt_num_unstable_links = tmp;
Packit 13e616
	else
Packit 13e616
		(*pp_pkt_randomizer)->osm_pkt_num_unstable_links = 1;
Packit 13e616
Packit 13e616
	if (getenv("OSM_PKT_UNSTABLE_LINK_RATE") != NULL
Packit 13e616
	    && (tmp = atol(getenv("OSM_PKT_UNSTABLE_LINK_RATE"))) > 0)
Packit 13e616
		(*pp_pkt_randomizer)->osm_pkt_unstable_link_rate = tmp;
Packit 13e616
	else
Packit 13e616
		(*pp_pkt_randomizer)->osm_pkt_unstable_link_rate = 20;
Packit 13e616
Packit 13e616
	OSM_LOG(p_log, OSM_LOG_VERBOSE, "Using OSM_PKT_DROP_RATE=%u \n"
Packit 13e616
		"\t\t\t\t OSM_PKT_NUM_UNSTABLE_LINKS=%u \n"
Packit 13e616
		"\t\t\t\t OSM_PKT_UNSTABLE_LINK_RATE=%u \n",
Packit 13e616
		(*pp_pkt_randomizer)->osm_pkt_drop_rate,
Packit 13e616
		(*pp_pkt_randomizer)->osm_pkt_num_unstable_links,
Packit 13e616
		(*pp_pkt_randomizer)->osm_pkt_unstable_link_rate);
Packit 13e616
Packit 13e616
	/* allocate the fault_dr_paths variable */
Packit 13e616
	/* It is the number of the paths that will be saved as fault = osm_pkt_num_unstable_links */
Packit 13e616
	(*pp_pkt_randomizer)->fault_dr_paths = malloc(sizeof(osm_dr_path_t) *
Packit 13e616
						      (*pp_pkt_randomizer)->
Packit 13e616
						      osm_pkt_num_unstable_links);
Packit 13e616
	if ((*pp_pkt_randomizer)->fault_dr_paths == NULL) {
Packit 13e616
		res = IB_INSUFFICIENT_MEMORY;
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	memset((*pp_pkt_randomizer)->fault_dr_paths, 0,
Packit 13e616
	       sizeof(osm_dr_path_t) *
Packit 13e616
	       (*pp_pkt_randomizer)->osm_pkt_num_unstable_links);
Packit 13e616
Packit 13e616
Exit:
Packit 13e616
	OSM_LOG_EXIT(p_log);
Packit 13e616
	return (res);
Packit 13e616
}
Packit 13e616
Packit 13e616
void
Packit 13e616
osm_pkt_randomizer_destroy(IN OUT osm_pkt_randomizer_t ** pp_pkt_randomizer,
Packit 13e616
			   IN osm_log_t * p_log)
Packit 13e616
{
Packit 13e616
	OSM_LOG_ENTER(p_log);
Packit 13e616
Packit 13e616
	if (*pp_pkt_randomizer != NULL) {
Packit 13e616
		free((*pp_pkt_randomizer)->fault_dr_paths);
Packit 13e616
		free(*pp_pkt_randomizer);
Packit 13e616
	}
Packit 13e616
	OSM_LOG_EXIT(p_log);
Packit 13e616
}