xref: /dpdk/lib/eal/windows/include/rte_os_shim.h (revision 259f6f78094d0fa33ce2ffe298b8df526c535f3b)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 
3 #ifndef _RTE_OS_SHIM_
4 #define _RTE_OS_SHIM_
5 
6 #include <time.h>
7 
8 #include <rte_os.h>
9 #include <rte_windows.h>
10 
11 /**
12  * @file
13  * @internal
14  * Provides semi-standard OS facilities by convenient names.
15  */
16 
17 #ifndef PATH_MAX
18 #define PATH_MAX _MAX_PATH
19 #endif
20 
21 #define strdup(str) _strdup(str)
22 #define strtok_r(str, delim, saveptr) strtok_s(str, delim, saveptr)
23 #ifndef RTE_TOOLCHAIN_GCC
24 #define strcasecmp(s1, s2) _stricmp(s1, s2)
25 #define strncasecmp(s1, s2, count) _strnicmp(s1, s2, count)
26 #endif
27 
28 #define open(...) _open(__VA_ARGS__)
29 #define read(fd, buf, n) _read(fd, buf, n)
30 #define write(fd, buf, n) _write(fd, buf, n)
31 #define close(fd) _close(fd)
32 #define unlink(path) _unlink(path)
33 #define fileno(f) _fileno(f)
34 #define isatty(fd) _isatty(fd)
35 
36 #define IPVERSION	4
37 
38 #define IPPROTO_IPIP	4
39 #define IPPROTO_GRE	47
40 #ifdef RTE_TOOLCHAIN_GCC
41 #define IPPROTO_SCTP	132
42 #endif
43 
44 #ifndef IPDEFTTL
45 #define IPDEFTTL 64
46 #endif
47 
48 #ifndef S_ISREG
49 #define S_ISREG(mode)  (((mode) & S_IFMT) == S_IFREG)
50 #endif
51 
52 #ifdef RTE_TOOLCHAIN_GCC
53 
54 #define TIME_UTC 1
55 
56 static inline int
57 rte_timespec_get(struct timespec *now, int base)
58 {
59 	/* 100ns ticks from 1601-01-01 to 1970-01-01 */
60 	static const uint64_t EPOCH = 116444736000000000ULL;
61 	static const uint64_t TICKS_PER_SEC = 10000000;
62 	static const uint64_t NS_PER_TICK = 100;
63 
64 	FILETIME ft;
65 	uint64_t ticks;
66 
67 	if (base != TIME_UTC)
68 		return 0;
69 
70 	GetSystemTimePreciseAsFileTime(&ft);
71 	ticks = ((uint64_t)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
72 	ticks -= EPOCH;
73 	now->tv_sec = ticks / TICKS_PER_SEC;
74 	now->tv_nsec = (ticks - now->tv_sec * TICKS_PER_SEC) * NS_PER_TICK;
75 	return base;
76 }
77 
78 #define timespec_get(ts, base) rte_timespec_get(ts, base)
79 
80 #endif /* RTE_TOOLCHAIN_GCC */
81 
82 /* Identifier for system-wide realtime clock. */
83 #define CLOCK_REALTIME                  0
84 /* Monotonic system-wide clock. */
85 #define CLOCK_MONOTONIC                 1
86 
87 typedef int clockid_t;
88 
89 static inline int
90 rte_clock_gettime(clockid_t clock_id, struct timespec *tp)
91 {
92 	const int NS_PER_SEC = 1E9;
93 	LARGE_INTEGER pf, pc;
94 	LONGLONG nsec;
95 
96 	switch (clock_id) {
97 	case CLOCK_REALTIME:
98 		if (timespec_get(tp, TIME_UTC) != TIME_UTC)
99 			return -1;
100 		return 0;
101 	case CLOCK_MONOTONIC:
102 		QueryPerformanceFrequency(&pf);
103 		QueryPerformanceCounter(&pc);
104 
105 		nsec = pc.QuadPart * NS_PER_SEC / pf.QuadPart;
106 		tp->tv_sec = nsec / NS_PER_SEC;
107 		tp->tv_nsec = nsec - tp->tv_sec * NS_PER_SEC;
108 		return 0;
109 	default:
110 		return -1;
111 	}
112 }
113 #define clock_gettime(clock_id, tp) rte_clock_gettime(clock_id, tp)
114 
115 static inline struct tm *
116 rte_localtime_r(const time_t *timep, struct tm *result)
117 {
118 	if (localtime_s(result, timep) == 0)
119 		return result;
120 	else
121 		return NULL;
122 }
123 #define localtime_r(timep, result) rte_localtime_r(timep, result)
124 
125 #endif /* _RTE_OS_SHIM_ */
126