19e39c5baSBill Taylor /*
29e39c5baSBill Taylor  * CDDL HEADER START
39e39c5baSBill Taylor  *
49e39c5baSBill Taylor  * The contents of this file are subject to the terms of the
59e39c5baSBill Taylor  * Common Development and Distribution License (the "License").
69e39c5baSBill Taylor  * You may not use this file except in compliance with the License.
79e39c5baSBill Taylor  *
89e39c5baSBill Taylor  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
99e39c5baSBill Taylor  * or http://www.opensolaris.org/os/licensing.
109e39c5baSBill Taylor  * See the License for the specific language governing permissions
119e39c5baSBill Taylor  * and limitations under the License.
129e39c5baSBill Taylor  *
139e39c5baSBill Taylor  * When distributing Covered Code, include this CDDL HEADER in each
149e39c5baSBill Taylor  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
159e39c5baSBill Taylor  * If applicable, add the following below this CDDL HEADER, with the
169e39c5baSBill Taylor  * fields enclosed by brackets "[]" replaced with your own identifying
179e39c5baSBill Taylor  * information: Portions Copyright [yyyy] [name of copyright owner]
189e39c5baSBill Taylor  *
199e39c5baSBill Taylor  * CDDL HEADER END
209e39c5baSBill Taylor  */
219e39c5baSBill Taylor 
229e39c5baSBill Taylor /*
239e39c5baSBill Taylor  * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved.
249e39c5baSBill Taylor  */
259e39c5baSBill Taylor 
269e39c5baSBill Taylor /*
279e39c5baSBill Taylor  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
289e39c5baSBill Taylor  * Use is subject to license terms.
299e39c5baSBill Taylor  */
309e39c5baSBill Taylor 
319e39c5baSBill Taylor /*
329e39c5baSBill Taylor  *
339e39c5baSBill Taylor  * HEADER: dapl_osd.h
349e39c5baSBill Taylor  *
359e39c5baSBill Taylor  * PURPOSE: Operating System Dependent layer
369e39c5baSBill Taylor  * Description:
379e39c5baSBill Taylor  *	Provide OS dependent data structures & functions with
389e39c5baSBill Taylor  *	a canonical DAPL interface. Designed to be portable
399e39c5baSBill Taylor  *	and hide OS specific quirks of common functions.
409e39c5baSBill Taylor  *
419e39c5baSBill Taylor  * $Id: dapl_osd.h,v 1.38 2003/08/20 14:08:57 sjs2 Exp $
429e39c5baSBill Taylor  */
439e39c5baSBill Taylor 
449e39c5baSBill Taylor #ifndef _DAPL_OSD_H_
459e39c5baSBill Taylor #define	_DAPL_OSD_H_
469e39c5baSBill Taylor 
479e39c5baSBill Taylor #ifdef __cplusplus
489e39c5baSBill Taylor extern "C" {
499e39c5baSBill Taylor #endif
509e39c5baSBill Taylor 
519e39c5baSBill Taylor /*
529e39c5baSBill Taylor  * <assert.h> keys off of NDEBUG
539e39c5baSBill Taylor  */
549e39c5baSBill Taylor #ifdef	DAPL_DBG
559e39c5baSBill Taylor #undef	NDEBUG
569e39c5baSBill Taylor #else
579e39c5baSBill Taylor #define	NDEBUG
589e39c5baSBill Taylor #endif
599e39c5baSBill Taylor 
609e39c5baSBill Taylor #include <dat/udat.h>
619e39c5baSBill Taylor #include <assert.h>
629e39c5baSBill Taylor #include <errno.h>
639e39c5baSBill Taylor #include <pthread.h>
649e39c5baSBill Taylor #include <semaphore.h>
659e39c5baSBill Taylor #include <stdio.h>
669e39c5baSBill Taylor #include <stdlib.h>
679e39c5baSBill Taylor #include <string.h>
689e39c5baSBill Taylor #include <stdarg.h>
699e39c5baSBill Taylor #include <time.h>
709e39c5baSBill Taylor #include <syslog.h>
719e39c5baSBill Taylor #include <netdb.h>
729e39c5baSBill Taylor #include <atomic.h>
739e39c5baSBill Taylor #include "dapl_debug.h"
749e39c5baSBill Taylor 
759e39c5baSBill Taylor /*
769e39c5baSBill Taylor  * networking related headers
779e39c5baSBill Taylor  */
789e39c5baSBill Taylor #include <unistd.h>
799e39c5baSBill Taylor #include <fcntl.h>
809e39c5baSBill Taylor #include <sys/types.h>
819e39c5baSBill Taylor #include <sys/socket.h>
829e39c5baSBill Taylor #include <ctype.h>
839e39c5baSBill Taylor #include <arpa/inet.h>
849e39c5baSBill Taylor 
859e39c5baSBill Taylor #ifndef _INLINE_
869e39c5baSBill Taylor #define	_INLINE_
879e39c5baSBill Taylor #endif /* _INLINE_ */
889e39c5baSBill Taylor 
899e39c5baSBill Taylor /*
909e39c5baSBill Taylor  * initialization function
919e39c5baSBill Taylor  */
929e39c5baSBill Taylor void dapl_os_init(void);
939e39c5baSBill Taylor 
949e39c5baSBill Taylor #define	dapl_os_panic(args) 			\
959e39c5baSBill Taylor {					\
969e39c5baSBill Taylor 	fprintf(stderr, "PANIC in %s:%i:\n", __FILE__, __LINE__); \
979e39c5baSBill Taylor 	fprintf(stderr, args);			\
989e39c5baSBill Taylor 	exit(1);				\
999e39c5baSBill Taylor }
1009e39c5baSBill Taylor 
1019e39c5baSBill Taylor int dapl_os_get_env_bool(
1029e39c5baSBill Taylor 	char		*env_str);
1039e39c5baSBill Taylor 
1049e39c5baSBill Taylor int dapl_os_get_env_val(
1059e39c5baSBill Taylor 	char		*env_str,
1069e39c5baSBill Taylor 	int		def_val);
1079e39c5baSBill Taylor 
1089e39c5baSBill Taylor /*
1099e39c5baSBill Taylor  * Atomic operations
1109e39c5baSBill Taylor  */
1119e39c5baSBill Taylor typedef volatile DAT_COUNT DAPL_ATOMIC;
1129e39c5baSBill Taylor 
1139e39c5baSBill Taylor /*
1149e39c5baSBill Taylor  * dapl_os_atomic_inc
1159e39c5baSBill Taylor  *
1169e39c5baSBill Taylor  * get the current value of '*v', and then increment it.
1179e39c5baSBill Taylor  *
1189e39c5baSBill Taylor  * This is equivalent to an IB atomic fetch and add of 1,
1199e39c5baSBill Taylor  * except that a DAT_COUNT might be 32 bits, rather than 64
1209e39c5baSBill Taylor  * and it occurs in local memory.
1219e39c5baSBill Taylor  *
122*fb4cdc19SToomas Soome  * void dapl_os_atomic_inc(INOUT	DAPL_ATOMIC *v)
1239e39c5baSBill Taylor  */
124*fb4cdc19SToomas Soome #define	dapl_os_atomic_inc(v)	atomic_add_32((uint32_t *)(v), 1)
1259e39c5baSBill Taylor 
1269e39c5baSBill Taylor /*
1279e39c5baSBill Taylor  * dapl_os_atomic_dec
1289e39c5baSBill Taylor  *
1299e39c5baSBill Taylor  * decrement the current value of '*v'. No return value is required.
1309e39c5baSBill Taylor  *
131*fb4cdc19SToomas Soome  * void dapl_os_atomic_dec(INOUT	DAPL_ATOMIC *v)
1329e39c5baSBill Taylor  */
1339e39c5baSBill Taylor #define	dapl_os_atomic_dec(v)	assert(*v != 0);		\
134*fb4cdc19SToomas Soome 				atomic_add_32((uint32_t *)(v), -1)
1359e39c5baSBill Taylor 
1369e39c5baSBill Taylor /*
1379e39c5baSBill Taylor  * dapl_os_atomic_assign
1389e39c5baSBill Taylor  *
1399e39c5baSBill Taylor  * assign 'new_value' to '*v' if the current value
1409e39c5baSBill Taylor  * matches the provided 'match_value'.
1419e39c5baSBill Taylor  *
1429e39c5baSBill Taylor  * Make no assignment if there is no match.
1439e39c5baSBill Taylor  *
1449e39c5baSBill Taylor  * Return the current value in any case.
1459e39c5baSBill Taylor  *
1469e39c5baSBill Taylor  * This matches the IBTA atomic operation compare & swap
1479e39c5baSBill Taylor  * except that it is for local memory and a DAT_COUNT may
1489e39c5baSBill Taylor  * be only 32 bits, rather than 64.
1499e39c5baSBill Taylor  *
1509e39c5baSBill Taylor  * DAT_COUNT dapl_os_atomic_assign(INOUT DAPL_ATOMIC *v,
1519e39c5baSBill Taylor  *	IN DAT_COUNT match_value, IN DAT_COUNT new_value)
1529e39c5baSBill Taylor  */
1539e39c5baSBill Taylor #define	dapl_os_atomic_assign(v, match_value, new_value)		\
1549e39c5baSBill Taylor 		atomic_cas_32((uint32_t *)(v), (uint32_t)(match_value),	\
1559e39c5baSBill Taylor 		    (uint32_t)(new_value))
1569e39c5baSBill Taylor 
1579e39c5baSBill Taylor /*
1589e39c5baSBill Taylor  * Thread Functions
1599e39c5baSBill Taylor  */
1609e39c5baSBill Taylor typedef pthread_t		DAPL_OS_THREAD;
1619e39c5baSBill Taylor 
1629e39c5baSBill Taylor DAT_RETURN
1639e39c5baSBill Taylor dapl_os_thread_create(
1649e39c5baSBill Taylor 	IN  void			(*func)	(void *),
1659e39c5baSBill Taylor 	IN  void			*data,
1669e39c5baSBill Taylor 	OUT DAPL_OS_THREAD		*thread_id);
1679e39c5baSBill Taylor 
1689e39c5baSBill Taylor 
1699e39c5baSBill Taylor /*
1709e39c5baSBill Taylor  * Lock Functions
1719e39c5baSBill Taylor  */
1729e39c5baSBill Taylor 
1739e39c5baSBill Taylor typedef pthread_mutex_t 	DAPL_OS_LOCK;
1749e39c5baSBill Taylor 
1759e39c5baSBill Taylor /*
1769e39c5baSBill Taylor  * DAT_RETURN dapl_os_lock_init(IN DAPL_OS_LOCK *m)
1779e39c5baSBill Taylor  */
1789e39c5baSBill Taylor #define	dapl_os_lock_init(m)	(void)					\
1799e39c5baSBill Taylor 				((0 == pthread_mutex_init((m), NULL)) ?	\
1809e39c5baSBill Taylor 					DAT_SUCCESS :			\
1819e39c5baSBill Taylor 					(DAT_CLASS_ERROR | DAT_INTERNAL_ERROR))
1829e39c5baSBill Taylor 
1839e39c5baSBill Taylor /* DAT_RETURN dapl_os_lock(IN DAPL_OS_LOCK *m) */
1849e39c5baSBill Taylor #define	dapl_os_lock(m)		((DAT_RETURN)(				\
1859e39c5baSBill Taylor 				(0 == pthread_mutex_lock((m))) ?	\
1869e39c5baSBill Taylor 					DAT_SUCCESS :			\
1879e39c5baSBill Taylor 					(DAT_CLASS_ERROR | DAT_INTERNAL_ERROR)))
1889e39c5baSBill Taylor 
1899e39c5baSBill Taylor /* DAT_RETURN dapl_os_unlock(IN DAPL_OS_LOCK *m) */
1909e39c5baSBill Taylor #define	dapl_os_unlock(m)	((DAT_RETURN)(				\
1919e39c5baSBill Taylor 				(0 == pthread_mutex_unlock((m))) ?	\
1929e39c5baSBill Taylor 					DAT_SUCCESS :			\
1939e39c5baSBill Taylor 					(DAT_CLASS_ERROR | DAT_INTERNAL_ERROR)))
1949e39c5baSBill Taylor 
1959e39c5baSBill Taylor /* DAT_RETURN dapl_os_lock_destroy(IN DAPL_OS_LOCK *m) */
1969e39c5baSBill Taylor #define	dapl_os_lock_destroy(m)	((DAT_RETURN)(				\
1979e39c5baSBill Taylor 				(0 == pthread_mutex_destroy((m))) ?	\
1989e39c5baSBill Taylor 					DAT_SUCCESS :			\
1999e39c5baSBill Taylor 					(DAT_CLASS_ERROR | DAT_INTERNAL_ERROR)))
2009e39c5baSBill Taylor /*
2019e39c5baSBill Taylor  * Wait Objects
2029e39c5baSBill Taylor  */
2039e39c5baSBill Taylor 
2049e39c5baSBill Taylor /*
2059e39c5baSBill Taylor  * The wait object invariant: Presuming a call to dapl_os_wait_object_wait
2069e39c5baSBill Taylor  * occurs at some point, there will be at least one wakeup after each call
2079e39c5baSBill Taylor  * to dapl_os_wait_object_signal.  I.e. Signals are not ignored, though
2089e39c5baSBill Taylor  * they may be coallesced.
2099e39c5baSBill Taylor  */
2109e39c5baSBill Taylor 
2119e39c5baSBill Taylor typedef struct
2129e39c5baSBill Taylor {
2139e39c5baSBill Taylor     DAT_BOOLEAN		signaled;
2149e39c5baSBill Taylor     pthread_cond_t	cv;
2159e39c5baSBill Taylor     pthread_mutex_t	lock;
2169e39c5baSBill Taylor } DAPL_OS_WAIT_OBJECT;
2179e39c5baSBill Taylor 
2189e39c5baSBill Taylor /* function prototypes */
2199e39c5baSBill Taylor DAT_RETURN
2209e39c5baSBill Taylor dapl_os_wait_object_init(
2219e39c5baSBill Taylor     IN DAPL_OS_WAIT_OBJECT *wait_obj);
2229e39c5baSBill Taylor 
2239e39c5baSBill Taylor DAT_RETURN
2249e39c5baSBill Taylor dapl_os_wait_object_wait(
2259e39c5baSBill Taylor     IN	DAPL_OS_WAIT_OBJECT *wait_obj,
2269e39c5baSBill Taylor     IN  DAT_TIMEOUT timeout_val);
2279e39c5baSBill Taylor 
2289e39c5baSBill Taylor DAT_RETURN
2299e39c5baSBill Taylor dapl_os_wait_object_wakeup(
2309e39c5baSBill Taylor     IN	DAPL_OS_WAIT_OBJECT *wait_obj);
2319e39c5baSBill Taylor 
2329e39c5baSBill Taylor DAT_RETURN
2339e39c5baSBill Taylor dapl_os_wait_object_destroy(
2349e39c5baSBill Taylor     IN	DAPL_OS_WAIT_OBJECT *wait_obj);
2359e39c5baSBill Taylor 
2369e39c5baSBill Taylor /*
2379e39c5baSBill Taylor  * Memory Functions
2389e39c5baSBill Taylor  */
2399e39c5baSBill Taylor 
2409e39c5baSBill Taylor /* void *dapl_os_alloc(int size) */
2419e39c5baSBill Taylor #define	dapl_os_alloc(size)	malloc((size))
2429e39c5baSBill Taylor 
2439e39c5baSBill Taylor /* void *dapl_os_realloc(void *ptr, int size) */
2449e39c5baSBill Taylor #define	dapl_os_realloc(ptr, size) realloc((ptr), (size))
2459e39c5baSBill Taylor 
2469e39c5baSBill Taylor /* void dapl_os_free(void *ptr, int size) */
2479e39c5baSBill Taylor #define	dapl_os_free(ptr, size)	free((ptr))
2489e39c5baSBill Taylor 
2499e39c5baSBill Taylor /* void * dapl_os_memzero(void *loc, int size) */
2509e39c5baSBill Taylor #define	dapl_os_memzero(loc, size)	memset((loc), 0, (size))
2519e39c5baSBill Taylor 
2529e39c5baSBill Taylor /* void * dapl_os_memcpy(void *dest, const void *src, int len) */
2539e39c5baSBill Taylor #define	dapl_os_memcpy(dest, src, len)	memcpy((dest), (src), (len))
2549e39c5baSBill Taylor 
2559e39c5baSBill Taylor /* int dapl_os_memcmp(const void *mem1, const void *mem2, int len) */
2569e39c5baSBill Taylor #define	dapl_os_memcmp(mem1, mem2, len)	memcmp((mem1), (mem2), (len))
2579e39c5baSBill Taylor 
2589e39c5baSBill Taylor /*
2599e39c5baSBill Taylor  * String Functions
2609e39c5baSBill Taylor  */
2619e39c5baSBill Taylor 
2629e39c5baSBill Taylor /* unsigned int dapl_os_strlen(const char *str) */
2639e39c5baSBill Taylor #define	dapl_os_strlen(str)	strlen((str))
2649e39c5baSBill Taylor /* char * dapl_os_strdup(const char *str) */
2659e39c5baSBill Taylor #define	dapl_os_strdup(str)	strdup((str))
2669e39c5baSBill Taylor /* char *strcpy(char *dest, char *src) */
2679e39c5baSBill Taylor #define	dapl_os_strcpy(dest, src) 	strcpy((dest), (src))
2689e39c5baSBill Taylor /* char *strncpy(char *s1, const char *s2, size_t n) */
2699e39c5baSBill Taylor #define	dapl_os_strncpy(dest, src, len) strncpy((dest), (src), (len))
2709e39c5baSBill Taylor /* char *strcat(char *dest, char *src) */
2719e39c5baSBill Taylor #define	dapl_os_strcat(dest, src) 	strcat((dest), (src))
2729e39c5baSBill Taylor 
2739e39c5baSBill Taylor /*
2749e39c5baSBill Taylor  * Timer Functions
2759e39c5baSBill Taylor  */
2769e39c5baSBill Taylor 
2779e39c5baSBill Taylor typedef DAT_UINT64		DAPL_OS_TIMEVAL;
2789e39c5baSBill Taylor 
2799e39c5baSBill Taylor 
2809e39c5baSBill Taylor typedef unsigned long long int	DAPL_OS_TICKS;
2819e39c5baSBill Taylor 
2829e39c5baSBill Taylor /* function prototypes */
2839e39c5baSBill Taylor 
2849e39c5baSBill Taylor /*
2859e39c5baSBill Taylor  * Sleep for the number of micro seconds specified by the invoking
2869e39c5baSBill Taylor  * function
2879e39c5baSBill Taylor  *
2889e39c5baSBill Taylor  * void dapl_os_sleep_usec(int sleep_time)
2899e39c5baSBill Taylor  */
2909e39c5baSBill Taylor #define	dapl_os_sleep_usec(sleep_time)	{				\
2919e39c5baSBill Taylor 		struct timespec sleep_spec;				\
2929e39c5baSBill Taylor 		sleep_spec.tv_sec = (sleep_time) / 100000;		\
2939e39c5baSBill Taylor 		sleep_spec.tv_nsec = (sleep_time) % 100000 * 1000;	\
2949e39c5baSBill Taylor 		nanosleep(&sleep_spec, NULL);				\
2959e39c5baSBill Taylor 		}
2969e39c5baSBill Taylor 
2979e39c5baSBill Taylor DAT_RETURN dapl_os_get_time(DAPL_OS_TIMEVAL *);
2989e39c5baSBill Taylor 
2999e39c5baSBill Taylor /*
3009e39c5baSBill Taylor  *
3019e39c5baSBill Taylor  * Name Service Helper functions
3029e39c5baSBill Taylor  *
3039e39c5baSBill Taylor  */
3049e39c5baSBill Taylor #if defined(IBHOSTS_NAMING)
3059e39c5baSBill Taylor #define	dapls_osd_getaddrinfo(name, addr_ptr)		\
3069e39c5baSBill Taylor 				getaddrinfo((name), NULL, NULL, (addr_ptr))
3079e39c5baSBill Taylor #define	dapls_osd_freeaddrinfo(addr) freeaddrinfo((addr))
3089e39c5baSBill Taylor 
3099e39c5baSBill Taylor #endif /* IBHOSTS_NAMING */
3109e39c5baSBill Taylor 
3119e39c5baSBill Taylor /*
3129e39c5baSBill Taylor  * *printf format helpers. We use the C string constant concatenation
3139e39c5baSBill Taylor  * ability to define 64 bit formats, which unfortunatly are non standard
3149e39c5baSBill Taylor  * in the C compiler world. E.g. %llx for gcc, %I64x for Windows
3159e39c5baSBill Taylor  */
3169e39c5baSBill Taylor #define	F64d   "%lld"
3179e39c5baSBill Taylor #define	F64u   "%llu"
3189e39c5baSBill Taylor #define	F64x   "%llx"
3199e39c5baSBill Taylor #define	F64X   "%llX"
3209e39c5baSBill Taylor 
3219e39c5baSBill Taylor 
3229e39c5baSBill Taylor /*
3239e39c5baSBill Taylor  *  Conversion Functions
3249e39c5baSBill Taylor  */
3259e39c5baSBill Taylor 
3269e39c5baSBill Taylor /* long int dapl_os_strtol(const char *nptr, char **endptr, int base) */
3279e39c5baSBill Taylor #define	dapl_os_strtol(nptr, endptr, base)	strtol((nptr), (endptr), (base))
3289e39c5baSBill Taylor 
3299e39c5baSBill Taylor /*
3309e39c5baSBill Taylor  *  Helper Functions
3319e39c5baSBill Taylor  */
3329e39c5baSBill Taylor 
3339e39c5baSBill Taylor 
3349e39c5baSBill Taylor #define	dapl_os_assert(expression)	assert((expression))
3359e39c5baSBill Taylor #define	dapl_os_printf			printf
3369e39c5baSBill Taylor #define	dapl_os_vprintf(fmt, args)	vprintf((fmt), (args))
3379e39c5baSBill Taylor #define	dapl_os_syslog(fmt, args)	vsyslog(LOG_USER | LOG_DEBUG,	\
3389e39c5baSBill Taylor 						(fmt), (args))
3399e39c5baSBill Taylor #ifdef __cplusplus
3409e39c5baSBill Taylor }
3419e39c5baSBill Taylor #endif
3429e39c5baSBill Taylor 
3439e39c5baSBill Taylor #endif /* _DAPL_OSD_H_ */