TAS
TCP Acceleration as an OS Service
utils.h
1 /*
2  * Copyright 2019 University of Washington, Max Planck Institute for
3  * Software Systems, and The University of Texas at Austin
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef UTILS_H_
26 #define UTILS_H_
27 
28 #include <stdint.h>
29 #include <arpa/inet.h>
30 
31 #define MIN(a,b) ((b) < (a) ? (b) : (a))
32 #define MAX(a,b) ((b) > (a) ? (b) : (a))
33 #define MEM_BARRIER() __asm__ volatile("" ::: "memory")
34 #define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(COND)?1:-1]
35 #define LIKELY(x) __builtin_expect((x),1)
36 #define UNLIKELY(x) __builtin_expect((x),0)
37 
38 int util_parse_ipv4(const char *s, uint32_t *ip);
39 int util_parse_mac(const char *s, uint64_t *mac);
40 
41 void util_dump_mem(const void *b, size_t len);
42 
43 /* types for big endian integers to catch those errors with types */
44 struct beui16 { uint16_t x; } __attribute__((packed));
45 struct beui32 { uint32_t x; } __attribute__((packed));
46 struct beui64 { uint64_t x; } __attribute__((packed));
47 typedef struct beui16 beui16_t;
48 typedef struct beui32 beui32_t;
49 typedef struct beui64 beui64_t;
50 
51 static inline uint16_t f_beui16(beui16_t x) { return __builtin_bswap16(x.x); }
52 static inline uint32_t f_beui32(beui32_t x) { return __builtin_bswap32(x.x); }
53 static inline uint64_t f_beui64(beui64_t x) { return __builtin_bswap64(x.x); }
54 
55 static inline beui16_t t_beui16(uint16_t x)
56 {
57  beui16_t b;
58  b.x = __builtin_bswap16(x);
59  return b;
60 }
61 
62 static inline beui32_t t_beui32(uint32_t x)
63 {
64  beui32_t b;
65  b.x = __builtin_bswap32(x);
66  return b;
67 }
68 
69 static inline beui64_t t_beui64(uint64_t x)
70 {
71  beui64_t b;
72  b.x = __builtin_bswap64(x);
73  return b;
74 }
75 
76 static inline uint64_t util_rdtsc(void)
77 {
78  uint32_t eax, edx;
79  asm volatile ("rdtsc" : "=a" (eax), "=d" (edx));
80  return ((uint64_t) edx << 32) | eax;
81 }
82 
83 static inline void util_prefetch0(const volatile void *p)
84 {
85  asm volatile ("prefetcht0 %[p]" : : [p] "m" (*(const volatile char *)p));
86 }
87 
88 
89 
90 #endif /* ndef UTILS_H_ */
Definition: utils.h:44
Definition: utils.h:45
Definition: utils.h:46