199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause */ 299a2dd95SBruce Richardson 399a2dd95SBruce Richardson #ifndef _RTE_OS_SHIM_ 499a2dd95SBruce Richardson #define _RTE_OS_SHIM_ 599a2dd95SBruce Richardson 61ee89997SDmitry Kozlyuk #include <time.h> 71ee89997SDmitry Kozlyuk 899a2dd95SBruce Richardson #include <rte_os.h> 91ee89997SDmitry Kozlyuk #include <rte_windows.h> 1099a2dd95SBruce Richardson 1199a2dd95SBruce Richardson /** 1299a2dd95SBruce Richardson * @file 1399a2dd95SBruce Richardson * @internal 1499a2dd95SBruce Richardson * Provides semi-standard OS facilities by convenient names. 1599a2dd95SBruce Richardson */ 1699a2dd95SBruce Richardson 1799a2dd95SBruce Richardson #ifndef PATH_MAX 1899a2dd95SBruce Richardson #define PATH_MAX _MAX_PATH 1999a2dd95SBruce Richardson #endif 2099a2dd95SBruce Richardson 2199a2dd95SBruce Richardson #define strdup(str) _strdup(str) 2299a2dd95SBruce Richardson #define strtok_r(str, delim, saveptr) strtok_s(str, delim, saveptr) 232231388fSTal Shnaiderman #ifndef RTE_TOOLCHAIN_GCC 2422f463e1SJie Zhou #define strcasecmp(s1, s2) _stricmp(s1, s2) 2599a2dd95SBruce Richardson #define strncasecmp(s1, s2, count) _strnicmp(s1, s2, count) 262231388fSTal Shnaiderman #endif 2799a2dd95SBruce Richardson 28826476bcSTal Shnaiderman #define open(...) _open(__VA_ARGS__) 2999a2dd95SBruce Richardson #define read(fd, buf, n) _read(fd, buf, n) 3099a2dd95SBruce Richardson #define write(fd, buf, n) _write(fd, buf, n) 3199a2dd95SBruce Richardson #define close(fd) _close(fd) 3299a2dd95SBruce Richardson #define unlink(path) _unlink(path) 33*259f6f78SStephen Hemminger #define fileno(f) _fileno(f) 34*259f6f78SStephen Hemminger #define isatty(fd) _isatty(fd) 3599a2dd95SBruce Richardson 3699a2dd95SBruce Richardson #define IPVERSION 4 3799a2dd95SBruce Richardson 3899a2dd95SBruce Richardson #define IPPROTO_IPIP 4 3999a2dd95SBruce Richardson #define IPPROTO_GRE 47 4099a2dd95SBruce Richardson #ifdef RTE_TOOLCHAIN_GCC 4199a2dd95SBruce Richardson #define IPPROTO_SCTP 132 4299a2dd95SBruce Richardson #endif 4399a2dd95SBruce Richardson 4422f463e1SJie Zhou #ifndef IPDEFTTL 4522f463e1SJie Zhou #define IPDEFTTL 64 4622f463e1SJie Zhou #endif 4722f463e1SJie Zhou 4822f463e1SJie Zhou #ifndef S_ISREG 4922f463e1SJie Zhou #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) 5022f463e1SJie Zhou #endif 5122f463e1SJie Zhou 521ee89997SDmitry Kozlyuk #ifdef RTE_TOOLCHAIN_GCC 531ee89997SDmitry Kozlyuk 541ee89997SDmitry Kozlyuk #define TIME_UTC 1 551ee89997SDmitry Kozlyuk 561ee89997SDmitry Kozlyuk static inline int 571ee89997SDmitry Kozlyuk rte_timespec_get(struct timespec *now, int base) 581ee89997SDmitry Kozlyuk { 591ee89997SDmitry Kozlyuk /* 100ns ticks from 1601-01-01 to 1970-01-01 */ 601ee89997SDmitry Kozlyuk static const uint64_t EPOCH = 116444736000000000ULL; 611ee89997SDmitry Kozlyuk static const uint64_t TICKS_PER_SEC = 10000000; 621ee89997SDmitry Kozlyuk static const uint64_t NS_PER_TICK = 100; 631ee89997SDmitry Kozlyuk 641ee89997SDmitry Kozlyuk FILETIME ft; 651ee89997SDmitry Kozlyuk uint64_t ticks; 661ee89997SDmitry Kozlyuk 671ee89997SDmitry Kozlyuk if (base != TIME_UTC) 681ee89997SDmitry Kozlyuk return 0; 691ee89997SDmitry Kozlyuk 701ee89997SDmitry Kozlyuk GetSystemTimePreciseAsFileTime(&ft); 711ee89997SDmitry Kozlyuk ticks = ((uint64_t)ft.dwHighDateTime << 32) | ft.dwLowDateTime; 721ee89997SDmitry Kozlyuk ticks -= EPOCH; 731ee89997SDmitry Kozlyuk now->tv_sec = ticks / TICKS_PER_SEC; 741ee89997SDmitry Kozlyuk now->tv_nsec = (ticks - now->tv_sec * TICKS_PER_SEC) * NS_PER_TICK; 751ee89997SDmitry Kozlyuk return base; 761ee89997SDmitry Kozlyuk } 771ee89997SDmitry Kozlyuk 781ee89997SDmitry Kozlyuk #define timespec_get(ts, base) rte_timespec_get(ts, base) 791ee89997SDmitry Kozlyuk 801ee89997SDmitry Kozlyuk #endif /* RTE_TOOLCHAIN_GCC */ 811ee89997SDmitry Kozlyuk 82799a5b9aSJie Zhou /* Identifier for system-wide realtime clock. */ 83799a5b9aSJie Zhou #define CLOCK_REALTIME 0 84799a5b9aSJie Zhou /* Monotonic system-wide clock. */ 85799a5b9aSJie Zhou #define CLOCK_MONOTONIC 1 86799a5b9aSJie Zhou 87799a5b9aSJie Zhou typedef int clockid_t; 88799a5b9aSJie Zhou 89799a5b9aSJie Zhou static inline int 90799a5b9aSJie Zhou rte_clock_gettime(clockid_t clock_id, struct timespec *tp) 91799a5b9aSJie Zhou { 92799a5b9aSJie Zhou const int NS_PER_SEC = 1E9; 93799a5b9aSJie Zhou LARGE_INTEGER pf, pc; 94799a5b9aSJie Zhou LONGLONG nsec; 95799a5b9aSJie Zhou 96799a5b9aSJie Zhou switch (clock_id) { 97799a5b9aSJie Zhou case CLOCK_REALTIME: 98799a5b9aSJie Zhou if (timespec_get(tp, TIME_UTC) != TIME_UTC) 99799a5b9aSJie Zhou return -1; 100799a5b9aSJie Zhou return 0; 101799a5b9aSJie Zhou case CLOCK_MONOTONIC: 102799a5b9aSJie Zhou QueryPerformanceFrequency(&pf); 103799a5b9aSJie Zhou QueryPerformanceCounter(&pc); 104799a5b9aSJie Zhou 105799a5b9aSJie Zhou nsec = pc.QuadPart * NS_PER_SEC / pf.QuadPart; 106799a5b9aSJie Zhou tp->tv_sec = nsec / NS_PER_SEC; 107799a5b9aSJie Zhou tp->tv_nsec = nsec - tp->tv_sec * NS_PER_SEC; 108799a5b9aSJie Zhou return 0; 109799a5b9aSJie Zhou default: 110799a5b9aSJie Zhou return -1; 111799a5b9aSJie Zhou } 112799a5b9aSJie Zhou } 113799a5b9aSJie Zhou #define clock_gettime(clock_id, tp) rte_clock_gettime(clock_id, tp) 114799a5b9aSJie Zhou 11562ae1149SStephen Hemminger static inline struct tm * 11662ae1149SStephen Hemminger rte_localtime_r(const time_t *timep, struct tm *result) 11762ae1149SStephen Hemminger { 11862ae1149SStephen Hemminger if (localtime_s(result, timep) == 0) 11962ae1149SStephen Hemminger return result; 12062ae1149SStephen Hemminger else 12162ae1149SStephen Hemminger return NULL; 12262ae1149SStephen Hemminger } 12362ae1149SStephen Hemminger #define localtime_r(timep, result) rte_localtime_r(timep, result) 12462ae1149SStephen Hemminger 12599a2dd95SBruce Richardson #endif /* _RTE_OS_SHIM_ */ 126