11# define _LOOSE_KERNEL_NAMES 1
12# undef __KERNEL_STRICT_NAMES
14# define __OPTIMIZE__ 1
16# include <click/cxxprotect.h>
18# ifdef WANT_MOD_USE_COUNT
19# define __NO_VERSION__
20# include <linux/module.h>
21# define HAVE_MOD_USE_COUNT 1
23# include <linux/kernel.h>
24# include <linux/string.h>
25# include <linux/skbuff.h>
26# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
27# include <linux/malloc.h>
28# include <linux/vmalloc.h>
29# include <linux/interrupt.h>
31# include <linux/hardirq.h>
33# include <linux/ctype.h>
34# include <linux/time.h>
35# include <linux/errno.h>
36# include <linux/delay.h>
38# include <click/cxxunprotect.h>
42# include <click/cxxprotect.h>
44# include <sys/ctype.h>
45# include <sys/systm.h>
47# include <sys/param.h>
48# include <sys/kernel.h>
50# include <sys/malloc.h>
51# include <sys/libkern.h>
53# include <sys/sysproto.h>
54# include <sys/limits.h>
55# include <sys/module.h>
57# include <click/cxxunprotect.h>
61# include <click/cxxprotect.h>
72# include <sys/socket.h>
73# include <netinet/in.h>
76# include <click/cxxunprotect.h>
90# include <sys/socket.h>
91# include <netinet/in.h>
94extern "C" int simclick_gettimeofday(
struct timeval *);
101#if HAVE_DPDK && !CLICK_TOOL
103 #include <rte_cycles.h>
106#if defined(__MACH__) && defined(__APPLE__)
107# include <mach/mach_time.h>
115void click_chatter(
const char *fmt, ...);
121#if CLICK_DMALLOC && (CLICK_LINUXMODULE || CLICK_BSDMODULE || CLICK_MINIOS)
122extern uint32_t click_dmalloc_where;
123# define CLICK_DMALLOC_REG(s) do { const unsigned char *__str = reinterpret_cast<const unsigned char *>(s); click_dmalloc_where = (__str[0]<<24) | (__str[1]<<16) | (__str[2]<<8) | __str[3]; } while (0)
125# define CLICK_DMALLOC_REG(s)
132# define CLICK_DEFAULT_ALIGNMENT ARCH_KMALLOC_MINALIGN
133# define CLICK_LALLOC(size) (click_lalloc((size)))
134# define CLICK_LFREE(p, size) (click_lfree((p), (size)))
136void *click_lalloc(
size_t size);
137void click_lfree(
volatile void *p,
size_t size);
140# ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
141# define CLICK_DEFAULT_ALIGNMENT __STDCPP_DEFAULT_NEW_ALIGNMENT__
143# define CLICK_DEFAULT_ALIGNMENT 16
145# define CLICK_LALLOC(size) ((void *)(new uint8_t[(size)]))
146# define CLICK_LFREE(p, size) delete[] ((void) (size), (uint8_t *)(p))
149# if HAVE_ALIGNED_ALLOC
150# define CLICK_ALIGNED_ALLOC(size, ...) (aligned_alloc(CLICK_CACHE_LINE_SIZE, size))
151# define CLICK_ALIGNED_ALLOC_T(size,align) (aligned_alloc(align, size))
152# define CLICK_ALIGNED_FREE(p,size) (free(p))
154# ifndef CLICK_LINUXMODULE
155# warning Using normal allocation instead of aligned one, please use a compiler that supports aligned_alloc
157# define CLICK_ALIGNED_ALLOC(size) CLICK_LALLOC(size)
158# define CLICK_ALIGNED_ALLOC_T(size,align) CLICK_LALLOC(size)
159# define CLICK_ALIGNED_FREE(p,size) CLICK_LFREE(p,size);
163# define CLICK_ALIGNED_NEW(T,n) (new T[n])
164# define CLICK_ALIGNED_DELETE(p,T,n) (delete[] p)
166# define CLICK_ALIGNED_NEW(T,n) ({T* v = (T*)CLICK_ALIGNED_ALLOC(sizeof(T) * (n));for (unsigned i = 0; i < (n); i++) {new(&v[i]) T();};v;})
167# define CLICK_ALIGNED_DELETE(p,T,n) {for (unsigned i = 0; i < (n); i++) p[i].~T();CLICK_ALIGNED_FREE(p,sizeof(T)*(n));}
170#define CLICK_ASSERT_ALIGNED(p) (assert(((intptr_t)p & (intptr_t)(CLICK_CACHE_LINE_SIZE - 1)) == 0))
176#define assume(cond) do { if (!(cond)) __builtin_unreachable(); } while (0)
179extern bool dpdk_enabled;
185uint32_t click_random();
190uint32_t click_random(uint32_t low, uint32_t high);
194uint32_t click_random(uint32_t high);
197void click_srandom(uint32_t seed);
201void click_random_srandom();
204# define CLICK_RAND_MAX 0x7FFFFFFFU
205#elif !CLICK_LINUXMODULE && RAND_MAX >= 0x7FFFFFFFU
206# define CLICK_RAND_MAX RAND_MAX
208# define CLICK_RAND_MAX 0x7FFFFFFFU
209extern uint32_t click_random_seed;
213extern uint32_t click_random();
215inline uint32_t click_random() {
218# elif CLICK_LINUXMODULE
219 click_random_seed = click_random_seed * 69069L + 5;
220 return (click_random_seed ^ jiffies) & CLICK_RAND_MAX;
223# elif HAVE_RANDOM && CLICK_RAND_MAX == RAND_MAX
232inline void click_srandom(uint32_t seed) {
235#elif CLICK_LINUXMODULE
236 click_random_seed = seed;
241#elif HAVE_RANDOM && CLICK_RAND_MAX == RAND_MAX
285int click_qsort(
void *base,
size_t n,
size_t size,
286 int (*compar)(
const void *a,
const void *b,
void *user_data),
287 void *user_data = 0);
298int click_qsort(
void *base,
size_t n,
size_t size,
299 int (*compar)(
const void *a,
const void *b)) CLICK_DEPRECATED;
304template <
typename T>
int click_compare(
const void *a,
const void *b,
void *)
306 const T *ta =
static_cast<const T *
>(a);
307 const T *tb =
static_cast<const T *
>(b);
308 return (*ta < *tb ? -1 : (*tb < *ta ? 1 : 0));
312template <
typename T>
int click_qsort(T *base,
size_t n)
314 return click_qsort(base, n,
sizeof(T), (
int (*)(
const void *,
const void *,
void *)) &click_compare<T>);
324long strtol(
const char *,
char **,
int);
327strtoul(
const char *nptr,
char **endptr,
int base)
329 return simple_strtoul(nptr, endptr, base);
332# if __GNUC__ == 2 && __GNUC_MINOR__ == 96
333int click_strcmp(
const char *,
const char *);
336strcmp(
const char *a,
const char *b)
338 return click_strcmp(a, b);
357extern unsigned char _ctype[];
359# define __ismask(x) (_ctype[(int)(unsigned char)(x)])
360# define isalnum(c) ((__ismask(c)&(_U|_L|_D)) != 0)
362# define strchr(s, c) index(s, c)
364# if __FreeBSD_version >= 700000 && __FreeBSD_version < 730000
366extern "C" void *memmove(
void *dest,
const void *src,
size_t len);
370typedef struct ifnet net_device;
375typedef struct device net_device;
382#if (!defined(__cplusplus) || !HAVE_CXX_STATIC_ASSERT) && !defined(static_assert)
383# define static_assert(x, ...) switch ((int) (x)) case 0: case !!((int) (x)):
390typedef uint32_t click_processor_t;
391# define CLICK_CPU_MAX NR_CPUS
392#elif CLICK_USERLEVEL && HAVE_MULTITHREAD
393typedef pthread_t click_processor_t;
394# define CLICK_CPU_MAX 256
396typedef int8_t click_processor_t;
398# define CLICK_CPU_MAX 256
400# define CLICK_CPU_MAX 1
404inline click_processor_t
408# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
411 return current->processor;
413#elif CLICK_USERLEVEL && HAVE_MULTITHREAD
414 return pthread_self();
424# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
425# ifdef put_cpu_no_resched
426 put_cpu_no_resched();
434#if CLICK_USERLEVEL && HAVE_MULTITHREAD && HAVE___THREAD_STORAGE_CLASS
435extern __thread
int click_current_thread_id;
439extern int click_nthreads;
442inline click_processor_t
443click_current_processor()
446# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
447 return smp_processor_id();
449 return current->processor;
451#elif CLICK_USERLEVEL && HAVE_MULTITHREAD
452 return pthread_self();
459click_current_cpu_id()
464# if HAVE___THREAD_STORAGE_CLASS
465 return click_current_thread_id & 0xffff;
467 return sched_getcpu();
470 return click_current_processor();
482#elif CLICK_USERLEVEL && HAVE___THREAD_STORAGE_CLASS
483 return click_nthreads;
485 return CLICK_CPU_MAX;
490inline click_processor_t
491click_invalid_processor()
495#elif CLICK_USERLEVEL && HAVE_MULTITHREAD
508# define click_gettimeofday(tvp) (do_gettimeofday(tvp))
509typedef unsigned long click_jiffies_t;
510typedef long click_jiffies_difference_t;
511# define click_jiffies() (jiffies)
512# define CLICK_JIFFIES_MONOTONIC 1
514# define click_jiffies_less(a, b) ((click_jiffies_difference_t) ((a) - (b)) < 0)
515# define HAS_LONG_CLICK_JIFFIES_T 1
517# define click_gettimeofday(tvp) (getmicrotime(tvp))
518typedef unsigned click_jiffies_t;
519typedef int click_jiffies_difference_t;
520# define click_jiffies() (ticks)
522# define click_jiffies_less(a, b) ((click_jiffies_difference_t) ((a) - (b)) < 0)
525void click_gettimeofday(timeval *tvp) CLICK_DEPRECATED;
526typedef long unsigned click_jiffies_t;
527typedef long int click_jiffies_difference_t;
528click_jiffies_t click_timestamp_jiffies(
void*);
530typedef click_jiffies_t (*click_jiffies_fct_t)(
void*);
531extern click_jiffies_fct_t click_jiffies_fct;
532extern void* click_jiffies_fct_data;
533# define click_jiffies() (click_jiffies_fct(click_jiffies_fct_data))
534# define CLICK_HZ 1000
536# define click_jiffies() (click_timestamp_jiffies(0))
537# define CLICK_HZ 1000
539# define HAS_LONG_CLICK_JIFFIES_T 1
540# define click_jiffies_less(a, b) ((click_jiffies_difference_t) ((a) - (b)) < 0)
544#if SIZEOF_CLICK_JIFFIES_T != (HAS_LONG_CLICK_JIFFIES_T ? SIZEOF_LONG : SIZEOF_INT)
545# error "SIZEOF_CLICK_JIFFIES_T declared incorrectly"
549#define sleep(ms) ssleep(ms)
557# define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
558# define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
559# define timercmp(a, b, CMP) \
560 (((a)->tv_sec == (b)->tv_sec) ? \
561 ((a)->tv_usec CMP (b)->tv_usec) : \
562 ((a)->tv_sec CMP (b)->tv_sec))
565# define timeradd(a, b, result) \
567 (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
568 (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
569 if ((result)->tv_usec >= 1000000) \
571 ++(result)->tv_sec; \
572 (result)->tv_usec -= 1000000; \
577# define timersub(a, b, result) \
579 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
580 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
581 if ((result)->tv_usec < 0) { \
582 --(result)->tv_sec; \
583 (result)->tv_usec += 1000000; \
588#ifndef CLICK_TIMEVAL_OPERATORS
590inline timeval make_timeval(
int sec,
int usec) CLICK_DEPRECATED;
591inline bool operator==(
const timeval &a,
const timeval &b) CLICK_DEPRECATED;
592inline bool operator!=(
const timeval &a,
const timeval &b) CLICK_DEPRECATED;
593inline bool operator<(
const timeval &a,
const timeval &b) CLICK_DEPRECATED;
594inline bool operator<=(
const timeval &a,
const timeval &b) CLICK_DEPRECATED;
595inline bool operator>(
const timeval &a,
const timeval &b) CLICK_DEPRECATED;
596inline bool operator>=(
const timeval &a,
const timeval &b) CLICK_DEPRECATED;
597inline timeval &operator+=(timeval &a,
const timeval &b) CLICK_DEPRECATED;
598inline timeval &operator-=(timeval &a,
const timeval &b) CLICK_DEPRECATED;
599inline timeval operator+(timeval a,
const timeval &b) CLICK_DEPRECATED;
600inline timeval operator-(timeval a,
const timeval &b) CLICK_DEPRECATED;
603make_timeval(int sec, int usec)
612operator==(
const struct timeval &a,
const struct timeval &b)
614 return a.tv_sec == b.tv_sec && a.tv_usec == b.tv_usec;
618operator!=(
const struct timeval &a,
const struct timeval &b)
620 return a.tv_sec != b.tv_sec || a.tv_usec != b.tv_usec;
624operator<(
const struct timeval &a,
const struct timeval &b)
626 return a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_usec < b.tv_usec);
630operator<=(
const struct timeval &a,
const struct timeval &b)
632 return a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_usec <= b.tv_usec);
636operator>=(
const struct timeval &a,
const struct timeval &b)
638 return a.tv_sec > b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_usec >= b.tv_usec);
642operator>(
const struct timeval &a,
const struct timeval &b)
644 return a.tv_sec > b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_usec > b.tv_usec);
647inline struct timeval &
648operator+=(
struct timeval &a,
const struct timeval &b)
650 timeradd(&a, &b, &a);
654inline struct timeval &
655operator-=(
struct timeval &a,
const struct timeval &b)
657 timersub(&a, &b, &a);
662operator+(struct timeval a, const struct timeval &b)
664 timeradd(&a, &b, &a);
669operator-(struct timeval a, const struct timeval &b)
671 timersub(&a, &b, &a);
686# if CLICK_BYTE_ORDER == CLICK_LITTLE_ENDIAN
687# define le16_to_cpu(x) (x)
688# define cpu_to_le16(x) (x)
689# define le32_to_cpu(x) (x)
690# define cpu_to_le32(x) (x)
691# elif CLICK_BYTE_ORDER == CLICK_BIG_ENDIAN && defined(__APPLE__)
692# include <machine/byte_order.h>
693# define le16_to_cpu(x) NXSwapShort((x))
694# define cpu_to_le16(x) NXSwapShort((x))
695# define le32_to_cpu(x) NXSwapInt((x))
696# define cpu_to_le32(x) NXSwapInt((x))
697# elif CLICK_BYTE_ORDER == CLICK_BIG_ENDIAN && HAVE_BYTESWAP_H
698# include <byteswap.h>
699# define le16_to_cpu(x) bswap_16((x))
700# define cpu_to_le16(x) bswap_16((x))
701# define le32_to_cpu(x) bswap_32((x))
702# define cpu_to_le32(x) bswap_32((x))
703# elif CLICK_BYTE_ORDER == CLICK_BIG_ENDIAN
704# define le16_to_cpu(x) ((((x) & 0x00ff) << 8) | (((x) & 0xff00) >> 8))
705# define cpu_to_le16(x) le16_to_cpu((x))
706# define le32_to_cpu(x) (le16_to_cpu((x) >> 16) | (le16_to_cpu(x) << 16))
707# define cpu_to_le32(x) le32_to_cpu((x))
719typedef uint64_t click_cycles_t;
721typedef uint32_t click_cycles_t;
727#if defined(__MACH__) && defined(__APPLE__)
728 return mach_absolute_time();
729#elif CLICK_LINUXMODULE && HAVE_INT64_TYPES && __i386__
731 __asm__ __volatile__ (
"rdtsc" :
"=A" (x));
733#elif CLICK_LINUXMODULE && HAVE_INT64_TYPES && __x86_64__
735 __asm__ __volatile__ (
"rdtsc" :
"=a" (xlo),
"=d" (xhi));
736 return xlo | (((uint64_t) xhi) << 32);
737#elif CLICK_LINUXMODULE && __i386__
739 __asm__ __volatile__ (
"rdtsc" :
"=a" (xlo),
"=d" (xhi));
743#elif CLICK_USERLEVEL && HAVE_INT64_TYPES && __i386__
745 __asm__ __volatile__ (
"rdtsc" :
"=A" (x));
747#elif CLICK_USERLEVEL && HAVE_INT64_TYPES && __x86_64__
749 __asm__ __volatile__ (
"rdtsc" :
"=a" (xlo),
"=d" (xhi));
750 return xlo | (((uint64_t) xhi) << 32);
751#elif CLICK_USERLEVEL && __i386__
753 __asm__ __volatile__ (
"rdtsc" :
"=a" (xlo),
"=d" (xhi));
755#elif CLICK_USERLEVEL && __aarch64__
757 asm volatile(
"mrs %0, cntvct_el0" :
"=r" (val));
759#elif CLICK_USERLEVEL && HAVE_DPDK && defined(_RTE_CYCLES_H_)
761 return rte_get_tsc_cycles();
763 #error "click_get_cycles is not implemented for your architecture!"
771extern click_cycles_t click_cycles_hz;
773inline click_cycles_t cycles_hz() {
774#if HAVE_DPDK && !CLICK_TOOL
775 if (likely(dpdk_enabled)) {
776 return rte_get_timer_hz();
779#elif defined(__MACH__) && defined(__APPLE__)
780 mach_timebase_info_data_t _clock_timebase;
781 mach_timebase_info(&_clock_timebase);
782 return _clock_timebase.denom / _clock_timebase.numer;
784 if (click_cycles_hz == 0) {
785 click_cycles_t tsc_freq = click_get_cycles();
787 click_cycles_hz = (click_get_cycles() - tsc_freq) * 10;
789 return click_cycles_hz;
799inline unsigned long long htonll(
unsigned long long src) {
800 static int typ = TYPE_INIT;
803 unsigned long long ull;
807 if (typ == TYPE_INIT) {
809 typ = (x.c[7] == 0x01ULL) ? TYPE_BIGEND : TYPE_LITEND;
812 if (typ == TYPE_BIGEND)
816 c = x.c[0]; x.c[0] = x.c[7]; x.c[7] = c;
817 c = x.c[1]; x.c[1] = x.c[6]; x.c[6] = c;
818 c = x.c[2]; x.c[2] = x.c[5]; x.c[5] = c;
819 c = x.c[3]; x.c[3] = x.c[4]; x.c[4] = c;
Efficiently build up Strings from pieces.
Definition straccum.hh:28
friend StringAccum & operator<<(StringAccum &sa, const String &str)
Append the contents of str to sa.
Definition straccum.hh:601