dapl_osd.h revision 9517:b4839b0aa7a4
167754Smsmith/*
267754Smsmith * CDDL HEADER START
367754Smsmith *
480062Smsmith * The contents of this file are subject to the terms of the
567754Smsmith * Common Development and Distribution License (the "License").
667754Smsmith * You may not use this file except in compliance with the License.
767754Smsmith *
867754Smsmith * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
967754Smsmith * or http://www.opensolaris.org/os/licensing.
1067754Smsmith * See the License for the specific language governing permissions
1167754Smsmith * and limitations under the License.
1271867Smsmith *
1370243Smsmith * When distributing Covered Code, include this CDDL HEADER in each
1467754Smsmith * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1567754Smsmith * If applicable, add the following below this CDDL HEADER, with the
1667754Smsmith * fields enclosed by brackets "[]" replaced with your own identifying
1767754Smsmith * information: Portions Copyright [yyyy] [name of copyright owner]
1867754Smsmith *
1967754Smsmith * CDDL HEADER END
2067754Smsmith */
2167754Smsmith
2267754Smsmith/*
2367754Smsmith * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved.
2467754Smsmith */
2567754Smsmith
2667754Smsmith/*
2767754Smsmith * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
2867754Smsmith * Use is subject to license terms.
2967754Smsmith */
3067754Smsmith
3167754Smsmith/*
3267754Smsmith *
3367754Smsmith * HEADER: dapl_osd.h
3467754Smsmith *
3567754Smsmith * PURPOSE: Operating System Dependent layer
3667754Smsmith * Description:
3767754Smsmith *	Provide OS dependent data structures & functions with
3867754Smsmith *	a canonical DAPL interface. Designed to be portable
3967754Smsmith *	and hide OS specific quirks of common functions.
4067754Smsmith *
4167754Smsmith * $Id: dapl_osd.h,v 1.38 2003/08/20 14:08:57 sjs2 Exp $
4267754Smsmith */
4367754Smsmith
4467754Smsmith#ifndef _DAPL_OSD_H_
4567754Smsmith#define	_DAPL_OSD_H_
4667754Smsmith
4767754Smsmith#ifdef __cplusplus
4867754Smsmithextern "C" {
4967754Smsmith#endif
5067754Smsmith
5167754Smsmith/*
5267754Smsmith * <assert.h> keys off of NDEBUG
5367754Smsmith */
5467754Smsmith#ifdef	DAPL_DBG
5567754Smsmith#undef	NDEBUG
5667754Smsmith#else
5767754Smsmith#define	NDEBUG
5867754Smsmith#endif
5967754Smsmith
6067754Smsmith#include <dat/udat.h>
6167754Smsmith#include <assert.h>
6267754Smsmith#include <errno.h>
6367754Smsmith#include <pthread.h>
6467754Smsmith#include <semaphore.h>
6567754Smsmith#include <stdio.h>
6667754Smsmith#include <stdlib.h>
6767754Smsmith#include <string.h>
6867754Smsmith#include <stdarg.h>
6967754Smsmith#include <time.h>
7067754Smsmith#include <syslog.h>
7167754Smsmith#include <netdb.h>
7267754Smsmith#include <atomic.h>
7367754Smsmith#include "dapl_debug.h"
7467754Smsmith
7567754Smsmith/*
7667754Smsmith * networking related headers
7767754Smsmith */
7867754Smsmith#include <unistd.h>
7967754Smsmith#include <fcntl.h>
8067754Smsmith#include <sys/types.h>
8167754Smsmith#include <sys/socket.h>
8267754Smsmith#include <ctype.h>
8367754Smsmith#include <arpa/inet.h>
8467754Smsmith
8567754Smsmith#ifndef _INLINE_
8667754Smsmith#define	_INLINE_
8767754Smsmith#endif /* _INLINE_ */
8867754Smsmith
8967754Smsmith/*
9067754Smsmith * initialization function
9167754Smsmith */
9267754Smsmithvoid dapl_os_init(void);
9367754Smsmith
9467754Smsmith#define	dapl_os_panic(args) 			\
9567754Smsmith{					\
9667754Smsmith	fprintf(stderr, "PANIC in %s:%i:\n", __FILE__, __LINE__); \
9767754Smsmith	fprintf(stderr, args);			\
9867754Smsmith	exit(1);				\
9967754Smsmith}
10067754Smsmith
10167754Smsmithint dapl_os_get_env_bool(
10267754Smsmith	char		*env_str);
10367754Smsmith
10467754Smsmithint dapl_os_get_env_val(
10567754Smsmith	char		*env_str,
10667754Smsmith	int		def_val);
10767754Smsmith
10867754Smsmith/*
10967754Smsmith * Atomic operations
11067754Smsmith */
11167754Smsmithtypedef volatile DAT_COUNT DAPL_ATOMIC;
11267754Smsmith
11367754Smsmith/*
11467754Smsmith * dapl_os_atomic_inc
11567754Smsmith *
11667754Smsmith * get the current value of '*v', and then increment it.
11767754Smsmith *
11867754Smsmith * This is equivalent to an IB atomic fetch and add of 1,
11967754Smsmith * except that a DAT_COUNT might be 32 bits, rather than 64
12067754Smsmith * and it occurs in local memory.
12167754Smsmith *
12267754Smsmith * DAT_COUNT dapl_os_atomic_inc(INOUT	DAPL_ATOMIC *v)
12380062Smsmith */
12467754Smsmith#define	dapl_os_atomic_inc(v)	((DAT_COUNT)			\
12577424Smsmith				(atomic_add_32_nv((uint32_t *)(v), 1) - 1))
12667754Smsmith
12767754Smsmith/*
12867754Smsmith * dapl_os_atomic_dec
12967754Smsmith *
13067754Smsmith * decrement the current value of '*v'. No return value is required.
13167754Smsmith *
13267754Smsmith * DAT_COUNT dapl_os_atomic_dec(INOUT	DAPL_ATOMIC *v)
13367754Smsmith */
13467754Smsmith#define	dapl_os_atomic_dec(v)	assert(*v != 0);		\
13567754Smsmith				((DAT_COUNT)			\
13667754Smsmith				(atomic_add_32_nv((uint32_t *)(v), -1) + 1))
13767754Smsmith
13867754Smsmith/*
13967754Smsmith * dapl_os_atomic_assign
14067754Smsmith *
14167754Smsmith * assign 'new_value' to '*v' if the current value
14267754Smsmith * matches the provided 'match_value'.
14367754Smsmith *
14467754Smsmith * Make no assignment if there is no match.
14567754Smsmith *
14667754Smsmith * Return the current value in any case.
14767754Smsmith *
14867754Smsmith * This matches the IBTA atomic operation compare & swap
14967754Smsmith * except that it is for local memory and a DAT_COUNT may
15067754Smsmith * be only 32 bits, rather than 64.
15167754Smsmith *
15267754Smsmith * DAT_COUNT dapl_os_atomic_assign(INOUT DAPL_ATOMIC *v,
15377424Smsmith *	IN DAT_COUNT match_value, IN DAT_COUNT new_value)
15477424Smsmith */
15567754Smsmith#define	dapl_os_atomic_assign(v, match_value, new_value)		\
15667754Smsmith		atomic_cas_32((uint32_t *)(v), (uint32_t)(match_value),	\
15767754Smsmith		    (uint32_t)(new_value))
15867754Smsmith
15967754Smsmith/*
16067754Smsmith * Thread Functions
16167754Smsmith */
16267754Smsmithtypedef pthread_t		DAPL_OS_THREAD;
16367754Smsmith
16467754SmsmithDAT_RETURN
16567754Smsmithdapl_os_thread_create(
16667754Smsmith	IN  void			(*func)	(void *),
16767754Smsmith	IN  void			*data,
16867754Smsmith	OUT DAPL_OS_THREAD		*thread_id);
16967754Smsmith
17067754Smsmith
17167754Smsmith/*
17277424Smsmith * Lock Functions
17367754Smsmith */
17467754Smsmith
17567754Smsmithtypedef pthread_mutex_t 	DAPL_OS_LOCK;
17667754Smsmith
17777424Smsmith/*
17877424Smsmith * DAT_RETURN dapl_os_lock_init(IN DAPL_OS_LOCK *m)
17977424Smsmith */
18077424Smsmith#define	dapl_os_lock_init(m)	(void)					\
18177424Smsmith				((0 == pthread_mutex_init((m), NULL)) ?	\
18277424Smsmith					DAT_SUCCESS :			\
18367754Smsmith					(DAT_CLASS_ERROR | DAT_INTERNAL_ERROR))
18467754Smsmith
18567754Smsmith/* DAT_RETURN dapl_os_lock(IN DAPL_OS_LOCK *m) */
18667754Smsmith#define	dapl_os_lock(m)		((DAT_RETURN)(				\
18767754Smsmith				(0 == pthread_mutex_lock((m))) ?	\
18877424Smsmith					DAT_SUCCESS :			\
18967754Smsmith					(DAT_CLASS_ERROR | DAT_INTERNAL_ERROR)))
19067754Smsmith
19167754Smsmith/* DAT_RETURN dapl_os_unlock(IN DAPL_OS_LOCK *m) */
19267754Smsmith#define	dapl_os_unlock(m)	((DAT_RETURN)(				\
19367754Smsmith				(0 == pthread_mutex_unlock((m))) ?	\
19467754Smsmith					DAT_SUCCESS :			\
19567754Smsmith					(DAT_CLASS_ERROR | DAT_INTERNAL_ERROR)))
19667754Smsmith
19767754Smsmith/* DAT_RETURN dapl_os_lock_destroy(IN DAPL_OS_LOCK *m) */
19867754Smsmith#define	dapl_os_lock_destroy(m)	((DAT_RETURN)(				\
19977424Smsmith				(0 == pthread_mutex_destroy((m))) ?	\
20077424Smsmith					DAT_SUCCESS :			\
20167754Smsmith					(DAT_CLASS_ERROR | DAT_INTERNAL_ERROR)))
20277424Smsmith/*
20367754Smsmith * Wait Objects
20467754Smsmith */
20567754Smsmith
20673561Smsmith/*
20767754Smsmith * The wait object invariant: Presuming a call to dapl_os_wait_object_wait
20867754Smsmith * occurs at some point, there will be at least one wakeup after each call
20967754Smsmith * to dapl_os_wait_object_signal.  I.e. Signals are not ignored, though
21067754Smsmith * they may be coallesced.
21167754Smsmith */
21267754Smsmith
21367754Smsmithtypedef struct
21467754Smsmith{
21567754Smsmith    DAT_BOOLEAN		signaled;
21667754Smsmith    pthread_cond_t	cv;
21767754Smsmith    pthread_mutex_t	lock;
21867754Smsmith} DAPL_OS_WAIT_OBJECT;
21967754Smsmith
22067754Smsmith/* function prototypes */
22167754SmsmithDAT_RETURN
22277424Smsmithdapl_os_wait_object_init(
22377424Smsmith    IN DAPL_OS_WAIT_OBJECT *wait_obj);
22467754Smsmith
22567754SmsmithDAT_RETURN
22667754Smsmithdapl_os_wait_object_wait(
22777424Smsmith    IN	DAPL_OS_WAIT_OBJECT *wait_obj,
22867754Smsmith    IN  DAT_TIMEOUT timeout_val);
22967754Smsmith
23067754SmsmithDAT_RETURN
23167754Smsmithdapl_os_wait_object_wakeup(
23269746Smsmith    IN	DAPL_OS_WAIT_OBJECT *wait_obj);
23369746Smsmith
23467754SmsmithDAT_RETURN
23567754Smsmithdapl_os_wait_object_destroy(
23667754Smsmith    IN	DAPL_OS_WAIT_OBJECT *wait_obj);
23767754Smsmith
23867754Smsmith/*
23967754Smsmith * Memory Functions
24067754Smsmith */
24167754Smsmith
24267754Smsmith/* void *dapl_os_alloc(int size) */
24367754Smsmith#define	dapl_os_alloc(size)	malloc((size))
24467754Smsmith
24567754Smsmith/* void *dapl_os_realloc(void *ptr, int size) */
24667754Smsmith#define	dapl_os_realloc(ptr, size) realloc((ptr), (size))
24767754Smsmith
24867754Smsmith/* void dapl_os_free(void *ptr, int size) */
24967754Smsmith#define	dapl_os_free(ptr, size)	free((ptr))
25067754Smsmith
25167754Smsmith/* void * dapl_os_memzero(void *loc, int size) */
25267754Smsmith#define	dapl_os_memzero(loc, size)	memset((loc), 0, (size))
25367754Smsmith
25467754Smsmith/* void * dapl_os_memcpy(void *dest, const void *src, int len) */
25567754Smsmith#define	dapl_os_memcpy(dest, src, len)	memcpy((dest), (src), (len))
25667754Smsmith
25767754Smsmith/* int dapl_os_memcmp(const void *mem1, const void *mem2, int len) */
25867754Smsmith#define	dapl_os_memcmp(mem1, mem2, len)	memcmp((mem1), (mem2), (len))
25967754Smsmith
26067754Smsmith/*
26167754Smsmith * String Functions
26267754Smsmith */
26367754Smsmith
26467754Smsmith/* unsigned int dapl_os_strlen(const char *str) */
26567754Smsmith#define	dapl_os_strlen(str)	strlen((str))
26677424Smsmith/* char * dapl_os_strdup(const char *str) */
26767754Smsmith#define	dapl_os_strdup(str)	strdup((str))
26867754Smsmith/* char *strcpy(char *dest, char *src) */
26967754Smsmith#define	dapl_os_strcpy(dest, src) 	strcpy((dest), (src))
27067754Smsmith/* char *strncpy(char *s1, const char *s2, size_t n) */
27167754Smsmith#define	dapl_os_strncpy(dest, src, len) strncpy((dest), (src), (len))
27267754Smsmith/* char *strcat(char *dest, char *src) */
27367754Smsmith#define	dapl_os_strcat(dest, src) 	strcat((dest), (src))
27467754Smsmith
27567754Smsmith/*
27667754Smsmith * Timer Functions
27767754Smsmith */
27867754Smsmith
27967754Smsmithtypedef DAT_UINT64		DAPL_OS_TIMEVAL;
28067754Smsmith
28167754Smsmith
28267754Smsmithtypedef unsigned long long int	DAPL_OS_TICKS;
28367754Smsmith
28467754Smsmith/* function prototypes */
28580062Smsmith
28680062Smsmith/*
28780062Smsmith * Sleep for the number of micro seconds specified by the invoking
28880062Smsmith * function
28980062Smsmith *
29080062Smsmith * void dapl_os_sleep_usec(int sleep_time)
29180062Smsmith */
29280062Smsmith#define	dapl_os_sleep_usec(sleep_time)	{				\
29380062Smsmith		struct timespec sleep_spec;				\
29480062Smsmith		sleep_spec.tv_sec = (sleep_time) / 100000;		\
29580062Smsmith		sleep_spec.tv_nsec = (sleep_time) % 100000 * 1000;	\
29680062Smsmith		nanosleep(&sleep_spec, NULL);				\
29780062Smsmith		}
29880062Smsmith
29980062SmsmithDAT_RETURN dapl_os_get_time(DAPL_OS_TIMEVAL *);
30080062Smsmith
30167754Smsmith/*
30267754Smsmith *
30367754Smsmith * Name Service Helper functions
30467754Smsmith *
30567754Smsmith */
30667754Smsmith#if defined(IBHOSTS_NAMING)
30767754Smsmith#define	dapls_osd_getaddrinfo(name, addr_ptr)		\
30867754Smsmith				getaddrinfo((name), NULL, NULL, (addr_ptr))
30967754Smsmith#define	dapls_osd_freeaddrinfo(addr) freeaddrinfo((addr))
31067754Smsmith
31167754Smsmith#endif /* IBHOSTS_NAMING */
31267754Smsmith
31367754Smsmith/*
31469746Smsmith * *printf format helpers. We use the C string constant concatenation
31569746Smsmith * ability to define 64 bit formats, which unfortunatly are non standard
31669746Smsmith * in the C compiler world. E.g. %llx for gcc, %I64x for Windows
31777424Smsmith */
31877424Smsmith#define	F64d   "%lld"
31980062Smsmith#define	F64u   "%llu"
32069746Smsmith#define	F64x   "%llx"
32169746Smsmith#define	F64X   "%llX"
32273561Smsmith
32373561Smsmith
32473561Smsmith/*
32573561Smsmith *  Conversion Functions
32673561Smsmith */
32767754Smsmith
32867754Smsmith/* long int dapl_os_strtol(const char *nptr, char **endptr, int base) */
32967754Smsmith#define	dapl_os_strtol(nptr, endptr, base)	strtol((nptr), (endptr), (base))
33077424Smsmith
33167754Smsmith/*
33267754Smsmith *  Helper Functions
33367754Smsmith */
33467754Smsmith
33567754Smsmith
33667754Smsmith#define	dapl_os_assert(expression)	assert((expression))
33767754Smsmith#define	dapl_os_printf			printf
33867754Smsmith#define	dapl_os_vprintf(fmt, args)	vprintf((fmt), (args))
33977424Smsmith#define	dapl_os_syslog(fmt, args)	vsyslog(LOG_USER | LOG_DEBUG,	\
34069746Smsmith						(fmt), (args))
34169746Smsmith#ifdef __cplusplus
34269746Smsmith}
34377424Smsmith#endif
34477424Smsmith
34580062Smsmith#endif /* _DAPL_OSD_H_ */
34669746Smsmith