internal.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __PACKET_INTERNAL_H__
  3. #define __PACKET_INTERNAL_H__
  4. #include <linux/refcount.h>
  5. struct packet_mclist {
  6. struct packet_mclist *next;
  7. int ifindex;
  8. int count;
  9. unsigned short type;
  10. unsigned short alen;
  11. unsigned char addr[MAX_ADDR_LEN];
  12. };
  13. /* kbdq - kernel block descriptor queue */
  14. struct tpacket_kbdq_core {
  15. struct pgv *pkbdq;
  16. unsigned int feature_req_word;
  17. unsigned int hdrlen;
  18. unsigned char reset_pending_on_curr_blk;
  19. unsigned char delete_blk_timer;
  20. unsigned short kactive_blk_num;
  21. unsigned short blk_sizeof_priv;
  22. /* last_kactive_blk_num:
  23. * trick to see if user-space has caught up
  24. * in order to avoid refreshing timer when every single pkt arrives.
  25. */
  26. unsigned short last_kactive_blk_num;
  27. char *pkblk_start;
  28. char *pkblk_end;
  29. int kblk_size;
  30. unsigned int max_frame_len;
  31. unsigned int knum_blocks;
  32. uint64_t knxt_seq_num;
  33. char *prev;
  34. char *nxt_offset;
  35. struct sk_buff *skb;
  36. rwlock_t blk_fill_in_prog_lock;
  37. /* Default is set to 8ms */
  38. #define DEFAULT_PRB_RETIRE_TOV (8)
  39. unsigned short retire_blk_tov;
  40. unsigned short version;
  41. unsigned long tov_in_jiffies;
  42. /* timer to retire an outstanding block */
  43. struct timer_list retire_blk_timer;
  44. };
  45. struct pgv {
  46. char *buffer;
  47. };
  48. struct packet_ring_buffer {
  49. struct pgv *pg_vec;
  50. unsigned int head;
  51. unsigned int frames_per_block;
  52. unsigned int frame_size;
  53. unsigned int frame_max;
  54. unsigned int pg_vec_order;
  55. unsigned int pg_vec_pages;
  56. unsigned int pg_vec_len;
  57. unsigned int __percpu *pending_refcnt;
  58. union {
  59. unsigned long *rx_owner_map;
  60. struct tpacket_kbdq_core prb_bdqc;
  61. };
  62. };
  63. extern struct mutex fanout_mutex;
  64. #define PACKET_FANOUT_MAX (1 << 16)
  65. struct packet_fanout {
  66. possible_net_t net;
  67. unsigned int num_members;
  68. u32 max_num_members;
  69. u16 id;
  70. u8 type;
  71. u8 flags;
  72. union {
  73. atomic_t rr_cur;
  74. struct bpf_prog __rcu *bpf_prog;
  75. };
  76. struct list_head list;
  77. spinlock_t lock;
  78. refcount_t sk_ref;
  79. struct packet_type prot_hook ____cacheline_aligned_in_smp;
  80. struct sock __rcu *arr[] __counted_by(max_num_members);
  81. };
  82. struct packet_rollover {
  83. int sock;
  84. atomic_long_t num;
  85. atomic_long_t num_huge;
  86. atomic_long_t num_failed;
  87. #define ROLLOVER_HLEN (L1_CACHE_BYTES / sizeof(u32))
  88. u32 history[ROLLOVER_HLEN] ____cacheline_aligned;
  89. } ____cacheline_aligned_in_smp;
  90. struct packet_sock {
  91. /* struct sock has to be the first member of packet_sock */
  92. struct sock sk;
  93. struct packet_fanout *fanout;
  94. union tpacket_stats_u stats;
  95. struct packet_ring_buffer rx_ring;
  96. struct packet_ring_buffer tx_ring;
  97. int copy_thresh;
  98. spinlock_t bind_lock;
  99. struct mutex pg_vec_lock;
  100. unsigned long flags;
  101. int ifindex; /* bound device */
  102. u8 vnet_hdr_sz;
  103. __be16 num;
  104. struct packet_rollover *rollover;
  105. struct packet_mclist *mclist;
  106. atomic_long_t mapped;
  107. enum tpacket_versions tp_version;
  108. unsigned int tp_hdrlen;
  109. unsigned int tp_reserve;
  110. unsigned int tp_tstamp;
  111. struct completion skb_completion;
  112. struct net_device __rcu *cached_dev;
  113. struct packet_type prot_hook ____cacheline_aligned_in_smp;
  114. atomic_t tp_drops ____cacheline_aligned_in_smp;
  115. };
  116. #define pkt_sk(ptr) container_of_const(ptr, struct packet_sock, sk)
  117. enum packet_sock_flags {
  118. PACKET_SOCK_ORIGDEV,
  119. PACKET_SOCK_AUXDATA,
  120. PACKET_SOCK_TX_HAS_OFF,
  121. PACKET_SOCK_TP_LOSS,
  122. PACKET_SOCK_RUNNING,
  123. PACKET_SOCK_PRESSURE,
  124. PACKET_SOCK_QDISC_BYPASS,
  125. };
  126. static inline void packet_sock_flag_set(struct packet_sock *po,
  127. enum packet_sock_flags flag,
  128. bool val)
  129. {
  130. if (val)
  131. set_bit(flag, &po->flags);
  132. else
  133. clear_bit(flag, &po->flags);
  134. }
  135. static inline bool packet_sock_flag(const struct packet_sock *po,
  136. enum packet_sock_flags flag)
  137. {
  138. return test_bit(flag, &po->flags);
  139. }
  140. #endif