blob: 1d20b98493a103030d203add46a540c6d1f8010f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Tom Herbert43a0c672016-08-15 14:51:01 -07002/*
3 * Stream Parser
4 *
5 * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
Tom Herbert43a0c672016-08-15 14:51:01 -07006 */
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
17struct strp_stats {
Tom Herbertbbb03022017-07-28 16:22:43 -070018 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;
Tom Herbert43a0c672016-08-15 14:51:01 -070025};
26
27struct strp_aggr_stats {
Tom Herbertbbb03022017-07-28 16:22:43 -070028 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;
Tom Herbert43a0c672016-08-15 14:51:01 -070038};
39
40struct strparser;
41
42/* Callbacks are called with lock held for the attached socket */
43struct 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);
Tom Herbertbbb03022017-07-28 16:22:43 -070048 void (*lock)(struct strparser *strp);
49 void (*unlock)(struct strparser *strp);
Tom Herbert43a0c672016-08-15 14:51:01 -070050};
51
Tom Herbertbbb03022017-07-28 16:22:43 -070052struct strp_msg {
Tom Herbert43a0c672016-08-15 14:51:01 -070053 int full_len;
54 int offset;
55};
56
Tom Herbertbbb03022017-07-28 16:22:43 -070057static inline struct strp_msg *strp_msg(struct sk_buff *skb)
Tom Herbert43a0c672016-08-15 14:51:01 -070058{
Tom Herbertbbb03022017-07-28 16:22:43 -070059 return (struct strp_msg *)((void *)skb->cb +
Tom Herbert43a0c672016-08-15 14:51:01 -070060 offsetof(struct qdisc_skb_cb, data));
61}
62
63/* Structure for an attached lower socket */
64struct strparser {
65 struct sock *sk;
66
Tom Herbertbbb03022017-07-28 16:22:43 -070067 u32 stopped : 1;
68 u32 paused : 1;
69 u32 aborted : 1;
70 u32 interrupted : 1;
71 u32 unrecov_intr : 1;
Tom Herbert43a0c672016-08-15 14:51:01 -070072
Tom Herbertbbb03022017-07-28 16:22:43 -070073 struct sk_buff **skb_nextp;
Tom Herbertbbb03022017-07-28 16:22:43 -070074 struct sk_buff *skb_head;
75 unsigned int need_bytes;
Tom Herbert829385f2017-10-20 16:40:43 -070076 struct delayed_work msg_timer_work;
Tom Herbertbbb03022017-07-28 16:22:43 -070077 struct work_struct work;
Tom Herbert43a0c672016-08-15 14:51:01 -070078 struct strp_stats stats;
79 struct strp_callbacks cb;
80};
81
82/* Must be called with lock held for attached socket */
83static inline void strp_pause(struct strparser *strp)
84{
Tom Herbertbbb03022017-07-28 16:22:43 -070085 strp->paused = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -070086}
87
88/* May be called without holding lock for attached socket */
Tom Herbertcff6a332016-08-23 11:55:30 -070089void strp_unpause(struct strparser *strp);
Doron Roberts-Kedes7170e602018-06-06 09:33:28 -070090/* Must be called with process lock held (lock_sock) */
91void __strp_unpause(struct strparser *strp);
Tom Herbert43a0c672016-08-15 14:51:01 -070092
93static inline void save_strp_stats(struct strparser *strp,
94 struct strp_aggr_stats *agg_stats)
95{
96 /* Save psock statistics in the mux when psock is being unattached. */
97
98#define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += \
99 strp->stats._stat)
Tom Herbertbbb03022017-07-28 16:22:43 -0700100 SAVE_PSOCK_STATS(msgs);
101 SAVE_PSOCK_STATS(bytes);
102 SAVE_PSOCK_STATS(mem_fail);
103 SAVE_PSOCK_STATS(need_more_hdr);
104 SAVE_PSOCK_STATS(msg_too_big);
105 SAVE_PSOCK_STATS(msg_timeouts);
106 SAVE_PSOCK_STATS(bad_hdr_len);
Tom Herbert43a0c672016-08-15 14:51:01 -0700107#undef SAVE_PSOCK_STATS
108
Tom Herbertbbb03022017-07-28 16:22:43 -0700109 if (strp->aborted)
110 agg_stats->aborts++;
111 if (strp->interrupted)
112 agg_stats->interrupted++;
113 if (strp->unrecov_intr)
114 agg_stats->unrecov_intr++;
Tom Herbert43a0c672016-08-15 14:51:01 -0700115}
116
117static inline void aggregate_strp_stats(struct strp_aggr_stats *stats,
118 struct strp_aggr_stats *agg_stats)
119{
120#define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += stats->_stat)
Tom Herbertbbb03022017-07-28 16:22:43 -0700121 SAVE_PSOCK_STATS(msgs);
122 SAVE_PSOCK_STATS(bytes);
123 SAVE_PSOCK_STATS(mem_fail);
124 SAVE_PSOCK_STATS(need_more_hdr);
125 SAVE_PSOCK_STATS(msg_too_big);
126 SAVE_PSOCK_STATS(msg_timeouts);
127 SAVE_PSOCK_STATS(bad_hdr_len);
128 SAVE_PSOCK_STATS(aborts);
129 SAVE_PSOCK_STATS(interrupted);
130 SAVE_PSOCK_STATS(unrecov_intr);
Tom Herbert43a0c672016-08-15 14:51:01 -0700131#undef SAVE_PSOCK_STATS
132
133}
134
135void strp_done(struct strparser *strp);
136void strp_stop(struct strparser *strp);
137void strp_check_rcv(struct strparser *strp);
Tom Herbertbbb03022017-07-28 16:22:43 -0700138int strp_init(struct strparser *strp, struct sock *sk,
Eric Biggers3fd87122017-08-24 14:38:51 -0700139 const struct strp_callbacks *cb);
Tom Herbert96a59082016-08-28 14:43:19 -0700140void strp_data_ready(struct strparser *strp);
Tom Herbertbbb03022017-07-28 16:22:43 -0700141int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
142 unsigned int orig_offset, size_t orig_len,
143 size_t max_msg_size, long timeo);
Tom Herbert43a0c672016-08-15 14:51:01 -0700144
145#endif /* __NET_STRPARSER_H_ */