^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Kernel Connection Multiplexor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #ifndef __NET_KCM_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define __NET_KCM_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <net/strparser.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <uapi/linux/kcm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) extern unsigned int kcm_net_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define KCM_STATS_ADD(stat, count) ((stat) += (count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define KCM_STATS_INCR(stat) ((stat)++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct kcm_psock_stats {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned long long tx_msgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned long long tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) unsigned long long reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) unsigned long long unreserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) unsigned int tx_aborts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct kcm_mux_stats {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned long long rx_msgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) unsigned long long rx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned long long tx_msgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned long long tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) unsigned int rx_ready_drops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) unsigned int tx_retries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) unsigned int psock_attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned int psock_unattach_rsvd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) unsigned int psock_unattach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct kcm_stats {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) unsigned long long rx_msgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned long long rx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned long long tx_msgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned long long tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct kcm_tx_msg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) unsigned int sent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned int fragidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned int frag_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned int msg_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct sk_buff *frag_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct sk_buff *last_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* Socket structure for KCM client sockets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct kcm_sock {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct sock sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct kcm_mux *mux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct list_head kcm_sock_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) u32 done : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct work_struct done_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct kcm_stats stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* Transmit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct kcm_psock *tx_psock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct work_struct tx_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct list_head wait_psock_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct sk_buff *seq_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) u32 tx_stopped : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /* Don't use bit fields here, these are set under different locks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) bool tx_wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) bool tx_wait_more;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /* Receive */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct kcm_psock *rx_psock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct list_head wait_rx_list; /* KCMs waiting for receiving */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) bool rx_wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) u32 rx_disabled : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct bpf_prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* Structure for an attached lower socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct kcm_psock {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct strparser strp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct kcm_mux *mux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) u32 tx_stopped : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) u32 done : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) u32 unattaching : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) void (*save_state_change)(struct sock *sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) void (*save_data_ready)(struct sock *sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) void (*save_write_space)(struct sock *sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct list_head psock_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct kcm_psock_stats stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* Receive */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct list_head psock_ready_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct bpf_prog *bpf_prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct kcm_sock *rx_kcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) unsigned long long saved_rx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) unsigned long long saved_rx_msgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct sk_buff *ready_rx_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* Transmit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct kcm_sock *tx_kcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct list_head psock_avail_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) unsigned long long saved_tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) unsigned long long saved_tx_msgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /* Per net MUX list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct kcm_net {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct kcm_psock_stats aggregate_psock_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct kcm_mux_stats aggregate_mux_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct strp_aggr_stats aggregate_strp_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct list_head mux_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* Structure for a MUX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct kcm_mux {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct list_head kcm_mux_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct rcu_head rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct kcm_net *knet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct list_head kcm_socks; /* All KCM sockets on MUX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int kcm_socks_cnt; /* Total KCM socket count for MUX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct list_head psocks; /* List of all psocks on MUX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int psocks_cnt; /* Total attached sockets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct kcm_mux_stats stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct kcm_psock_stats aggregate_psock_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct strp_aggr_stats aggregate_strp_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /* Receive */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) spinlock_t rx_lock ____cacheline_aligned_in_smp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct list_head kcm_rx_waiters; /* KCMs waiting for receiving */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct list_head psocks_ready; /* List of psocks with a msg ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct sk_buff_head rx_hold_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* Transmit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) spinlock_t lock ____cacheline_aligned_in_smp; /* TX and mux locking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct list_head psocks_avail; /* List of available psocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct list_head kcm_tx_waiters; /* KCMs waiting for a TX psock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int kcm_proc_init(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) void kcm_proc_exit(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static inline int kcm_proc_init(void) { return 0; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static inline void kcm_proc_exit(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static inline void aggregate_psock_stats(struct kcm_psock_stats *stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct kcm_psock_stats *agg_stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /* Save psock statistics in the mux when psock is being unattached. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) #define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += stats->_stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) SAVE_PSOCK_STATS(tx_msgs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) SAVE_PSOCK_STATS(tx_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) SAVE_PSOCK_STATS(reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) SAVE_PSOCK_STATS(unreserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) SAVE_PSOCK_STATS(tx_aborts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) #undef SAVE_PSOCK_STATS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static inline void aggregate_mux_stats(struct kcm_mux_stats *stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct kcm_mux_stats *agg_stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /* Save psock statistics in the mux when psock is being unattached. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) #define SAVE_MUX_STATS(_stat) (agg_stats->_stat += stats->_stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) SAVE_MUX_STATS(rx_msgs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) SAVE_MUX_STATS(rx_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) SAVE_MUX_STATS(tx_msgs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) SAVE_MUX_STATS(tx_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) SAVE_MUX_STATS(rx_ready_drops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) SAVE_MUX_STATS(psock_attach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) SAVE_MUX_STATS(psock_unattach_rsvd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) SAVE_MUX_STATS(psock_unattach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) #undef SAVE_MUX_STATS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) #endif /* __NET_KCM_H_ */