xref: /openbmc/linux/include/net/strparser.h (revision 48ca54e3)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Stream Parser
4  *
5  * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
6  */
7 
8 #ifndef __NET_STRPARSER_H_
9 #define __NET_STRPARSER_H_
10 
11 #include <linux/skbuff.h>
12 #include <net/sock.h>
13 
14 #define STRP_STATS_ADD(stat, count) ((stat) += (count))
15 #define STRP_STATS_INCR(stat) ((stat)++)
16 
17 struct strp_stats {
18 	unsigned long long msgs;
19 	unsigned long long bytes;
20 	unsigned int mem_fail;
21 	unsigned int need_more_hdr;
22 	unsigned int msg_too_big;
23 	unsigned int msg_timeouts;
24 	unsigned int bad_hdr_len;
25 };
26 
27 struct strp_aggr_stats {
28 	unsigned long long msgs;
29 	unsigned long long bytes;
30 	unsigned int mem_fail;
31 	unsigned int need_more_hdr;
32 	unsigned int msg_too_big;
33 	unsigned int msg_timeouts;
34 	unsigned int bad_hdr_len;
35 	unsigned int aborts;
36 	unsigned int interrupted;
37 	unsigned int unrecov_intr;
38 };
39 
40 struct strparser;
41 
42 /* Callbacks are called with lock held for the attached socket */
43 struct strp_callbacks {
44 	int (*parse_msg)(struct strparser *strp, struct sk_buff *skb);
45 	void (*rcv_msg)(struct strparser *strp, struct sk_buff *skb);
46 	int (*read_sock_done)(struct strparser *strp, int err);
47 	void (*abort_parser)(struct strparser *strp, int err);
48 	void (*lock)(struct strparser *strp);
49 	void (*unlock)(struct strparser *strp);
50 };
51 
52 struct strp_msg {
53 	int full_len;
54 	int offset;
55 };
56 
57 struct _strp_msg {
58 	/* Internal cb structure. struct strp_msg must be first for passing
59 	 * to upper layer.
60 	 */
61 	struct strp_msg strp;
62 	int accum_len;
63 };
64 
65 struct sk_skb_cb {
66 #define SK_SKB_CB_PRIV_LEN 20
67 	unsigned char data[SK_SKB_CB_PRIV_LEN];
68 	struct _strp_msg strp;
69 	/* temp_reg is a temporary register used for bpf_convert_data_end_access
70 	 * when dst_reg == src_reg.
71 	 */
72 	u64 temp_reg;
73 	struct tls_msg {
74 		u8 control;
75 		u8 decrypted;
76 	} tls;
77 };
78 
79 static inline struct strp_msg *strp_msg(struct sk_buff *skb)
80 {
81 	return (struct strp_msg *)((void *)skb->cb +
82 		offsetof(struct sk_skb_cb, strp));
83 }
84 
85 /* Structure for an attached lower socket */
86 struct strparser {
87 	struct sock *sk;
88 
89 	u32 stopped : 1;
90 	u32 paused : 1;
91 	u32 aborted : 1;
92 	u32 interrupted : 1;
93 	u32 unrecov_intr : 1;
94 
95 	struct sk_buff **skb_nextp;
96 	struct sk_buff *skb_head;
97 	unsigned int need_bytes;
98 	struct delayed_work msg_timer_work;
99 	struct work_struct work;
100 	struct strp_stats stats;
101 	struct strp_callbacks cb;
102 };
103 
104 /* Must be called with lock held for attached socket */
105 static inline void strp_pause(struct strparser *strp)
106 {
107 	strp->paused = 1;
108 }
109 
110 /* May be called without holding lock for attached socket */
111 void strp_unpause(struct strparser *strp);
112 /* Must be called with process lock held (lock_sock) */
113 void __strp_unpause(struct strparser *strp);
114 
115 static inline void save_strp_stats(struct strparser *strp,
116 				   struct strp_aggr_stats *agg_stats)
117 {
118 	/* Save psock statistics in the mux when psock is being unattached. */
119 
120 #define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat +=		\
121 				 strp->stats._stat)
122 	SAVE_PSOCK_STATS(msgs);
123 	SAVE_PSOCK_STATS(bytes);
124 	SAVE_PSOCK_STATS(mem_fail);
125 	SAVE_PSOCK_STATS(need_more_hdr);
126 	SAVE_PSOCK_STATS(msg_too_big);
127 	SAVE_PSOCK_STATS(msg_timeouts);
128 	SAVE_PSOCK_STATS(bad_hdr_len);
129 #undef SAVE_PSOCK_STATS
130 
131 	if (strp->aborted)
132 		agg_stats->aborts++;
133 	if (strp->interrupted)
134 		agg_stats->interrupted++;
135 	if (strp->unrecov_intr)
136 		agg_stats->unrecov_intr++;
137 }
138 
139 static inline void aggregate_strp_stats(struct strp_aggr_stats *stats,
140 					struct strp_aggr_stats *agg_stats)
141 {
142 #define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += stats->_stat)
143 	SAVE_PSOCK_STATS(msgs);
144 	SAVE_PSOCK_STATS(bytes);
145 	SAVE_PSOCK_STATS(mem_fail);
146 	SAVE_PSOCK_STATS(need_more_hdr);
147 	SAVE_PSOCK_STATS(msg_too_big);
148 	SAVE_PSOCK_STATS(msg_timeouts);
149 	SAVE_PSOCK_STATS(bad_hdr_len);
150 	SAVE_PSOCK_STATS(aborts);
151 	SAVE_PSOCK_STATS(interrupted);
152 	SAVE_PSOCK_STATS(unrecov_intr);
153 #undef SAVE_PSOCK_STATS
154 
155 }
156 
157 void strp_done(struct strparser *strp);
158 void strp_stop(struct strparser *strp);
159 void strp_check_rcv(struct strparser *strp);
160 int strp_init(struct strparser *strp, struct sock *sk,
161 	      const struct strp_callbacks *cb);
162 void strp_data_ready(struct strparser *strp);
163 int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
164 		 unsigned int orig_offset, size_t orig_len,
165 		 size_t max_msg_size, long timeo);
166 
167 #endif /* __NET_STRPARSER_H_ */
168