2 * Copyright 2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and
30 * documentation are those of the authors and should not be
31 * interpreted as representing official policies, either expressed
32 * or implied, of GRNET S.A.
35 #ifndef _XSEG_SYS_UTIL_H
36 #define _XSEG_SYS_UTIL_H
39 #include <sys/domain.h>
41 /* performance stuff */
43 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 303)
46 #define LIKELY(x) __builtin_expect(!!(x),1)
49 #define UNLIKELY(x) __builtin_expect(!!(x),0)
54 #define UNLIKELY(x) (x)
60 #define FMTARG(fmt, arg, format, ...) fmt format "%s", arg, ## __VA_ARGS__
61 #define XSEGLOG(...) (xseg_snprintf(__xseg_errbuf, 4096, FMTARG("%s: ", __func__, ## __VA_ARGS__, "")), \
62 __xseg_errbuf[4095] = 0, __xseg_log(__xseg_errbuf))
64 #define XSEGLOG2(__ctx, __level, ...) \
66 if (__level <= ((__ctx)->log_level)) { \
67 __xseg_log2(__ctx, __level, FMTARG("%s: ", __func__, ## __VA_ARGS__ ,"")); \
72 void log_request(struct log_context *lc, struct xseg *xseg, struct xseg_request *req)
74 __xseg_log2(lc, I, "\n\t"
75 "Request %lx: target[%u](xptr: %llu): %s, data[%llu](xptr: %llu): %s \n\t"
76 "offset: %llu, size: %llu, serviced; %llu, op: %u, state: %u, flags: %u \n\t"
77 "src: %u, transit: %u, dst: %u, effective dst: %u",
78 (unsigned long) req, req->targetlen, (unsigned long long)req->target,
79 xseg_get_target(xseg, req),
80 (unsigned long long) req->datalen, (unsigned long long) req->data,
81 xseg_get_data(xseg, req),
82 (unsigned long long) req->offset, (unsigned long long) req->size,
83 (unsigned long long) req->serviced, req->op, req->state, req->flags,
84 (unsigned int) req->src_portno, (unsigned int) req->transit_portno,
85 (unsigned int) req->dst_portno, (unsigned int) req->effective_dst_portno);
89 /* general purpose xflags */
90 #define X_ALLOC ((uint32_t) (1 << 0))
91 #define X_LOCAL ((uint32_t) (1 << 1))
92 #define X_NONBLOCK ((uint32_t) (1 << 2))
95 typedef uint64_t xpointer;
97 /* type to be used as absolute pointer
98 * this should be the same as xqindex
99 * and must fit into a pointer type
101 typedef uint64_t xptr;
103 #define Noneidx ((xqindex)-1)
104 #define Null ((xpointer)-1)
106 #define __align(x, shift) (((((x) -1) >> (shift)) +1) << (shift))
108 #define XPTR_TYPE(ptrtype) \
114 #define XPTRI(xptraddr) \
115 ( (xpointer)(unsigned long)(xptraddr) + \
118 #define XPTRISET(xptraddr, ptrval) \
119 ((xptraddr)->x = (xpointer)(ptrval) - \
120 (xpointer)(unsigned long)(xptraddr) )
122 #define XPTR(xptraddr) \
123 ( (typeof((xptraddr)->t)) \
125 ( (xpointer)(unsigned long)(xptraddr) + \
128 #define XPTRSET(xptraddr, ptrval) \
129 ((xptraddr)->x = (xpointer)(unsigned long)(ptrval) - \
130 (xpointer)(unsigned long)(xptraddr) )
134 #define XPTR_OFFSET(base, ptr) ((unsigned long)(ptr) - (unsigned long)(base))
136 #define XPTR_MAKE(ptrval, base) ((xptr) XPTR_OFFSET(base, ptrval))
138 #define XPTR_TAKE(xptrval, base) \
139 ( (void *) ( (unsigned long) base + (unsigned long) xptrval))