blob: f177c87ce38bd1919eab1f07f14b8c07006883eb [file] [log] [blame]
Tom Herbert43a0c672016-08-15 14:51:01 -07001/*
2 * Stream Parser
3 *
4 * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 */
10
11#ifndef __NET_STRPARSER_H_
12#define __NET_STRPARSER_H_
13
14#include <linux/skbuff.h>
15#include <net/sock.h>
16
17#define STRP_STATS_ADD(stat, count) ((stat) += (count))
18#define STRP_STATS_INCR(stat) ((stat)++)
19
20struct strp_stats {
Tom Herbertbbb03022017-07-28 16:22:43 -070021 unsigned long long msgs;
22 unsigned long long bytes;
23 unsigned int mem_fail;
24 unsigned int need_more_hdr;
25 unsigned int msg_too_big;
26 unsigned int msg_timeouts;
27 unsigned int bad_hdr_len;
Tom Herbert43a0c672016-08-15 14:51:01 -070028};
29
30struct strp_aggr_stats {
Tom Herbertbbb03022017-07-28 16:22:43 -070031 unsigned long long msgs;
32 unsigned long long bytes;
33 unsigned int mem_fail;
34 unsigned int need_more_hdr;
35 unsigned int msg_too_big;
36 unsigned int msg_timeouts;
37 unsigned int bad_hdr_len;
38 unsigned int aborts;
39 unsigned int interrupted;
40 unsigned int unrecov_intr;
Tom Herbert43a0c672016-08-15 14:51:01 -070041};
42
43struct strparser;
44
45/* Callbacks are called with lock held for the attached socket */
46struct strp_callbacks {
47 int (*parse_msg)(struct strparser *strp, struct sk_buff *skb);
48 void (*rcv_msg)(struct strparser *strp, struct sk_buff *skb);
49 int (*read_sock_done)(struct strparser *strp, int err);
50 void (*abort_parser)(struct strparser *strp, int err);
Tom Herbertbbb03022017-07-28 16:22:43 -070051 void (*lock)(struct strparser *strp);
52 void (*unlock)(struct strparser *strp);
Tom Herbert43a0c672016-08-15 14:51:01 -070053};
54
Tom Herbertbbb03022017-07-28 16:22:43 -070055struct strp_msg {
Tom Herbert43a0c672016-08-15 14:51:01 -070056 int full_len;
57 int offset;
58};
59
Tom Herbertbbb03022017-07-28 16:22:43 -070060static inline struct strp_msg *strp_msg(struct sk_buff *skb)
Tom Herbert43a0c672016-08-15 14:51:01 -070061{
Tom Herbertbbb03022017-07-28 16:22:43 -070062 return (struct strp_msg *)((void *)skb->cb +
Tom Herbert43a0c672016-08-15 14:51:01 -070063 offsetof(struct qdisc_skb_cb, data));
64}
65
66/* Structure for an attached lower socket */
67struct strparser {
68 struct sock *sk;
69
Tom Herbertbbb03022017-07-28 16:22:43 -070070 u32 stopped : 1;
71 u32 paused : 1;
72 u32 aborted : 1;
73 u32 interrupted : 1;
74 u32 unrecov_intr : 1;
Tom Herbert43a0c672016-08-15 14:51:01 -070075
Tom Herbertbbb03022017-07-28 16:22:43 -070076 struct sk_buff **skb_nextp;
Tom Herbertbbb03022017-07-28 16:22:43 -070077 struct sk_buff *skb_head;
78 unsigned int need_bytes;
Tom Herbert829385f2017-10-20 16:40:43 -070079 struct delayed_work msg_timer_work;
Tom Herbertbbb03022017-07-28 16:22:43 -070080 struct work_struct work;
Tom Herbert43a0c672016-08-15 14:51:01 -070081 struct strp_stats stats;
82 struct strp_callbacks cb;
83};
84
85/* Must be called with lock held for attached socket */
86static inline void strp_pause(struct strparser *strp)
87{
Tom Herbertbbb03022017-07-28 16:22:43 -070088 strp->paused = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -070089}
90
91/* May be called without holding lock for attached socket */
Tom Herbertcff6a332016-08-23 11:55:30 -070092void strp_unpause(struct strparser *strp);
Doron Roberts-Kedes7170e602018-06-06 09:33:28 -070093/* Must be called with process lock held (lock_sock) */
94void __strp_unpause(struct strparser *strp);
Tom Herbert43a0c672016-08-15 14:51:01 -070095
96static inline void save_strp_stats(struct strparser *strp,
97 struct strp_aggr_stats *agg_stats)
98{
99 /* Save psock statistics in the mux when psock is being unattached. */
100
101#define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += \
102 strp->stats._stat)
Tom Herbertbbb03022017-07-28 16:22:43 -0700103 SAVE_PSOCK_STATS(msgs);
104 SAVE_PSOCK_STATS(bytes);
105 SAVE_PSOCK_STATS(mem_fail);
106 SAVE_PSOCK_STATS(need_more_hdr);
107 SAVE_PSOCK_STATS(msg_too_big);
108 SAVE_PSOCK_STATS(msg_timeouts);
109 SAVE_PSOCK_STATS(bad_hdr_len);
Tom Herbert43a0c672016-08-15 14:51:01 -0700110#undef SAVE_PSOCK_STATS
111
Tom Herbertbbb03022017-07-28 16:22:43 -0700112 if (strp->aborted)
113 agg_stats->aborts++;
114 if (strp->interrupted)
115 agg_stats->interrupted++;
116 if (strp->unrecov_intr)
117 agg_stats->unrecov_intr++;
Tom Herbert43a0c672016-08-15 14:51:01 -0700118}
119
120static inline void aggregate_strp_stats(struct strp_aggr_stats *stats,
121 struct strp_aggr_stats *agg_stats)
122{
123#define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += stats->_stat)
Tom Herbertbbb03022017-07-28 16:22:43 -0700124 SAVE_PSOCK_STATS(msgs);
125 SAVE_PSOCK_STATS(bytes);
126 SAVE_PSOCK_STATS(mem_fail);
127 SAVE_PSOCK_STATS(need_more_hdr);
128 SAVE_PSOCK_STATS(msg_too_big);
129 SAVE_PSOCK_STATS(msg_timeouts);
130 SAVE_PSOCK_STATS(bad_hdr_len);
131 SAVE_PSOCK_STATS(aborts);
132 SAVE_PSOCK_STATS(interrupted);
133 SAVE_PSOCK_STATS(unrecov_intr);
Tom Herbert43a0c672016-08-15 14:51:01 -0700134#undef SAVE_PSOCK_STATS
135
136}
137
138void strp_done(struct strparser *strp);
139void strp_stop(struct strparser *strp);
140void strp_check_rcv(struct strparser *strp);
Tom Herbertbbb03022017-07-28 16:22:43 -0700141int strp_init(struct strparser *strp, struct sock *sk,
Eric Biggers3fd87122017-08-24 14:38:51 -0700142 const struct strp_callbacks *cb);
Tom Herbert96a59082016-08-28 14:43:19 -0700143void strp_data_ready(struct strparser *strp);
Tom Herbertbbb03022017-07-28 16:22:43 -0700144int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
145 unsigned int orig_offset, size_t orig_len,
146 size_t max_msg_size, long timeo);
Tom Herbert43a0c672016-08-15 14:51:01 -0700147
148#endif /* __NET_STRPARSER_H_ */