strparser.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. static inline void save_strp_stats(struct strparser *strp,
  80. struct strp_aggr_stats *agg_stats)
  81. {
  82. /* Save psock statistics in the mux when psock is being unattached. */
  83. #define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += \
  84. strp->stats._stat)
  85. SAVE_PSOCK_STATS(msgs);
  86. SAVE_PSOCK_STATS(bytes);
  87. SAVE_PSOCK_STATS(mem_fail);
  88. SAVE_PSOCK_STATS(need_more_hdr);
  89. SAVE_PSOCK_STATS(msg_too_big);
  90. SAVE_PSOCK_STATS(msg_timeouts);
  91. SAVE_PSOCK_STATS(bad_hdr_len);
  92. #undef SAVE_PSOCK_STATS
  93. if (strp->aborted)
  94. agg_stats->aborts++;
  95. if (strp->interrupted)
  96. agg_stats->interrupted++;
  97. if (strp->unrecov_intr)
  98. agg_stats->unrecov_intr++;
  99. }
  100. static inline void aggregate_strp_stats(struct strp_aggr_stats *stats,
  101. struct strp_aggr_stats *agg_stats)
  102. {
  103. #define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += stats->_stat)
  104. SAVE_PSOCK_STATS(msgs);
  105. SAVE_PSOCK_STATS(bytes);
  106. SAVE_PSOCK_STATS(mem_fail);
  107. SAVE_PSOCK_STATS(need_more_hdr);
  108. SAVE_PSOCK_STATS(msg_too_big);
  109. SAVE_PSOCK_STATS(msg_timeouts);
  110. SAVE_PSOCK_STATS(bad_hdr_len);
  111. SAVE_PSOCK_STATS(aborts);
  112. SAVE_PSOCK_STATS(interrupted);
  113. SAVE_PSOCK_STATS(unrecov_intr);
  114. #undef SAVE_PSOCK_STATS
  115. }
  116. void strp_done(struct strparser *strp);
  117. void strp_stop(struct strparser *strp);
  118. void strp_check_rcv(struct strparser *strp);
  119. int strp_init(struct strparser *strp, struct sock *sk,
  120. const struct strp_callbacks *cb);
  121. void strp_data_ready(struct strparser *strp);
  122. int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
  123. unsigned int orig_offset, size_t orig_len,
  124. size_t max_msg_size, long timeo);
  125. #endif /* __NET_STRPARSER_H_ */