net_rx_pkt.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * QEMU RX packets abstraction
  3. *
  4. * Copyright (c) 2012 Ravello Systems LTD (http://ravellosystems.com)
  5. *
  6. * Developed by Daynix Computing LTD (http://www.daynix.com)
  7. *
  8. * Authors:
  9. * Dmitry Fleytman <dmitry@daynix.com>
  10. * Tamir Shomer <tamirs@daynix.com>
  11. * Yan Vugenfirer <yan@daynix.com>
  12. *
  13. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  14. * See the COPYING file in the top-level directory.
  15. *
  16. */
  17. #ifndef NET_RX_PKT_H
  18. #define NET_RX_PKT_H
  19. #include "net/eth.h"
  20. /* defines to enable packet dump functions */
  21. /*#define NET_RX_PKT_DEBUG*/
  22. struct NetRxPkt;
  23. /**
  24. * Clean all rx packet resources
  25. *
  26. * @pkt: packet
  27. *
  28. */
  29. void net_rx_pkt_uninit(struct NetRxPkt *pkt);
  30. /**
  31. * Init function for rx packet functionality
  32. *
  33. * @pkt: packet pointer
  34. *
  35. */
  36. void net_rx_pkt_init(struct NetRxPkt **pkt);
  37. /**
  38. * returns total length of data attached to rx context
  39. *
  40. * @pkt: packet
  41. *
  42. * Return: nothing
  43. *
  44. */
  45. size_t net_rx_pkt_get_total_len(struct NetRxPkt *pkt);
  46. /**
  47. * parse and set packet analysis results
  48. *
  49. * @pkt: packet
  50. * @iov: received data scatter-gather list
  51. * @iovcnt: number of elements in iov
  52. * @iovoff: data start offset in the iov
  53. *
  54. */
  55. void net_rx_pkt_set_protocols(struct NetRxPkt *pkt,
  56. const struct iovec *iov, size_t iovcnt,
  57. size_t iovoff);
  58. /**
  59. * fetches packet analysis results
  60. *
  61. * @pkt: packet
  62. * @hasip4: whether the packet has an IPv4 header
  63. * @hasip6: whether the packet has an IPv6 header
  64. * @l4hdr_proto: protocol of L4 header
  65. *
  66. */
  67. void net_rx_pkt_get_protocols(struct NetRxPkt *pkt,
  68. bool *hasip4, bool *hasip6,
  69. EthL4HdrProto *l4hdr_proto);
  70. /**
  71. * fetches L4 header offset
  72. *
  73. * @pkt: packet
  74. *
  75. */
  76. size_t net_rx_pkt_get_l4_hdr_offset(struct NetRxPkt *pkt);
  77. /**
  78. * fetches L5 header offset
  79. *
  80. * @pkt: packet
  81. *
  82. */
  83. size_t net_rx_pkt_get_l5_hdr_offset(struct NetRxPkt *pkt);
  84. /**
  85. * fetches IP6 header analysis results
  86. *
  87. * Return: pointer to analysis results structure which is stored in internal
  88. * packet area.
  89. *
  90. */
  91. eth_ip6_hdr_info *net_rx_pkt_get_ip6_info(struct NetRxPkt *pkt);
  92. /**
  93. * fetches IP4 header analysis results
  94. *
  95. * Return: pointer to analysis results structure which is stored in internal
  96. * packet area.
  97. *
  98. */
  99. eth_ip4_hdr_info *net_rx_pkt_get_ip4_info(struct NetRxPkt *pkt);
  100. typedef enum {
  101. NetPktRssIpV4,
  102. NetPktRssIpV4Tcp,
  103. NetPktRssIpV6Tcp,
  104. NetPktRssIpV6,
  105. NetPktRssIpV6Ex,
  106. NetPktRssIpV6TcpEx,
  107. NetPktRssIpV4Udp,
  108. NetPktRssIpV6Udp,
  109. NetPktRssIpV6UdpEx,
  110. } NetRxPktRssType;
  111. /**
  112. * calculates RSS hash for packet
  113. *
  114. * @pkt: packet
  115. * @type: RSS hash type
  116. *
  117. * Return: Toeplitz RSS hash.
  118. *
  119. */
  120. uint32_t
  121. net_rx_pkt_calc_rss_hash(struct NetRxPkt *pkt,
  122. NetRxPktRssType type,
  123. uint8_t *key);
  124. /**
  125. * fetches IP identification for the packet
  126. *
  127. * @pkt: packet
  128. *
  129. */
  130. uint16_t net_rx_pkt_get_ip_id(struct NetRxPkt *pkt);
  131. /**
  132. * check if given packet is a TCP ACK packet
  133. *
  134. * @pkt: packet
  135. *
  136. */
  137. bool net_rx_pkt_is_tcp_ack(struct NetRxPkt *pkt);
  138. /**
  139. * check if given packet contains TCP data
  140. *
  141. * @pkt: packet
  142. *
  143. */
  144. bool net_rx_pkt_has_tcp_data(struct NetRxPkt *pkt);
  145. /**
  146. * returns virtio header stored in rx context
  147. *
  148. * @pkt: packet
  149. * @ret: virtio header
  150. *
  151. */
  152. struct virtio_net_hdr *net_rx_pkt_get_vhdr(struct NetRxPkt *pkt);
  153. /**
  154. * returns packet type
  155. *
  156. * @pkt: packet
  157. * @ret: packet type
  158. *
  159. */
  160. eth_pkt_types_e net_rx_pkt_get_packet_type(struct NetRxPkt *pkt);
  161. /**
  162. * returns vlan tag
  163. *
  164. * @pkt: packet
  165. * @ret: VLAN tag
  166. *
  167. */
  168. uint16_t net_rx_pkt_get_vlan_tag(struct NetRxPkt *pkt);
  169. /**
  170. * tells whether vlan was stripped from the packet
  171. *
  172. * @pkt: packet
  173. * @ret: VLAN stripped sign
  174. *
  175. */
  176. bool net_rx_pkt_is_vlan_stripped(struct NetRxPkt *pkt);
  177. /**
  178. * attach scatter-gather data to rx packet
  179. *
  180. * @pkt: packet
  181. * @iov: received data scatter-gather list
  182. * @iovcnt number of elements in iov
  183. * @iovoff data start offset in the iov
  184. * @strip_vlan: should the module strip vlan from data
  185. *
  186. */
  187. void net_rx_pkt_attach_iovec(struct NetRxPkt *pkt,
  188. const struct iovec *iov,
  189. int iovcnt, size_t iovoff,
  190. bool strip_vlan);
  191. /**
  192. * attach scatter-gather data to rx packet
  193. *
  194. * @pkt: packet
  195. * @iov: received data scatter-gather list
  196. * @iovcnt: number of elements in iov
  197. * @iovoff: data start offset in the iov
  198. * @strip_vlan_index: index of Q tag if it is to be stripped. negative otherwise.
  199. * @vet: VLAN tag Ethernet type
  200. * @vet_ext: outer VLAN tag Ethernet type
  201. *
  202. */
  203. void net_rx_pkt_attach_iovec_ex(struct NetRxPkt *pkt,
  204. const struct iovec *iov, int iovcnt,
  205. size_t iovoff, int strip_vlan_index,
  206. uint16_t vet, uint16_t vet_ext);
  207. /**
  208. * attach data to rx packet
  209. *
  210. * @pkt: packet
  211. * @data: pointer to the data buffer
  212. * @len: data length
  213. * @strip_vlan: should the module strip vlan from data
  214. *
  215. */
  216. static inline void
  217. net_rx_pkt_attach_data(struct NetRxPkt *pkt, const void *data,
  218. size_t len, bool strip_vlan)
  219. {
  220. const struct iovec iov = {
  221. .iov_base = (void *) data,
  222. .iov_len = len
  223. };
  224. net_rx_pkt_attach_iovec(pkt, &iov, 1, 0, strip_vlan);
  225. }
  226. /**
  227. * returns io vector that holds the attached data
  228. *
  229. * @pkt: packet
  230. * @ret: pointer to IOVec
  231. *
  232. */
  233. struct iovec *net_rx_pkt_get_iovec(struct NetRxPkt *pkt);
  234. /**
  235. * prints rx packet data if debug is enabled
  236. *
  237. * @pkt: packet
  238. *
  239. */
  240. void net_rx_pkt_dump(struct NetRxPkt *pkt);
  241. /**
  242. * copy passed vhdr data to packet context
  243. *
  244. * @pkt: packet
  245. * @vhdr: VHDR buffer
  246. *
  247. */
  248. void net_rx_pkt_set_vhdr(struct NetRxPkt *pkt,
  249. struct virtio_net_hdr *vhdr);
  250. /**
  251. * copy passed vhdr data to packet context
  252. *
  253. * @pkt: packet
  254. * @iov: VHDR iov
  255. * @iovcnt: VHDR iov array size
  256. *
  257. */
  258. void net_rx_pkt_set_vhdr_iovec(struct NetRxPkt *pkt,
  259. const struct iovec *iov, int iovcnt);
  260. /**
  261. * unset vhdr data from packet context
  262. *
  263. * @pkt: packet
  264. *
  265. */
  266. void net_rx_pkt_unset_vhdr(struct NetRxPkt *pkt);
  267. /**
  268. * save packet type in packet context
  269. *
  270. * @pkt: packet
  271. * @packet_type: the packet type
  272. *
  273. */
  274. void net_rx_pkt_set_packet_type(struct NetRxPkt *pkt,
  275. eth_pkt_types_e packet_type);
  276. /**
  277. * validate TCP/UDP checksum of the packet
  278. *
  279. * @pkt: packet
  280. * @csum_valid: checksum validation result
  281. * @ret: true if validation was performed, false in case packet is
  282. * not TCP/UDP or checksum validation is not possible
  283. *
  284. */
  285. bool net_rx_pkt_validate_l4_csum(struct NetRxPkt *pkt, bool *csum_valid);
  286. /**
  287. * validate IPv4 checksum of the packet
  288. *
  289. * @pkt: packet
  290. * @csum_valid: checksum validation result
  291. * @ret: true if validation was performed, false in case packet is
  292. * not TCP/UDP or checksum validation is not possible
  293. *
  294. */
  295. bool net_rx_pkt_validate_l3_csum(struct NetRxPkt *pkt, bool *csum_valid);
  296. /**
  297. * fix IPv4 checksum of the packet
  298. *
  299. * @pkt: packet
  300. * @ret: true if checksum was fixed, false in case packet is
  301. * not TCP/UDP or checksum correction is not possible
  302. *
  303. */
  304. bool net_rx_pkt_fix_l4_csum(struct NetRxPkt *pkt);
  305. #endif