xdp_sample.bpf.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-License-Identifier: GPL-2.0
  2. #ifndef _XDP_SAMPLE_BPF_H
  3. #define _XDP_SAMPLE_BPF_H
  4. #include "vmlinux.h"
  5. #include <bpf/bpf_tracing.h>
  6. #include <bpf/bpf_core_read.h>
  7. #include <bpf/bpf_helpers.h>
  8. #include "net_shared.h"
  9. #include "xdp_sample_shared.h"
  10. #define EINVAL 22
  11. #define ENETDOWN 100
  12. #define EMSGSIZE 90
  13. #define EOPNOTSUPP 95
  14. #define ENOSPC 28
  15. typedef struct {
  16. __uint(type, BPF_MAP_TYPE_ARRAY);
  17. __uint(map_flags, BPF_F_MMAPABLE);
  18. __type(key, unsigned int);
  19. __type(value, struct datarec);
  20. } array_map;
  21. extern array_map rx_cnt;
  22. extern const volatile int nr_cpus;
  23. enum {
  24. XDP_REDIRECT_SUCCESS = 0,
  25. XDP_REDIRECT_ERROR = 1
  26. };
  27. static __always_inline void swap_src_dst_mac(void *data)
  28. {
  29. unsigned short *p = data;
  30. unsigned short dst[3];
  31. dst[0] = p[0];
  32. dst[1] = p[1];
  33. dst[2] = p[2];
  34. p[0] = p[3];
  35. p[1] = p[4];
  36. p[2] = p[5];
  37. p[3] = dst[0];
  38. p[4] = dst[1];
  39. p[5] = dst[2];
  40. }
  41. /*
  42. * Note: including linux/compiler.h or linux/kernel.h for the macros below
  43. * conflicts with vmlinux.h include in BPF files, so we define them here.
  44. *
  45. * Following functions are taken from kernel sources and
  46. * break aliasing rules in their original form.
  47. *
  48. * While kernel is compiled with -fno-strict-aliasing,
  49. * perf uses -Wstrict-aliasing=3 which makes build fail
  50. * under gcc 4.4.
  51. *
  52. * Using extra __may_alias__ type to allow aliasing
  53. * in this case.
  54. */
  55. typedef __u8 __attribute__((__may_alias__)) __u8_alias_t;
  56. typedef __u16 __attribute__((__may_alias__)) __u16_alias_t;
  57. typedef __u32 __attribute__((__may_alias__)) __u32_alias_t;
  58. typedef __u64 __attribute__((__may_alias__)) __u64_alias_t;
  59. static __always_inline void __read_once_size(const volatile void *p, void *res, int size)
  60. {
  61. switch (size) {
  62. case 1: *(__u8_alias_t *) res = *(volatile __u8_alias_t *) p; break;
  63. case 2: *(__u16_alias_t *) res = *(volatile __u16_alias_t *) p; break;
  64. case 4: *(__u32_alias_t *) res = *(volatile __u32_alias_t *) p; break;
  65. case 8: *(__u64_alias_t *) res = *(volatile __u64_alias_t *) p; break;
  66. default:
  67. asm volatile ("" : : : "memory");
  68. __builtin_memcpy((void *)res, (const void *)p, size);
  69. asm volatile ("" : : : "memory");
  70. }
  71. }
  72. static __always_inline void __write_once_size(volatile void *p, void *res, int size)
  73. {
  74. switch (size) {
  75. case 1: *(volatile __u8_alias_t *) p = *(__u8_alias_t *) res; break;
  76. case 2: *(volatile __u16_alias_t *) p = *(__u16_alias_t *) res; break;
  77. case 4: *(volatile __u32_alias_t *) p = *(__u32_alias_t *) res; break;
  78. case 8: *(volatile __u64_alias_t *) p = *(__u64_alias_t *) res; break;
  79. default:
  80. asm volatile ("" : : : "memory");
  81. __builtin_memcpy((void *)p, (const void *)res, size);
  82. asm volatile ("" : : : "memory");
  83. }
  84. }
  85. #define READ_ONCE(x) \
  86. ({ \
  87. union { typeof(x) __val; char __c[1]; } __u = \
  88. { .__c = { 0 } }; \
  89. __read_once_size(&(x), __u.__c, sizeof(x)); \
  90. __u.__val; \
  91. })
  92. #define WRITE_ONCE(x, val) \
  93. ({ \
  94. union { typeof(x) __val; char __c[1]; } __u = \
  95. { .__val = (val) }; \
  96. __write_once_size(&(x), __u.__c, sizeof(x)); \
  97. __u.__val; \
  98. })
  99. /* Add a value using relaxed read and relaxed write. Less expensive than
  100. * fetch_add when there is no write concurrency.
  101. */
  102. #define NO_TEAR_ADD(x, val) WRITE_ONCE((x), READ_ONCE(x) + (val))
  103. #define NO_TEAR_INC(x) NO_TEAR_ADD((x), 1)
  104. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  105. #endif