net_rx_pkt.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. * @has_virt_hdr: device uses virtio header
  35. *
  36. */
  37. void net_rx_pkt_init(struct NetRxPkt **pkt, bool has_virt_hdr);
  38. /**
  39. * returns total length of data attached to rx context
  40. *
  41. * @pkt: packet
  42. *
  43. * Return: nothing
  44. *
  45. */
  46. size_t net_rx_pkt_get_total_len(struct NetRxPkt *pkt);
  47. /**
  48. * parse and set packet analysis results
  49. *
  50. * @pkt: packet
  51. * @data: pointer to the data buffer to be parsed
  52. * @len: data length
  53. *
  54. */
  55. void net_rx_pkt_set_protocols(struct NetRxPkt *pkt, const void *data,
  56. size_t len);
  57. /**
  58. * fetches packet analysis results
  59. *
  60. * @pkt: packet
  61. * @isip4: whether the packet given is IPv4
  62. * @isip6: whether the packet given is IPv6
  63. * @isudp: whether the packet given is UDP
  64. * @istcp: whether the packet given is TCP
  65. *
  66. */
  67. void net_rx_pkt_get_protocols(struct NetRxPkt *pkt,
  68. bool *isip4, bool *isip6,
  69. bool *isudp, bool *istcp);
  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. * notifies caller if the packet has virtio header
  194. *
  195. * @pkt: packet
  196. * @ret: true if packet has virtio header, false otherwize
  197. *
  198. */
  199. bool net_rx_pkt_has_virt_hdr(struct NetRxPkt *pkt);
  200. /**
  201. * attach scatter-gather data to rx packet
  202. *
  203. * @pkt: packet
  204. * @iov: received data scatter-gather list
  205. * @iovcnt number of elements in iov
  206. * @iovoff data start offset in the iov
  207. * @strip_vlan: should the module strip vlan from data
  208. *
  209. */
  210. void net_rx_pkt_attach_iovec(struct NetRxPkt *pkt,
  211. const struct iovec *iov,
  212. int iovcnt, size_t iovoff,
  213. bool strip_vlan);
  214. /**
  215. * attach scatter-gather data to rx packet
  216. *
  217. * @pkt: packet
  218. * @iov: received data scatter-gather list
  219. * @iovcnt number of elements in iov
  220. * @iovoff data start offset in the iov
  221. * @strip_vlan: should the module strip vlan from data
  222. * @vet: VLAN tag Ethernet type
  223. *
  224. */
  225. void net_rx_pkt_attach_iovec_ex(struct NetRxPkt *pkt,
  226. const struct iovec *iov, int iovcnt,
  227. size_t iovoff, bool strip_vlan,
  228. uint16_t vet);
  229. /**
  230. * attach data to rx packet
  231. *
  232. * @pkt: packet
  233. * @data: pointer to the data buffer
  234. * @len: data length
  235. * @strip_vlan: should the module strip vlan from data
  236. *
  237. */
  238. static inline void
  239. net_rx_pkt_attach_data(struct NetRxPkt *pkt, const void *data,
  240. size_t len, bool strip_vlan)
  241. {
  242. const struct iovec iov = {
  243. .iov_base = (void *) data,
  244. .iov_len = len
  245. };
  246. net_rx_pkt_attach_iovec(pkt, &iov, 1, 0, strip_vlan);
  247. }
  248. /**
  249. * returns io vector that holds the attached data
  250. *
  251. * @pkt: packet
  252. * @ret: pointer to IOVec
  253. *
  254. */
  255. struct iovec *net_rx_pkt_get_iovec(struct NetRxPkt *pkt);
  256. /**
  257. * returns io vector length that holds the attached data
  258. *
  259. * @pkt: packet
  260. * @ret: IOVec length
  261. *
  262. */
  263. uint16_t net_rx_pkt_get_iovec_len(struct NetRxPkt *pkt);
  264. /**
  265. * prints rx packet data if debug is enabled
  266. *
  267. * @pkt: packet
  268. *
  269. */
  270. void net_rx_pkt_dump(struct NetRxPkt *pkt);
  271. /**
  272. * copy passed vhdr data to packet context
  273. *
  274. * @pkt: packet
  275. * @vhdr: VHDR buffer
  276. *
  277. */
  278. void net_rx_pkt_set_vhdr(struct NetRxPkt *pkt,
  279. struct virtio_net_hdr *vhdr);
  280. /**
  281. * copy passed vhdr data to packet context
  282. *
  283. * @pkt: packet
  284. * @iov: VHDR iov
  285. * @iovcnt: VHDR iov array size
  286. *
  287. */
  288. void net_rx_pkt_set_vhdr_iovec(struct NetRxPkt *pkt,
  289. const struct iovec *iov, int iovcnt);
  290. /**
  291. * save packet type in packet context
  292. *
  293. * @pkt: packet
  294. * @packet_type: the packet type
  295. *
  296. */
  297. void net_rx_pkt_set_packet_type(struct NetRxPkt *pkt,
  298. eth_pkt_types_e packet_type);
  299. /**
  300. * validate TCP/UDP checksum of the packet
  301. *
  302. * @pkt: packet
  303. * @csum_valid: checksum validation result
  304. * @ret: true if validation was performed, false in case packet is
  305. * not TCP/UDP or checksum validation is not possible
  306. *
  307. */
  308. bool net_rx_pkt_validate_l4_csum(struct NetRxPkt *pkt, bool *csum_valid);
  309. /**
  310. * validate IPv4 checksum of the packet
  311. *
  312. * @pkt: packet
  313. * @csum_valid: checksum validation result
  314. * @ret: true if validation was performed, false in case packet is
  315. * not TCP/UDP or checksum validation is not possible
  316. *
  317. */
  318. bool net_rx_pkt_validate_l3_csum(struct NetRxPkt *pkt, bool *csum_valid);
  319. /**
  320. * fix IPv4 checksum of the packet
  321. *
  322. * @pkt: packet
  323. * @ret: true if checksum was fixed, false in case packet is
  324. * not TCP/UDP or checksum correction is not possible
  325. *
  326. */
  327. bool net_rx_pkt_fix_l4_csum(struct NetRxPkt *pkt);
  328. #endif