strparser.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 rx_msgs;
  18. unsigned long long rx_bytes;
  19. unsigned int rx_mem_fail;
  20. unsigned int rx_need_more_hdr;
  21. unsigned int rx_msg_too_big;
  22. unsigned int rx_msg_timeouts;
  23. unsigned int rx_bad_hdr_len;
  24. };
  25. struct strp_aggr_stats {
  26. unsigned long long rx_msgs;
  27. unsigned long long rx_bytes;
  28. unsigned int rx_mem_fail;
  29. unsigned int rx_need_more_hdr;
  30. unsigned int rx_msg_too_big;
  31. unsigned int rx_msg_timeouts;
  32. unsigned int rx_bad_hdr_len;
  33. unsigned int rx_aborts;
  34. unsigned int rx_interrupted;
  35. unsigned int rx_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. };
  45. struct strp_rx_msg {
  46. int full_len;
  47. int offset;
  48. };
  49. static inline struct strp_rx_msg *strp_rx_msg(struct sk_buff *skb)
  50. {
  51. return (struct strp_rx_msg *)((void *)skb->cb +
  52. offsetof(struct qdisc_skb_cb, data));
  53. }
  54. /* Structure for an attached lower socket */
  55. struct strparser {
  56. struct sock *sk;
  57. u32 rx_stopped : 1;
  58. u32 rx_paused : 1;
  59. u32 rx_aborted : 1;
  60. u32 rx_interrupted : 1;
  61. u32 rx_unrecov_intr : 1;
  62. struct sk_buff **rx_skb_nextp;
  63. struct timer_list rx_msg_timer;
  64. struct sk_buff *rx_skb_head;
  65. unsigned int rx_need_bytes;
  66. struct delayed_work rx_delayed_work;
  67. struct work_struct rx_work;
  68. struct strp_stats stats;
  69. struct strp_callbacks cb;
  70. };
  71. /* Must be called with lock held for attached socket */
  72. static inline void strp_pause(struct strparser *strp)
  73. {
  74. strp->rx_paused = 1;
  75. }
  76. /* May be called without holding lock for attached socket */
  77. void strp_unpause(struct strparser *strp);
  78. static inline void save_strp_stats(struct strparser *strp,
  79. struct strp_aggr_stats *agg_stats)
  80. {
  81. /* Save psock statistics in the mux when psock is being unattached. */
  82. #define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += \
  83. strp->stats._stat)
  84. SAVE_PSOCK_STATS(rx_msgs);
  85. SAVE_PSOCK_STATS(rx_bytes);
  86. SAVE_PSOCK_STATS(rx_mem_fail);
  87. SAVE_PSOCK_STATS(rx_need_more_hdr);
  88. SAVE_PSOCK_STATS(rx_msg_too_big);
  89. SAVE_PSOCK_STATS(rx_msg_timeouts);
  90. SAVE_PSOCK_STATS(rx_bad_hdr_len);
  91. #undef SAVE_PSOCK_STATS
  92. if (strp->rx_aborted)
  93. agg_stats->rx_aborts++;
  94. if (strp->rx_interrupted)
  95. agg_stats->rx_interrupted++;
  96. if (strp->rx_unrecov_intr)
  97. agg_stats->rx_unrecov_intr++;
  98. }
  99. static inline void aggregate_strp_stats(struct strp_aggr_stats *stats,
  100. struct strp_aggr_stats *agg_stats)
  101. {
  102. #define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += stats->_stat)
  103. SAVE_PSOCK_STATS(rx_msgs);
  104. SAVE_PSOCK_STATS(rx_bytes);
  105. SAVE_PSOCK_STATS(rx_mem_fail);
  106. SAVE_PSOCK_STATS(rx_need_more_hdr);
  107. SAVE_PSOCK_STATS(rx_msg_too_big);
  108. SAVE_PSOCK_STATS(rx_msg_timeouts);
  109. SAVE_PSOCK_STATS(rx_bad_hdr_len);
  110. SAVE_PSOCK_STATS(rx_aborts);
  111. SAVE_PSOCK_STATS(rx_interrupted);
  112. SAVE_PSOCK_STATS(rx_unrecov_intr);
  113. #undef SAVE_PSOCK_STATS
  114. }
  115. void strp_done(struct strparser *strp);
  116. void strp_stop(struct strparser *strp);
  117. void strp_check_rcv(struct strparser *strp);
  118. int strp_init(struct strparser *strp, struct sock *csk,
  119. struct strp_callbacks *cb);
  120. void strp_data_ready(struct strparser *strp);
  121. #endif /* __NET_STRPARSER_H_ */