18d59ecb2SHans Petter Selasky /*-
28d59ecb2SHans Petter Selasky * Copyright (c) 2010 Isilon Systems, Inc.
38d59ecb2SHans Petter Selasky * Copyright (c) 2010 iX Systems, Inc.
48d59ecb2SHans Petter Selasky * Copyright (c) 2010 Panasas, Inc.
5fe68f570SHans Petter Selasky * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
68d59ecb2SHans Petter Selasky * Copyright (c) 2014-2015 François Tigeot
78d59ecb2SHans Petter Selasky * All rights reserved.
88d59ecb2SHans Petter Selasky *
98d59ecb2SHans Petter Selasky * Redistribution and use in source and binary forms, with or without
108d59ecb2SHans Petter Selasky * modification, are permitted provided that the following conditions
118d59ecb2SHans Petter Selasky * are met:
128d59ecb2SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright
138d59ecb2SHans Petter Selasky * notice unmodified, this list of conditions, and the following
148d59ecb2SHans Petter Selasky * disclaimer.
158d59ecb2SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright
168d59ecb2SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the
178d59ecb2SHans Petter Selasky * documentation and/or other materials provided with the distribution.
188d59ecb2SHans Petter Selasky *
198d59ecb2SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
208d59ecb2SHans Petter Selasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
218d59ecb2SHans Petter Selasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
228d59ecb2SHans Petter Selasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
238d59ecb2SHans Petter Selasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
248d59ecb2SHans Petter Selasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
258d59ecb2SHans Petter Selasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
268d59ecb2SHans Petter Selasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
278d59ecb2SHans Petter Selasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
288d59ecb2SHans Petter Selasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
298d59ecb2SHans Petter Selasky */
30307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_LINUX_KERNEL_H_
31307f78f3SVladimir Kondratyev #define _LINUXKPI_LINUX_KERNEL_H_
328d59ecb2SHans Petter Selasky
338d59ecb2SHans Petter Selasky #include <sys/types.h>
348d59ecb2SHans Petter Selasky #include <sys/systm.h>
358d59ecb2SHans Petter Selasky #include <sys/param.h>
368d59ecb2SHans Petter Selasky #include <sys/libkern.h>
378d59ecb2SHans Petter Selasky #include <sys/stat.h>
388d59ecb2SHans Petter Selasky #include <sys/smp.h>
398d59ecb2SHans Petter Selasky #include <sys/stddef.h>
408d59ecb2SHans Petter Selasky #include <sys/syslog.h>
418f7eee5aSHans Petter Selasky #include <sys/time.h>
428d59ecb2SHans Petter Selasky
438d59ecb2SHans Petter Selasky #include <linux/bitops.h>
4431ef2909SVladimir Kondratyev #include <linux/build_bug.h>
458d59ecb2SHans Petter Selasky #include <linux/compiler.h>
46fa862482SVladimir Kondratyev #include <linux/container_of.h>
47a97f3a9eSVladimir Kondratyev #include <linux/kstrtox.h>
488cfd1dd8SVladimir Kondratyev #include <linux/limits.h>
49588064e6SVladimir Kondratyev #include <linux/math.h>
50f79474c4SVladimir Kondratyev #include <linux/minmax.h>
5143b4c006SBjoern A. Zeeb #include <linux/stringify.h>
528d59ecb2SHans Petter Selasky #include <linux/errno.h>
538d59ecb2SHans Petter Selasky #include <linux/types.h>
544b2cb13eSEmmanuel Vadot #include <linux/typecheck.h>
558d59ecb2SHans Petter Selasky #include <linux/jiffies.h>
568d59ecb2SHans Petter Selasky #include <linux/log2.h>
574b0552d5SJean-Sébastien Pédron #include <linux/kconfig.h>
5835555d47SHans Petter Selasky
598d59ecb2SHans Petter Selasky #include <asm/byteorder.h>
604b2cb13eSEmmanuel Vadot #include <asm/cpufeature.h>
614b2cb13eSEmmanuel Vadot #include <asm/processor.h>
6235555d47SHans Petter Selasky #include <asm/uaccess.h>
638d59ecb2SHans Petter Selasky
64d91cf060SJean-Sébastien Pédron #include <linux/stdarg.h>
65aef2a67bSHans Petter Selasky
668d59ecb2SHans Petter Selasky #define KERN_CONT ""
678d59ecb2SHans Petter Selasky #define KERN_EMERG "<0>"
688d59ecb2SHans Petter Selasky #define KERN_ALERT "<1>"
698d59ecb2SHans Petter Selasky #define KERN_CRIT "<2>"
708d59ecb2SHans Petter Selasky #define KERN_ERR "<3>"
718d59ecb2SHans Petter Selasky #define KERN_WARNING "<4>"
728d59ecb2SHans Petter Selasky #define KERN_NOTICE "<5>"
738d59ecb2SHans Petter Selasky #define KERN_INFO "<6>"
748d59ecb2SHans Petter Selasky #define KERN_DEBUG "<7>"
758d59ecb2SHans Petter Selasky
76abb14a54SHans Petter Selasky #define S8_C(x) x
77abb14a54SHans Petter Selasky #define U8_C(x) x ## U
78abb14a54SHans Petter Selasky #define S16_C(x) x
79abb14a54SHans Petter Selasky #define U16_C(x) x ## U
80abb14a54SHans Petter Selasky #define S32_C(x) x
81abb14a54SHans Petter Selasky #define U32_C(x) x ## U
82abb14a54SHans Petter Selasky #define S64_C(x) x ## LL
83abb14a54SHans Petter Selasky #define U64_C(x) x ## ULL
84abb14a54SHans Petter Selasky
85abb14a54SHans Petter Selasky #define BUG() panic("BUG at %s:%d", __FILE__, __LINE__)
86abb14a54SHans Petter Selasky #define BUG_ON(cond) do { \
87abb14a54SHans Petter Selasky if (cond) { \
88abb14a54SHans Petter Selasky panic("BUG ON %s failed at %s:%d", \
89abb14a54SHans Petter Selasky __stringify(cond), __FILE__, __LINE__); \
90abb14a54SHans Petter Selasky } \
91abb14a54SHans Petter Selasky } while (0)
92abb14a54SHans Petter Selasky
932390a144SHans Petter Selasky extern int linuxkpi_warn_dump_stack;
94abb14a54SHans Petter Selasky #define WARN_ON(cond) ({ \
95abb14a54SHans Petter Selasky bool __ret = (cond); \
96abb14a54SHans Petter Selasky if (__ret) { \
97abb14a54SHans Petter Selasky printf("WARNING %s failed at %s:%d\n", \
98abb14a54SHans Petter Selasky __stringify(cond), __FILE__, __LINE__); \
992390a144SHans Petter Selasky if (linuxkpi_warn_dump_stack) \
100a164a32bSAndriy Gapon linux_dump_stack(); \
101abb14a54SHans Petter Selasky } \
102abb14a54SHans Petter Selasky unlikely(__ret); \
103abb14a54SHans Petter Selasky })
104abb14a54SHans Petter Selasky
105abb14a54SHans Petter Selasky #define WARN_ON_SMP(cond) WARN_ON(cond)
106abb14a54SHans Petter Selasky
107abb14a54SHans Petter Selasky #define WARN_ON_ONCE(cond) ({ \
108abb14a54SHans Petter Selasky static bool __warn_on_once; \
109abb14a54SHans Petter Selasky bool __ret = (cond); \
110abb14a54SHans Petter Selasky if (__ret && !__warn_on_once) { \
111abb14a54SHans Petter Selasky __warn_on_once = 1; \
112abb14a54SHans Petter Selasky printf("WARNING %s failed at %s:%d\n", \
113abb14a54SHans Petter Selasky __stringify(cond), __FILE__, __LINE__); \
1142390a144SHans Petter Selasky if (linuxkpi_warn_dump_stack) \
115a164a32bSAndriy Gapon linux_dump_stack(); \
116abb14a54SHans Petter Selasky } \
117abb14a54SHans Petter Selasky unlikely(__ret); \
118abb14a54SHans Petter Selasky })
1198d59ecb2SHans Petter Selasky
120cb564d24SMark Johnston #define oops_in_progress SCHEDULER_STOPPED()
121cb564d24SMark Johnston
1228d59ecb2SHans Petter Selasky #undef ALIGN
1238d59ecb2SHans Petter Selasky #define ALIGN(x, y) roundup2((x), (y))
12497f2e93aSBjoern A. Zeeb #define ALIGN_DOWN(x, y) rounddown2(x, y)
1258d59ecb2SHans Petter Selasky #undef PTR_ALIGN
1268d59ecb2SHans Petter Selasky #define PTR_ALIGN(p, a) ((__typeof(p))ALIGN((uintptr_t)(p), (a)))
127c4e0746eSJohannes Lundberg #define IS_ALIGNED(x, a) (((x) & ((__typeof(x))(a) - 1)) == 0)
128bf05cd05SHans Petter Selasky #define __KERNEL_DIV_ROUND_UP(x, n) howmany(x, n)
1298d59ecb2SHans Petter Selasky #define FIELD_SIZEOF(t, f) sizeof(((t *)0)->f)
1308d59ecb2SHans Petter Selasky
131aef2a67bSHans Petter Selasky #define printk(...) printf(__VA_ARGS__)
132aef2a67bSHans Petter Selasky #define vprintk(f, a) vprintf(f, a)
133aef2a67bSHans Petter Selasky
134e0eeeca8SVladimir Kondratyev #define PTR_IF(x, p) ((x) ? (p) : NULL)
135e0eeeca8SVladimir Kondratyev
1360df8bab6SHans Petter Selasky #define asm __asm
1370df8bab6SHans Petter Selasky
138e35079dbSHans Petter Selasky extern void linux_dump_stack(void);
139e35079dbSHans Petter Selasky #define dump_stack() linux_dump_stack()
140e35079dbSHans Petter Selasky
141aef2a67bSHans Petter Selasky struct va_format {
142aef2a67bSHans Petter Selasky const char *fmt;
143aef2a67bSHans Petter Selasky va_list *va;
144aef2a67bSHans Petter Selasky };
145aef2a67bSHans Petter Selasky
146aef2a67bSHans Petter Selasky static inline int
vscnprintf(char * buf,size_t size,const char * fmt,va_list args)147aef2a67bSHans Petter Selasky vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
148aef2a67bSHans Petter Selasky {
149aef2a67bSHans Petter Selasky ssize_t ssize = size;
150aef2a67bSHans Petter Selasky int i;
151aef2a67bSHans Petter Selasky
152aef2a67bSHans Petter Selasky i = vsnprintf(buf, size, fmt, args);
153aef2a67bSHans Petter Selasky
154aef2a67bSHans Petter Selasky return ((i >= ssize) ? (ssize - 1) : i);
155aef2a67bSHans Petter Selasky }
156aef2a67bSHans Petter Selasky
157aef2a67bSHans Petter Selasky static inline int
scnprintf(char * buf,size_t size,const char * fmt,...)158aef2a67bSHans Petter Selasky scnprintf(char *buf, size_t size, const char *fmt, ...)
159aef2a67bSHans Petter Selasky {
160aef2a67bSHans Petter Selasky va_list args;
161aef2a67bSHans Petter Selasky int i;
162aef2a67bSHans Petter Selasky
163aef2a67bSHans Petter Selasky va_start(args, fmt);
164aef2a67bSHans Petter Selasky i = vscnprintf(buf, size, fmt, args);
165aef2a67bSHans Petter Selasky va_end(args);
166aef2a67bSHans Petter Selasky
167aef2a67bSHans Petter Selasky return (i);
168aef2a67bSHans Petter Selasky }
1698d59ecb2SHans Petter Selasky
1708d59ecb2SHans Petter Selasky /*
1718d59ecb2SHans Petter Selasky * The "pr_debug()" and "pr_devel()" macros should produce zero code
1728d59ecb2SHans Petter Selasky * unless DEBUG is defined:
1738d59ecb2SHans Petter Selasky */
1748d59ecb2SHans Petter Selasky #ifdef DEBUG
1754580f5eaSHans Petter Selasky extern int linuxkpi_debug;
1768d59ecb2SHans Petter Selasky #define pr_debug(fmt, ...) \
1774580f5eaSHans Petter Selasky do { \
1784580f5eaSHans Petter Selasky if (linuxkpi_debug) \
1794580f5eaSHans Petter Selasky log(LOG_DEBUG, fmt, ##__VA_ARGS__); \
1804580f5eaSHans Petter Selasky } while (0)
1818d59ecb2SHans Petter Selasky #define pr_devel(fmt, ...) \
1828d59ecb2SHans Petter Selasky log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
1838d59ecb2SHans Petter Selasky #else
1848d59ecb2SHans Petter Selasky #define pr_debug(fmt, ...) \
1858d59ecb2SHans Petter Selasky ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
1868d59ecb2SHans Petter Selasky #define pr_devel(fmt, ...) \
1878d59ecb2SHans Petter Selasky ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
1888d59ecb2SHans Petter Selasky #endif
1898d59ecb2SHans Petter Selasky
1908d59ecb2SHans Petter Selasky #ifndef pr_fmt
1918d59ecb2SHans Petter Selasky #define pr_fmt(fmt) fmt
1928d59ecb2SHans Petter Selasky #endif
1938d59ecb2SHans Petter Selasky
1948d59ecb2SHans Petter Selasky /*
1958d59ecb2SHans Petter Selasky * Print a one-time message (analogous to WARN_ONCE() et al):
1968d59ecb2SHans Petter Selasky */
1978d59ecb2SHans Petter Selasky #define printk_once(...) do { \
1988d59ecb2SHans Petter Selasky static bool __print_once; \
1998d59ecb2SHans Petter Selasky \
2008d59ecb2SHans Petter Selasky if (!__print_once) { \
2018d59ecb2SHans Petter Selasky __print_once = true; \
2028d59ecb2SHans Petter Selasky printk(__VA_ARGS__); \
2038d59ecb2SHans Petter Selasky } \
2048d59ecb2SHans Petter Selasky } while (0)
2058d59ecb2SHans Petter Selasky
2068d59ecb2SHans Petter Selasky /*
2078d59ecb2SHans Petter Selasky * Log a one-time message (analogous to WARN_ONCE() et al):
2088d59ecb2SHans Petter Selasky */
2098d59ecb2SHans Petter Selasky #define log_once(level,...) do { \
2108d59ecb2SHans Petter Selasky static bool __log_once; \
2118d59ecb2SHans Petter Selasky \
212abb14a54SHans Petter Selasky if (unlikely(!__log_once)) { \
2138d59ecb2SHans Petter Selasky __log_once = true; \
2148d59ecb2SHans Petter Selasky log(level, __VA_ARGS__); \
2158d59ecb2SHans Petter Selasky } \
2168d59ecb2SHans Petter Selasky } while (0)
2178d59ecb2SHans Petter Selasky
2188d59ecb2SHans Petter Selasky #define pr_emerg(fmt, ...) \
2198d59ecb2SHans Petter Selasky log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
2208d59ecb2SHans Petter Selasky #define pr_alert(fmt, ...) \
2218d59ecb2SHans Petter Selasky log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
2228d59ecb2SHans Petter Selasky #define pr_crit(fmt, ...) \
2238d59ecb2SHans Petter Selasky log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
2248d59ecb2SHans Petter Selasky #define pr_err(fmt, ...) \
2258d59ecb2SHans Petter Selasky log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
22605c2d94aSGreg V #define pr_err_once(fmt, ...) \
22705c2d94aSGreg V log_once(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
2288d59ecb2SHans Petter Selasky #define pr_warning(fmt, ...) \
2298d59ecb2SHans Petter Selasky log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
230abb14a54SHans Petter Selasky #define pr_warn(...) \
231abb14a54SHans Petter Selasky pr_warning(__VA_ARGS__)
232abb14a54SHans Petter Selasky #define pr_warn_once(fmt, ...) \
233abb14a54SHans Petter Selasky log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
2348d59ecb2SHans Petter Selasky #define pr_notice(fmt, ...) \
2358d59ecb2SHans Petter Selasky log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
2368d59ecb2SHans Petter Selasky #define pr_info(fmt, ...) \
2378d59ecb2SHans Petter Selasky log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
2388d59ecb2SHans Petter Selasky #define pr_info_once(fmt, ...) \
2398d59ecb2SHans Petter Selasky log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
2408d59ecb2SHans Petter Selasky #define pr_cont(fmt, ...) \
2418d59ecb2SHans Petter Selasky printk(KERN_CONT fmt, ##__VA_ARGS__)
242e9db3df2SHans Petter Selasky #define pr_warn_ratelimited(...) do { \
2438f7eee5aSHans Petter Selasky static linux_ratelimit_t __ratelimited; \
244e9db3df2SHans Petter Selasky if (linux_ratelimited(&__ratelimited)) \
245e9db3df2SHans Petter Selasky pr_warning(__VA_ARGS__); \
246e9db3df2SHans Petter Selasky } while (0)
2478d59ecb2SHans Petter Selasky
2488d59ecb2SHans Petter Selasky #ifndef WARN
249abb14a54SHans Petter Selasky #define WARN(condition, ...) ({ \
250abb14a54SHans Petter Selasky bool __ret_warn_on = (condition); \
2518d59ecb2SHans Petter Selasky if (unlikely(__ret_warn_on)) \
252abb14a54SHans Petter Selasky pr_warning(__VA_ARGS__); \
253abb14a54SHans Petter Selasky unlikely(__ret_warn_on); \
254abb14a54SHans Petter Selasky })
255abb14a54SHans Petter Selasky #endif
256abb14a54SHans Petter Selasky
257abb14a54SHans Petter Selasky #ifndef WARN_ONCE
258abb14a54SHans Petter Selasky #define WARN_ONCE(condition, ...) ({ \
259abb14a54SHans Petter Selasky bool __ret_warn_on = (condition); \
260abb14a54SHans Petter Selasky if (unlikely(__ret_warn_on)) \
261abb14a54SHans Petter Selasky pr_warn_once(__VA_ARGS__); \
2628d59ecb2SHans Petter Selasky unlikely(__ret_warn_on); \
2638d59ecb2SHans Petter Selasky })
2648d59ecb2SHans Petter Selasky #endif
2658d59ecb2SHans Petter Selasky
2668d59ecb2SHans Petter Selasky #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
2678d59ecb2SHans Petter Selasky
268ee7c3198SMark Johnston #define u64_to_user_ptr(val) ((void *)(uintptr_t)(val))
269ee7c3198SMark Johnston
270fee0d486SNeel Chauhan #define _RET_IP_ __builtin_return_address(0)
271fee0d486SNeel Chauhan
272355711eaSEmmanuel Vadot #define offsetofend(t, m) \
273355711eaSEmmanuel Vadot (offsetof(t, m) + sizeof((((t *)0)->m)))
274355711eaSEmmanuel Vadot
275fe68f570SHans Petter Selasky #define smp_processor_id() PCPU_GET(cpuid)
2768d59ecb2SHans Petter Selasky #define num_possible_cpus() mp_ncpus
2778d59ecb2SHans Petter Selasky #define num_online_cpus() mp_ncpus
27803219fbaSHans Petter Selasky
27903219fbaSHans Petter Selasky #if defined(__i386__) || defined(__amd64__)
28003219fbaSHans Petter Selasky extern bool linux_cpu_has_clflush;
28103219fbaSHans Petter Selasky #define cpu_has_clflush linux_cpu_has_clflush
28203219fbaSHans Petter Selasky #endif
2838d59ecb2SHans Petter Selasky
2848f7eee5aSHans Petter Selasky typedef struct linux_ratelimit {
2858f7eee5aSHans Petter Selasky struct timeval lasttime;
2868f7eee5aSHans Petter Selasky int counter;
2878f7eee5aSHans Petter Selasky } linux_ratelimit_t;
2888f7eee5aSHans Petter Selasky
2898f7eee5aSHans Petter Selasky static inline bool
linux_ratelimited(linux_ratelimit_t * rl)2908f7eee5aSHans Petter Selasky linux_ratelimited(linux_ratelimit_t *rl)
2918f7eee5aSHans Petter Selasky {
2928f7eee5aSHans Petter Selasky return (ppsratecheck(&rl->lasttime, &rl->counter, 1));
2938f7eee5aSHans Petter Selasky }
294e9db3df2SHans Petter Selasky
295d053391cSHans Petter Selasky #define __is_constexpr(x) \
296d053391cSHans Petter Selasky __builtin_constant_p(x)
297d053391cSHans Petter Selasky
298d326a6c7SHans Petter Selasky /*
299d326a6c7SHans Petter Selasky * The is_signed() macro below returns true if the passed data type is
300d326a6c7SHans Petter Selasky * signed. Else false is returned.
301d326a6c7SHans Petter Selasky */
302d326a6c7SHans Petter Selasky #define is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0)
303d0eed838SHans Petter Selasky
3040e4e9e8fSEmmanuel Vadot #define TAINT_WARN 0
3050e4e9e8fSEmmanuel Vadot #define test_taint(x) (0)
306bf27839aSEmmanuel Vadot #define add_taint(x,y) do { \
307bf27839aSEmmanuel Vadot } while (0)
3080e4e9e8fSEmmanuel Vadot
309deb9bfbdSBjoern A. Zeeb static inline int
_h2b(const char c)310deb9bfbdSBjoern A. Zeeb _h2b(const char c)
311deb9bfbdSBjoern A. Zeeb {
312deb9bfbdSBjoern A. Zeeb
313deb9bfbdSBjoern A. Zeeb if (c >= '0' && c <= '9')
314deb9bfbdSBjoern A. Zeeb return (c - '0');
315deb9bfbdSBjoern A. Zeeb if (c >= 'a' && c <= 'f')
316deb9bfbdSBjoern A. Zeeb return (10 + c - 'a');
317deb9bfbdSBjoern A. Zeeb if (c >= 'A' && c <= 'F')
318deb9bfbdSBjoern A. Zeeb return (10 + c - 'A');
319deb9bfbdSBjoern A. Zeeb return (-EINVAL);
320deb9bfbdSBjoern A. Zeeb }
321deb9bfbdSBjoern A. Zeeb
322deb9bfbdSBjoern A. Zeeb static inline int
hex2bin(uint8_t * bindst,const char * hexsrc,size_t binlen)323deb9bfbdSBjoern A. Zeeb hex2bin(uint8_t *bindst, const char *hexsrc, size_t binlen)
324deb9bfbdSBjoern A. Zeeb {
325deb9bfbdSBjoern A. Zeeb int hi4, lo4;
326deb9bfbdSBjoern A. Zeeb
327deb9bfbdSBjoern A. Zeeb while (binlen > 0) {
328deb9bfbdSBjoern A. Zeeb hi4 = _h2b(*hexsrc++);
329deb9bfbdSBjoern A. Zeeb lo4 = _h2b(*hexsrc++);
330deb9bfbdSBjoern A. Zeeb if (hi4 < 0 || lo4 < 0)
331deb9bfbdSBjoern A. Zeeb return (-EINVAL);
332deb9bfbdSBjoern A. Zeeb
333deb9bfbdSBjoern A. Zeeb *bindst++ = (hi4 << 4) | lo4;
334deb9bfbdSBjoern A. Zeeb binlen--;
335deb9bfbdSBjoern A. Zeeb }
336deb9bfbdSBjoern A. Zeeb
337deb9bfbdSBjoern A. Zeeb return (0);
338deb9bfbdSBjoern A. Zeeb }
339deb9bfbdSBjoern A. Zeeb
340f82cf28bSBjoern A. Zeeb static inline bool
mac_pton(const char * macin,uint8_t * macout)341f82cf28bSBjoern A. Zeeb mac_pton(const char *macin, uint8_t *macout)
342f82cf28bSBjoern A. Zeeb {
343f82cf28bSBjoern A. Zeeb const char *s, *d;
344*b4856b8eSZhenlei Huang uint8_t mac[6], hx, lx;
345f82cf28bSBjoern A. Zeeb int i;
346f82cf28bSBjoern A. Zeeb
347f82cf28bSBjoern A. Zeeb if (strlen(macin) < (3 * 6 - 1))
348f82cf28bSBjoern A. Zeeb return (false);
349f82cf28bSBjoern A. Zeeb
350f82cf28bSBjoern A. Zeeb i = 0;
351f82cf28bSBjoern A. Zeeb s = macin;
352f82cf28bSBjoern A. Zeeb do {
353f82cf28bSBjoern A. Zeeb /* Should we also support '-'-delimiters? */
354f82cf28bSBjoern A. Zeeb d = strchrnul(s, ':');
355f82cf28bSBjoern A. Zeeb hx = lx = 0;
356f82cf28bSBjoern A. Zeeb while (s < d) {
357f82cf28bSBjoern A. Zeeb /* Fail on abc:123:xxx:... */
358f82cf28bSBjoern A. Zeeb if ((d - s) > 2)
359f82cf28bSBjoern A. Zeeb return (false);
360f82cf28bSBjoern A. Zeeb /* We do support non-well-formed strings: 3:45:6:... */
361f82cf28bSBjoern A. Zeeb if ((d - s) > 1) {
362f82cf28bSBjoern A. Zeeb hx = _h2b(*s);
363f82cf28bSBjoern A. Zeeb if (hx < 0)
364f82cf28bSBjoern A. Zeeb return (false);
365f82cf28bSBjoern A. Zeeb s++;
366f82cf28bSBjoern A. Zeeb }
367f82cf28bSBjoern A. Zeeb lx = _h2b(*s);
368f82cf28bSBjoern A. Zeeb if (lx < 0)
369f82cf28bSBjoern A. Zeeb return (false);
370f82cf28bSBjoern A. Zeeb s++;
371f82cf28bSBjoern A. Zeeb }
372f82cf28bSBjoern A. Zeeb mac[i] = (hx << 4) | lx;
373f82cf28bSBjoern A. Zeeb i++;
374f82cf28bSBjoern A. Zeeb if (i >= 6)
375f82cf28bSBjoern A. Zeeb return (false);
376f82cf28bSBjoern A. Zeeb } while (d != NULL && *d != '\0');
377f82cf28bSBjoern A. Zeeb
378f82cf28bSBjoern A. Zeeb memcpy(macout, mac, 6);
379f82cf28bSBjoern A. Zeeb return (true);
380f82cf28bSBjoern A. Zeeb }
381f82cf28bSBjoern A. Zeeb
382c840d5ceSBjoern A. Zeeb #define DECLARE_FLEX_ARRAY(_t, _n) \
383fab601c5SBjoern A. Zeeb struct { struct { } __dummy_ ## _n; _t _n[0]; }
384c840d5ceSBjoern A. Zeeb
385307f78f3SVladimir Kondratyev #endif /* _LINUXKPI_LINUX_KERNEL_H_ */
386