net_rx_pkt.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 L3 header offset
  72. *
  73. * @pkt: packet
  74. *
  75. */
  76. size_t net_rx_pkt_get_l3_hdr_offset(struct NetRxPkt *pkt);
  77. /**
  78. * fetches L4 header offset
  79. *
  80. * @pkt: packet
  81. *
  82. */
  83. size_t net_rx_pkt_get_l4_hdr_offset(struct NetRxPkt *pkt);
  84. /**
  85. * fetches L5 header offset
  86. *
  87. * @pkt: packet
  88. *
  89. */
  90. size_t net_rx_pkt_get_l5_hdr_offset(struct NetRxPkt *pkt);
  91. /**
  92. * fetches IP6 header analysis results
  93. *
  94. * Return: pointer to analysis results structure which is stored in internal
  95. * packet area.
  96. *
  97. */
  98. eth_ip6_hdr_info *net_rx_pkt_get_ip6_info(struct NetRxPkt *pkt);
  99. /**
  100. * fetches IP4 header analysis results
  101. *
  102. * Return: pointer to analysis results structure which is stored in internal
  103. * packet area.
  104. *
  105. */
  106. eth_ip4_hdr_info *net_rx_pkt_get_ip4_info(struct NetRxPkt *pkt);
  107. /**
  108. * fetches L4 header analysis results
  109. *
  110. * Return: pointer to analysis results structure which is stored in internal
  111. * packet area.
  112. *
  113. */
  114. eth_l4_hdr_info *net_rx_pkt_get_l4_info(struct NetRxPkt *pkt);
  115. typedef enum {
  116. NetPktRssIpV4,
  117. NetPktRssIpV4Tcp,
  118. NetPktRssIpV6Tcp,
  119. NetPktRssIpV6,
  120. NetPktRssIpV6Ex,
  121. NetPktRssIpV6TcpEx,
  122. NetPktRssIpV4Udp,
  123. NetPktRssIpV6Udp,
  124. NetPktRssIpV6UdpEx,
  125. } NetRxPktRssType;
  126. /**
  127. * calculates RSS hash for packet
  128. *
  129. * @pkt: packet
  130. * @type: RSS hash type
  131. *
  132. * Return: Toeplitz RSS hash.
  133. *
  134. */
  135. uint32_t
  136. net_rx_pkt_calc_rss_hash(struct NetRxPkt *pkt,
  137. NetRxPktRssType type,
  138. uint8_t *key);
  139. /**
  140. * fetches IP identification for the packet
  141. *
  142. * @pkt: packet
  143. *
  144. */
  145. uint16_t net_rx_pkt_get_ip_id(struct NetRxPkt *pkt);
  146. /**
  147. * check if given packet is a TCP ACK packet
  148. *
  149. * @pkt: packet
  150. *
  151. */
  152. bool net_rx_pkt_is_tcp_ack(struct NetRxPkt *pkt);
  153. /**
  154. * check if given packet contains TCP data
  155. *
  156. * @pkt: packet
  157. *
  158. */
  159. bool net_rx_pkt_has_tcp_data(struct NetRxPkt *pkt);
  160. /**
  161. * returns virtio header stored in rx context
  162. *
  163. * @pkt: packet
  164. * @ret: virtio header
  165. *
  166. */
  167. struct virtio_net_hdr *net_rx_pkt_get_vhdr(struct NetRxPkt *pkt);
  168. /**
  169. * returns packet type
  170. *
  171. * @pkt: packet
  172. * @ret: packet type
  173. *
  174. */
  175. eth_pkt_types_e net_rx_pkt_get_packet_type(struct NetRxPkt *pkt);
  176. /**
  177. * returns vlan tag
  178. *
  179. * @pkt: packet
  180. * @ret: VLAN tag
  181. *
  182. */
  183. uint16_t net_rx_pkt_get_vlan_tag(struct NetRxPkt *pkt);
  184. /**
  185. * tells whether vlan was stripped from the packet
  186. *
  187. * @pkt: packet
  188. * @ret: VLAN stripped sign
  189. *
  190. */
  191. bool net_rx_pkt_is_vlan_stripped(struct NetRxPkt *pkt);
  192. /**
  193. * attach scatter-gather data to rx packet
  194. *
  195. * @pkt: packet
  196. * @iov: received data scatter-gather list
  197. * @iovcnt number of elements in iov
  198. * @iovoff data start offset in the iov
  199. * @strip_vlan: should the module strip vlan from data
  200. *
  201. */
  202. void net_rx_pkt_attach_iovec(struct NetRxPkt *pkt,
  203. const struct iovec *iov,
  204. int iovcnt, size_t iovoff,
  205. bool strip_vlan);
  206. /**
  207. * attach scatter-gather data to rx packet
  208. *
  209. * @pkt: packet
  210. * @iov: received data scatter-gather list
  211. * @iovcnt number of elements in iov
  212. * @iovoff data start offset in the iov
  213. * @strip_vlan: should the module strip vlan from data
  214. * @vet: VLAN tag Ethernet type
  215. *
  216. */
  217. void net_rx_pkt_attach_iovec_ex(struct NetRxPkt *pkt,
  218. const struct iovec *iov, int iovcnt,
  219. size_t iovoff, bool strip_vlan,
  220. uint16_t vet);
  221. /**
  222. * attach data to rx packet
  223. *
  224. * @pkt: packet
  225. * @data: pointer to the data buffer
  226. * @len: data length
  227. * @strip_vlan: should the module strip vlan from data
  228. *
  229. */
  230. static inline void
  231. net_rx_pkt_attach_data(struct NetRxPkt *pkt, const void *data,
  232. size_t len, bool strip_vlan)
  233. {
  234. const struct iovec iov = {
  235. .iov_base = (void *) data,
  236. .iov_len = len
  237. };
  238. net_rx_pkt_attach_iovec(pkt, &iov, 1, 0, strip_vlan);
  239. }
  240. /**
  241. * returns io vector that holds the attached data
  242. *
  243. * @pkt: packet
  244. * @ret: pointer to IOVec
  245. *
  246. */
  247. struct iovec *net_rx_pkt_get_iovec(struct NetRxPkt *pkt);
  248. /**
  249. * returns io vector length that holds the attached data
  250. *
  251. * @pkt: packet
  252. * @ret: IOVec length
  253. *
  254. */
  255. uint16_t net_rx_pkt_get_iovec_len(struct NetRxPkt *pkt);
  256. /**
  257. * prints rx packet data if debug is enabled
  258. *
  259. * @pkt: packet
  260. *
  261. */
  262. void net_rx_pkt_dump(struct NetRxPkt *pkt);
  263. /**
  264. * copy passed vhdr data to packet context
  265. *
  266. * @pkt: packet
  267. * @vhdr: VHDR buffer
  268. *
  269. */
  270. void net_rx_pkt_set_vhdr(struct NetRxPkt *pkt,
  271. struct virtio_net_hdr *vhdr);
  272. /**
  273. * copy passed vhdr data to packet context
  274. *
  275. * @pkt: packet
  276. * @iov: VHDR iov
  277. * @iovcnt: VHDR iov array size
  278. *
  279. */
  280. void net_rx_pkt_set_vhdr_iovec(struct NetRxPkt *pkt,
  281. const struct iovec *iov, int iovcnt);
  282. /**
  283. * unset vhdr data from packet context
  284. *
  285. * @pkt: packet
  286. *
  287. */
  288. void net_rx_pkt_unset_vhdr(struct NetRxPkt *pkt);
  289. /**
  290. * save packet type in packet context
  291. *
  292. * @pkt: packet
  293. * @packet_type: the packet type
  294. *
  295. */
  296. void net_rx_pkt_set_packet_type(struct NetRxPkt *pkt,
  297. eth_pkt_types_e packet_type);
  298. /**
  299. * validate TCP/UDP checksum of the packet
  300. *
  301. * @pkt: packet
  302. * @csum_valid: checksum validation result
  303. * @ret: true if validation was performed, false in case packet is
  304. * not TCP/UDP or checksum validation is not possible
  305. *
  306. */
  307. bool net_rx_pkt_validate_l4_csum(struct NetRxPkt *pkt, bool *csum_valid);
  308. /**
  309. * validate IPv4 checksum of the packet
  310. *
  311. * @pkt: packet
  312. * @csum_valid: checksum validation result
  313. * @ret: true if validation was performed, false in case packet is
  314. * not TCP/UDP or checksum validation is not possible
  315. *
  316. */
  317. bool net_rx_pkt_validate_l3_csum(struct NetRxPkt *pkt, bool *csum_valid);
  318. /**
  319. * fix IPv4 checksum of the packet
  320. *
  321. * @pkt: packet
  322. * @ret: true if checksum was fixed, false in case packet is
  323. * not TCP/UDP or checksum correction is not possible
  324. *
  325. */
  326. bool net_rx_pkt_fix_l4_csum(struct NetRxPkt *pkt);
  327. #endif