Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Tom Herbert | 43a0c67 | 2016-08-15 14:51:01 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Stream Parser |
| 4 | * |
| 5 | * Copyright (c) 2016 Tom Herbert <tom@herbertland.com> |
Tom Herbert | 43a0c67 | 2016-08-15 14:51:01 -0700 | [diff] [blame] | 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 { |
Tom Herbert | bbb0302 | 2017-07-28 16:22:43 -0700 | [diff] [blame] | 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; |
Tom Herbert | 43a0c67 | 2016-08-15 14:51:01 -0700 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | struct strp_aggr_stats { |
Tom Herbert | bbb0302 | 2017-07-28 16:22:43 -0700 | [diff] [blame] | 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; |
Tom Herbert | 43a0c67 | 2016-08-15 14:51:01 -0700 | [diff] [blame] | 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); |
Tom Herbert | bbb0302 | 2017-07-28 16:22:43 -0700 | [diff] [blame] | 48 | void (*lock)(struct strparser *strp); |
| 49 | void (*unlock)(struct strparser *strp); |
Tom Herbert | 43a0c67 | 2016-08-15 14:51:01 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Tom Herbert | bbb0302 | 2017-07-28 16:22:43 -0700 | [diff] [blame] | 52 | struct strp_msg { |
Tom Herbert | 43a0c67 | 2016-08-15 14:51:01 -0700 | [diff] [blame] | 53 | int full_len; |
| 54 | int offset; |
| 55 | }; |
| 56 | |
Tom Herbert | bbb0302 | 2017-07-28 16:22:43 -0700 | [diff] [blame] | 57 | static inline struct strp_msg *strp_msg(struct sk_buff *skb) |
Tom Herbert | 43a0c67 | 2016-08-15 14:51:01 -0700 | [diff] [blame] | 58 | { |
Tom Herbert | bbb0302 | 2017-07-28 16:22:43 -0700 | [diff] [blame] | 59 | return (struct strp_msg *)((void *)skb->cb + |
Tom Herbert | 43a0c67 | 2016-08-15 14:51:01 -0700 | [diff] [blame] | 60 | offsetof(struct qdisc_skb_cb, data)); |
| 61 | } |
| 62 | |
| 63 | /* Structure for an attached lower socket */ |
| 64 | struct strparser { |
| 65 | struct sock *sk; |
| 66 | |
Tom Herbert | bbb0302 | 2017-07-28 16:22:43 -0700 | [diff] [blame] | 67 | u32 stopped : 1; |
| 68 | u32 paused : 1; |
| 69 | u32 aborted : 1; |
| 70 | u32 interrupted : 1; |
| 71 | u32 unrecov_intr : 1; |
Tom Herbert | 43a0c67 | 2016-08-15 14:51:01 -0700 | [diff] [blame] | 72 | |
Tom Herbert | bbb0302 | 2017-07-28 16:22:43 -0700 | [diff] [blame] | 73 | struct sk_buff **skb_nextp; |
Tom Herbert | bbb0302 | 2017-07-28 16:22:43 -0700 | [diff] [blame] | 74 | struct sk_buff *skb_head; |
| 75 | unsigned int need_bytes; |
Tom Herbert | 829385f | 2017-10-20 16:40:43 -0700 | [diff] [blame] | 76 | struct delayed_work msg_timer_work; |
Tom Herbert | bbb0302 | 2017-07-28 16:22:43 -0700 | [diff] [blame] | 77 | struct work_struct work; |
Tom Herbert | 43a0c67 | 2016-08-15 14:51:01 -0700 | [diff] [blame] | 78 | struct strp_stats stats; |
| 79 | struct strp_callbacks cb; |
| 80 | }; |
| 81 | |
| 82 | /* Must be called with lock held for attached socket */ |
| 83 | static inline void strp_pause(struct strparser *strp) |
| 84 | { |
Tom Herbert | bbb0302 | 2017-07-28 16:22:43 -0700 | [diff] [blame] | 85 | strp->paused = 1; |
Tom Herbert | 43a0c67 | 2016-08-15 14:51:01 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* May be called without holding lock for attached socket */ |
Tom Herbert | cff6a33 | 2016-08-23 11:55:30 -0700 | [diff] [blame] | 89 | void strp_unpause(struct strparser *strp); |
Doron Roberts-Kedes | 7170e60 | 2018-06-06 09:33:28 -0700 | [diff] [blame] | 90 | /* Must be called with process lock held (lock_sock) */ |
| 91 | void __strp_unpause(struct strparser *strp); |
Tom Herbert | 43a0c67 | 2016-08-15 14:51:01 -0700 | [diff] [blame] | 92 | |
| 93 | static 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 Herbert | bbb0302 | 2017-07-28 16:22:43 -0700 | [diff] [blame] | 100 | 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 Herbert | 43a0c67 | 2016-08-15 14:51:01 -0700 | [diff] [blame] | 107 | #undef SAVE_PSOCK_STATS |
| 108 | |
Tom Herbert | bbb0302 | 2017-07-28 16:22:43 -0700 | [diff] [blame] | 109 | 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 Herbert | 43a0c67 | 2016-08-15 14:51:01 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static 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 Herbert | bbb0302 | 2017-07-28 16:22:43 -0700 | [diff] [blame] | 121 | 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 Herbert | 43a0c67 | 2016-08-15 14:51:01 -0700 | [diff] [blame] | 131 | #undef SAVE_PSOCK_STATS |
| 132 | |
| 133 | } |
| 134 | |
| 135 | void strp_done(struct strparser *strp); |
| 136 | void strp_stop(struct strparser *strp); |
| 137 | void strp_check_rcv(struct strparser *strp); |
Tom Herbert | bbb0302 | 2017-07-28 16:22:43 -0700 | [diff] [blame] | 138 | int strp_init(struct strparser *strp, struct sock *sk, |
Eric Biggers | 3fd8712 | 2017-08-24 14:38:51 -0700 | [diff] [blame] | 139 | const struct strp_callbacks *cb); |
Tom Herbert | 96a5908 | 2016-08-28 14:43:19 -0700 | [diff] [blame] | 140 | void strp_data_ready(struct strparser *strp); |
Tom Herbert | bbb0302 | 2017-07-28 16:22:43 -0700 | [diff] [blame] | 141 | int 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 Herbert | 43a0c67 | 2016-08-15 14:51:01 -0700 | [diff] [blame] | 144 | |
| 145 | #endif /* __NET_STRPARSER_H_ */ |