cxgb4_tc_u32_parse.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * This file is part of the Chelsio T4 Ethernet driver for Linux.
  3. *
  4. * Copyright (c) 2016 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #ifndef __CXGB4_TC_U32_PARSE_H
  35. #define __CXGB4_TC_U32_PARSE_H
  36. struct cxgb4_match_field {
  37. int off; /* Offset from the beginning of the header to match */
  38. /* Fill the value/mask pair in the spec if matched */
  39. int (*val)(struct ch_filter_specification *f, u32 val, u32 mask);
  40. };
  41. /* IPv4 match fields */
  42. static inline int cxgb4_fill_ipv4_tos(struct ch_filter_specification *f,
  43. u32 val, u32 mask)
  44. {
  45. f->val.tos = (ntohl(val) >> 16) & 0x000000FF;
  46. f->mask.tos = (ntohl(mask) >> 16) & 0x000000FF;
  47. return 0;
  48. }
  49. static inline int cxgb4_fill_ipv4_frag(struct ch_filter_specification *f,
  50. u32 val, u32 mask)
  51. {
  52. u32 mask_val;
  53. u8 frag_val;
  54. frag_val = (ntohl(val) >> 13) & 0x00000007;
  55. mask_val = ntohl(mask) & 0x0000FFFF;
  56. if (frag_val == 0x1 && mask_val != 0x3FFF) { /* MF set */
  57. f->val.frag = 1;
  58. f->mask.frag = 1;
  59. } else if (frag_val == 0x2 && mask_val != 0x3FFF) { /* DF set */
  60. f->val.frag = 0;
  61. f->mask.frag = 1;
  62. } else {
  63. return -EINVAL;
  64. }
  65. return 0;
  66. }
  67. static inline int cxgb4_fill_ipv4_proto(struct ch_filter_specification *f,
  68. u32 val, u32 mask)
  69. {
  70. f->val.proto = (ntohl(val) >> 16) & 0x000000FF;
  71. f->mask.proto = (ntohl(mask) >> 16) & 0x000000FF;
  72. return 0;
  73. }
  74. static inline int cxgb4_fill_ipv4_src_ip(struct ch_filter_specification *f,
  75. u32 val, u32 mask)
  76. {
  77. memcpy(&f->val.fip[0], &val, sizeof(u32));
  78. memcpy(&f->mask.fip[0], &mask, sizeof(u32));
  79. return 0;
  80. }
  81. static inline int cxgb4_fill_ipv4_dst_ip(struct ch_filter_specification *f,
  82. u32 val, u32 mask)
  83. {
  84. memcpy(&f->val.lip[0], &val, sizeof(u32));
  85. memcpy(&f->mask.lip[0], &mask, sizeof(u32));
  86. return 0;
  87. }
  88. static const struct cxgb4_match_field cxgb4_ipv4_fields[] = {
  89. { .off = 0, .val = cxgb4_fill_ipv4_tos },
  90. { .off = 4, .val = cxgb4_fill_ipv4_frag },
  91. { .off = 8, .val = cxgb4_fill_ipv4_proto },
  92. { .off = 12, .val = cxgb4_fill_ipv4_src_ip },
  93. { .off = 16, .val = cxgb4_fill_ipv4_dst_ip },
  94. { .val = NULL }
  95. };
  96. /* IPv6 match fields */
  97. static inline int cxgb4_fill_ipv6_tos(struct ch_filter_specification *f,
  98. u32 val, u32 mask)
  99. {
  100. f->val.tos = (ntohl(val) >> 20) & 0x000000FF;
  101. f->mask.tos = (ntohl(mask) >> 20) & 0x000000FF;
  102. return 0;
  103. }
  104. static inline int cxgb4_fill_ipv6_proto(struct ch_filter_specification *f,
  105. u32 val, u32 mask)
  106. {
  107. f->val.proto = (ntohl(val) >> 8) & 0x000000FF;
  108. f->mask.proto = (ntohl(mask) >> 8) & 0x000000FF;
  109. return 0;
  110. }
  111. static inline int cxgb4_fill_ipv6_src_ip0(struct ch_filter_specification *f,
  112. u32 val, u32 mask)
  113. {
  114. memcpy(&f->val.fip[0], &val, sizeof(u32));
  115. memcpy(&f->mask.fip[0], &mask, sizeof(u32));
  116. return 0;
  117. }
  118. static inline int cxgb4_fill_ipv6_src_ip1(struct ch_filter_specification *f,
  119. u32 val, u32 mask)
  120. {
  121. memcpy(&f->val.fip[4], &val, sizeof(u32));
  122. memcpy(&f->mask.fip[4], &mask, sizeof(u32));
  123. return 0;
  124. }
  125. static inline int cxgb4_fill_ipv6_src_ip2(struct ch_filter_specification *f,
  126. u32 val, u32 mask)
  127. {
  128. memcpy(&f->val.fip[8], &val, sizeof(u32));
  129. memcpy(&f->mask.fip[8], &mask, sizeof(u32));
  130. return 0;
  131. }
  132. static inline int cxgb4_fill_ipv6_src_ip3(struct ch_filter_specification *f,
  133. u32 val, u32 mask)
  134. {
  135. memcpy(&f->val.fip[12], &val, sizeof(u32));
  136. memcpy(&f->mask.fip[12], &mask, sizeof(u32));
  137. return 0;
  138. }
  139. static inline int cxgb4_fill_ipv6_dst_ip0(struct ch_filter_specification *f,
  140. u32 val, u32 mask)
  141. {
  142. memcpy(&f->val.lip[0], &val, sizeof(u32));
  143. memcpy(&f->mask.lip[0], &mask, sizeof(u32));
  144. return 0;
  145. }
  146. static inline int cxgb4_fill_ipv6_dst_ip1(struct ch_filter_specification *f,
  147. u32 val, u32 mask)
  148. {
  149. memcpy(&f->val.lip[4], &val, sizeof(u32));
  150. memcpy(&f->mask.lip[4], &mask, sizeof(u32));
  151. return 0;
  152. }
  153. static inline int cxgb4_fill_ipv6_dst_ip2(struct ch_filter_specification *f,
  154. u32 val, u32 mask)
  155. {
  156. memcpy(&f->val.lip[8], &val, sizeof(u32));
  157. memcpy(&f->mask.lip[8], &mask, sizeof(u32));
  158. return 0;
  159. }
  160. static inline int cxgb4_fill_ipv6_dst_ip3(struct ch_filter_specification *f,
  161. u32 val, u32 mask)
  162. {
  163. memcpy(&f->val.lip[12], &val, sizeof(u32));
  164. memcpy(&f->mask.lip[12], &mask, sizeof(u32));
  165. return 0;
  166. }
  167. static const struct cxgb4_match_field cxgb4_ipv6_fields[] = {
  168. { .off = 0, .val = cxgb4_fill_ipv6_tos },
  169. { .off = 4, .val = cxgb4_fill_ipv6_proto },
  170. { .off = 8, .val = cxgb4_fill_ipv6_src_ip0 },
  171. { .off = 12, .val = cxgb4_fill_ipv6_src_ip1 },
  172. { .off = 16, .val = cxgb4_fill_ipv6_src_ip2 },
  173. { .off = 20, .val = cxgb4_fill_ipv6_src_ip3 },
  174. { .off = 24, .val = cxgb4_fill_ipv6_dst_ip0 },
  175. { .off = 28, .val = cxgb4_fill_ipv6_dst_ip1 },
  176. { .off = 32, .val = cxgb4_fill_ipv6_dst_ip2 },
  177. { .off = 36, .val = cxgb4_fill_ipv6_dst_ip3 },
  178. { .val = NULL }
  179. };
  180. /* TCP/UDP match */
  181. static inline int cxgb4_fill_l4_ports(struct ch_filter_specification *f,
  182. u32 val, u32 mask)
  183. {
  184. f->val.fport = ntohl(val) >> 16;
  185. f->mask.fport = ntohl(mask) >> 16;
  186. f->val.lport = ntohl(val) & 0x0000FFFF;
  187. f->mask.lport = ntohl(mask) & 0x0000FFFF;
  188. return 0;
  189. };
  190. static const struct cxgb4_match_field cxgb4_tcp_fields[] = {
  191. { .off = 0, .val = cxgb4_fill_l4_ports },
  192. { .val = NULL }
  193. };
  194. static const struct cxgb4_match_field cxgb4_udp_fields[] = {
  195. { .off = 0, .val = cxgb4_fill_l4_ports },
  196. { .val = NULL }
  197. };
  198. struct cxgb4_next_header {
  199. unsigned int offset; /* Offset to next header */
  200. /* offset, shift, and mask added to offset above
  201. * to get to next header. Useful when using a header
  202. * field's value to jump to next header such as IHL field
  203. * in IPv4 header.
  204. */
  205. unsigned int offoff;
  206. u32 shift;
  207. u32 mask;
  208. /* match criteria to make this jump */
  209. unsigned int match_off;
  210. u32 match_val;
  211. u32 match_mask;
  212. /* location of jump to make */
  213. const struct cxgb4_match_field *jump;
  214. };
  215. /* Accept a rule with a jump to transport layer header based on IHL field in
  216. * IPv4 header.
  217. */
  218. static const struct cxgb4_next_header cxgb4_ipv4_jumps[] = {
  219. { .offset = 0, .offoff = 0, .shift = 6, .mask = 0xF,
  220. .match_off = 8, .match_val = 0x600, .match_mask = 0xFF00,
  221. .jump = cxgb4_tcp_fields },
  222. { .offset = 0, .offoff = 0, .shift = 6, .mask = 0xF,
  223. .match_off = 8, .match_val = 0x1100, .match_mask = 0xFF00,
  224. .jump = cxgb4_udp_fields },
  225. { .jump = NULL }
  226. };
  227. /* Accept a rule with a jump directly past the 40 Bytes of IPv6 fixed header
  228. * to get to transport layer header.
  229. */
  230. static const struct cxgb4_next_header cxgb4_ipv6_jumps[] = {
  231. { .offset = 0x28, .offoff = 0, .shift = 0, .mask = 0,
  232. .match_off = 4, .match_val = 0x60000, .match_mask = 0xFF0000,
  233. .jump = cxgb4_tcp_fields },
  234. { .offset = 0x28, .offoff = 0, .shift = 0, .mask = 0,
  235. .match_off = 4, .match_val = 0x110000, .match_mask = 0xFF0000,
  236. .jump = cxgb4_udp_fields },
  237. { .jump = NULL }
  238. };
  239. struct cxgb4_link {
  240. const struct cxgb4_match_field *match_field; /* Next header */
  241. struct ch_filter_specification fs; /* Match spec associated with link */
  242. u32 link_handle; /* Knode handle associated with the link */
  243. unsigned long *tid_map; /* Bitmap for filter tids */
  244. };
  245. struct cxgb4_tc_u32_table {
  246. unsigned int size; /* number of entries in table */
  247. struct cxgb4_link table[0]; /* Jump table */
  248. };
  249. #endif /* __CXGB4_TC_U32_PARSE_H */