strparser.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  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. #ifndef __NET_STRPARSER_H_
  11. #define __NET_STRPARSER_H_
  12. #include <linux/skbuff.h>
  13. #include <net/sock.h>
  14. #define STRP_STATS_ADD(stat, count) ((stat) += (count))
  15. #define STRP_STATS_INCR(stat) ((stat)++)
  16. struct strp_stats {
  17. unsigned long long msgs;
  18. unsigned long long bytes;
  19. unsigned int mem_fail;
  20. unsigned int need_more_hdr;
  21. unsigned int msg_too_big;
  22. unsigned int msg_timeouts;
  23. unsigned int bad_hdr_len;
  24. };
  25. struct strp_aggr_stats {
  26. unsigned long long msgs;
  27. unsigned long long bytes;
  28. unsigned int mem_fail;
  29. unsigned int need_more_hdr;
  30. unsigned int msg_too_big;
  31. unsigned int msg_timeouts;
  32. unsigned int bad_hdr_len;
  33. unsigned int aborts;
  34. unsigned int interrupted;
  35. unsigned int unrecov_intr;
  36. };
  37. struct strparser;
  38. /* Callbacks are called with lock held for the attached socket */
  39. struct strp_callbacks {
  40. int (*parse_msg)(struct strparser *strp, struct sk_buff *skb);
  41. void (*rcv_msg)(struct strparser *strp, struct sk_buff *skb);
  42. int (*read_sock_done)(struct strparser *strp, int err);
  43. void (*abort_parser)(struct strparser *strp, int err);
  44. void (*lock)(struct strparser *strp);
  45. void (*unlock)(struct strparser *strp);
  46. };
  47. struct strp_msg {
  48. int full_len;
  49. int offset;
  50. };
  51. static inline struct strp_msg *strp_msg(struct sk_buff *skb)
  52. {
  53. return (struct strp_msg *)((void *)skb->cb +
  54. offsetof(struct qdisc_skb_cb, data));
  55. }
  56. /* Structure for an attached lower socket */
  57. struct strparser {
  58. struct sock *sk;
  59. u32 stopped : 1;
  60. u32 paused : 1;
  61. u32 aborted : 1;
  62. u32 interrupted : 1;
  63. u32 unrecov_intr : 1;
  64. struct sk_buff **skb_nextp;
  65. struct sk_buff *skb_head;
  66. unsigned int need_bytes;
  67. struct delayed_work msg_timer_work;
  68. struct work_struct work;
  69. struct strp_stats stats;
  70. struct strp_callbacks cb;
  71. };
  72. /* Must be called with lock held for attached socket */
  73. static inline void strp_pause(struct strparser *strp)
  74. {
  75. strp->paused = 1;
  76. }
  77. /* May be called without holding lock for attached socket */
  78. void strp_unpause(struct strparser *strp);
  79. /* Must be called with process lock held (lock_sock) */
  80. void __strp_unpause(struct strparser *strp);
  81. static inline void save_strp_stats(struct strparser *strp,
  82. struct strp_aggr_stats *agg_stats)
  83. {
  84. /* Save psock statistics in the mux when psock is being unattached. */
  85. #define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += \
  86. strp->stats._stat)
  87. SAVE_PSOCK_STATS(msgs);
  88. SAVE_PSOCK_STATS(bytes);
  89. SAVE_PSOCK_STATS(mem_fail);
  90. SAVE_PSOCK_STATS(need_more_hdr);
  91. SAVE_PSOCK_STATS(msg_too_big);
  92. SAVE_PSOCK_STATS(msg_timeouts);
  93. SAVE_PSOCK_STATS(bad_hdr_len);
  94. #undef SAVE_PSOCK_STATS
  95. if (strp->aborted)
  96. agg_stats->aborts++;
  97. if (strp->interrupted)
  98. agg_stats->interrupted++;
  99. if (strp->unrecov_intr)
  100. agg_stats->unrecov_intr++;
  101. }
  102. static inline void aggregate_strp_stats(struct strp_aggr_stats *stats,
  103. struct strp_aggr_stats *agg_stats)
  104. {
  105. #define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += stats->_stat)
  106. SAVE_PSOCK_STATS(msgs);
  107. SAVE_PSOCK_STATS(bytes);
  108. SAVE_PSOCK_STATS(mem_fail);
  109. SAVE_PSOCK_STATS(need_more_hdr);
  110. SAVE_PSOCK_STATS(msg_too_big);
  111. SAVE_PSOCK_STATS(msg_timeouts);
  112. SAVE_PSOCK_STATS(bad_hdr_len);
  113. SAVE_PSOCK_STATS(aborts);
  114. SAVE_PSOCK_STATS(interrupted);
  115. SAVE_PSOCK_STATS(unrecov_intr);
  116. #undef SAVE_PSOCK_STATS
  117. }
  118. void strp_done(struct strparser *strp);
  119. void strp_stop(struct strparser *strp);
  120. void strp_check_rcv(struct strparser *strp);
  121. int strp_init(struct strparser *strp, struct sock *sk,
  122. const struct strp_callbacks *cb);
  123. void strp_data_ready(struct strparser *strp);
  124. int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
  125. unsigned int orig_offset, size_t orig_len,
  126. size_t max_msg_size, long timeo);
  127. #endif /* __NET_STRPARSER_H_ */