1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
3219820Sjeff *
4219820Sjeff * This software is available to you under a choice of one of two
5219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
6219820Sjeff * General Public License (GPL) Version 2, available from the file
7219820Sjeff * COPYING in the main directory of this source tree, or the
8219820Sjeff * OpenIB.org BSD license below:
9219820Sjeff *
10219820Sjeff *     Redistribution and use in source and binary forms, with or
11219820Sjeff *     without modification, are permitted provided that the following
12219820Sjeff *     conditions are met:
13219820Sjeff *
14219820Sjeff *      - Redistributions of source code must retain the above
15219820Sjeff *        copyright notice, this list of conditions and the following
16219820Sjeff *        disclaimer.
17219820Sjeff *
18219820Sjeff *      - Redistributions in binary form must reproduce the above
19219820Sjeff *        copyright notice, this list of conditions and the following
20219820Sjeff *        disclaimer in the documentation and/or other materials
21219820Sjeff *        provided with the distribution.
22219820Sjeff *
23219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30219820Sjeff * SOFTWARE.
31219820Sjeff *
32219820Sjeff */
33219820Sjeff
34219820Sjeff#define _GNU_SOURCE
35219820Sjeff
36219820Sjeff#if HAVE_CONFIG_H
37219820Sjeff#  include <config.h>
38219820Sjeff#endif /* HAVE_CONFIG_H */
39219820Sjeff
40219820Sjeff#include <inttypes.h>
41219820Sjeff#include <string.h>
42219820Sjeff#include <errno.h>
43219820Sjeff#include <stdio.h>
44219820Sjeff#include <stdlib.h>
45219820Sjeff#include <unistd.h>
46219820Sjeff#include <stdarg.h>
47219820Sjeff#include <sys/types.h>
48219820Sjeff#include <sys/stat.h>
49219820Sjeff#include <fcntl.h>
50219820Sjeff#include <sys/ioctl.h>
51219820Sjeff#include <unistd.h>
52219820Sjeff#include <string.h>
53219820Sjeff#include <endian.h>
54219820Sjeff#include <byteswap.h>
55219820Sjeff#include <sys/poll.h>
56219820Sjeff#include <syslog.h>
57219820Sjeff#include <netinet/in.h>
58219820Sjeff
59219820Sjeff#include <common.h>
60219820Sjeff
61219820Sjeffvoid
62219820Sjeffibwarn(const char * const fn, char *msg, ...)
63219820Sjeff{
64219820Sjeff	char buf[512];
65219820Sjeff	va_list va;
66219820Sjeff	int n;
67219820Sjeff
68219820Sjeff	va_start(va, msg);
69219820Sjeff	n = vsnprintf(buf, sizeof(buf), msg, va);
70219820Sjeff	va_end(va);
71219820Sjeff
72219820Sjeff	printf("ibwarn: [%d] %s: %s\n", getpid(), fn, buf);
73219820Sjeff}
74219820Sjeff
75219820Sjeffvoid
76219820Sjeffibpanic(const char * const fn, char *msg, ...)
77219820Sjeff{
78219820Sjeff	char buf[512];
79219820Sjeff	va_list va;
80219820Sjeff	int n;
81219820Sjeff
82219820Sjeff	va_start(va, msg);
83219820Sjeff	n = vsnprintf(buf, sizeof(buf), msg, va);
84219820Sjeff	va_end(va);
85219820Sjeff
86219820Sjeff	printf("ibpanic: [%d] %s: %s: (%m)\n", getpid(), fn, buf);
87219820Sjeff	syslog(LOG_ALERT, "ibpanic: [%d] %s: %s: (%m)\n", getpid(), fn, buf);
88219820Sjeff
89219820Sjeff	exit(-1);
90219820Sjeff}
91219820Sjeff
92219820Sjeffvoid
93219820Sjefflogmsg(const char * const fn, char *msg, ...)
94219820Sjeff{
95219820Sjeff	char buf[512];
96219820Sjeff	va_list va;
97219820Sjeff	int n;
98219820Sjeff
99219820Sjeff	va_start(va, msg);
100219820Sjeff	n = vsnprintf(buf, sizeof(buf), msg, va);
101219820Sjeff	va_end(va);
102219820Sjeff
103219820Sjeff	syslog(LOG_ALERT, "[%d] %s: %s: (%m)\n", getpid(), fn, buf);
104219820Sjeff}
105219820Sjeff
106219820Sjeffvoid
107219820Sjeffxdump(FILE *file, char *msg, void *p, int size)
108219820Sjeff{
109219820Sjeff#define HEX(x)  ((x) < 10 ? '0' + (x) : 'a' + ((x) -10))
110219820Sjeff        uint8_t *cp = p;
111219820Sjeff        int i;
112219820Sjeff
113219820Sjeff	if (msg)
114219820Sjeff		fputs(msg, file);
115219820Sjeff
116219820Sjeff        for (i = 0; i < size;) {
117219820Sjeff                fputc(HEX(*cp >> 4), file);
118219820Sjeff                fputc(HEX(*cp & 0xf), file);
119219820Sjeff                if (++i >= size)
120219820Sjeff                        break;
121219820Sjeff                fputc(HEX(cp[1] >> 4), file);
122219820Sjeff                fputc(HEX(cp[1] & 0xf), file);
123219820Sjeff                if ((++i) % 16)
124219820Sjeff                        fputc(' ', file);
125219820Sjeff                else
126219820Sjeff                        fputc('\n', file);
127219820Sjeff                cp += 2;
128219820Sjeff        }
129219820Sjeff        if (i % 16) {
130219820Sjeff                fputc('\n', file);
131219820Sjeff        }
132219820Sjeff}
133