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