2
0

net_rx_pkt.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. } NetRxPktRssType;
  122. /**
  123. * calculates RSS hash for packet
  124. *
  125. * @pkt: packet
  126. * @type: RSS hash type
  127. *
  128. * Return: Toeplitz RSS hash.
  129. *
  130. */
  131. uint32_t
  132. net_rx_pkt_calc_rss_hash(struct NetRxPkt *pkt,
  133. NetRxPktRssType type,
  134. uint8_t *key);
  135. /**
  136. * fetches IP identification for the packet
  137. *
  138. * @pkt: packet
  139. *
  140. */
  141. uint16_t net_rx_pkt_get_ip_id(struct NetRxPkt *pkt);
  142. /**
  143. * check if given packet is a TCP ACK packet
  144. *
  145. * @pkt: packet
  146. *
  147. */
  148. bool net_rx_pkt_is_tcp_ack(struct NetRxPkt *pkt);
  149. /**
  150. * check if given packet contains TCP data
  151. *
  152. * @pkt: packet
  153. *
  154. */
  155. bool net_rx_pkt_has_tcp_data(struct NetRxPkt *pkt);
  156. /**
  157. * returns virtio header stored in rx context
  158. *
  159. * @pkt: packet
  160. * @ret: virtio header
  161. *
  162. */
  163. struct virtio_net_hdr *net_rx_pkt_get_vhdr(struct NetRxPkt *pkt);
  164. /**
  165. * returns packet type
  166. *
  167. * @pkt: packet
  168. * @ret: packet type
  169. *
  170. */
  171. eth_pkt_types_e net_rx_pkt_get_packet_type(struct NetRxPkt *pkt);
  172. /**
  173. * returns vlan tag
  174. *
  175. * @pkt: packet
  176. * @ret: VLAN tag
  177. *
  178. */
  179. uint16_t net_rx_pkt_get_vlan_tag(struct NetRxPkt *pkt);
  180. /**
  181. * tells whether vlan was stripped from the packet
  182. *
  183. * @pkt: packet
  184. * @ret: VLAN stripped sign
  185. *
  186. */
  187. bool net_rx_pkt_is_vlan_stripped(struct NetRxPkt *pkt);
  188. /**
  189. * notifies caller if the packet has virtio header
  190. *
  191. * @pkt: packet
  192. * @ret: true if packet has virtio header, false otherwize
  193. *
  194. */
  195. bool net_rx_pkt_has_virt_hdr(struct NetRxPkt *pkt);
  196. /**
  197. * attach scatter-gather data to rx packet
  198. *
  199. * @pkt: packet
  200. * @iov: received data scatter-gather list
  201. * @iovcnt number of elements in iov
  202. * @iovoff data start offset in the iov
  203. * @strip_vlan: should the module strip vlan from data
  204. *
  205. */
  206. void net_rx_pkt_attach_iovec(struct NetRxPkt *pkt,
  207. const struct iovec *iov,
  208. int iovcnt, size_t iovoff,
  209. bool strip_vlan);
  210. /**
  211. * attach scatter-gather data to rx packet
  212. *
  213. * @pkt: packet
  214. * @iov: received data scatter-gather list
  215. * @iovcnt number of elements in iov
  216. * @iovoff data start offset in the iov
  217. * @strip_vlan: should the module strip vlan from data
  218. * @vet: VLAN tag Ethernet type
  219. *
  220. */
  221. void net_rx_pkt_attach_iovec_ex(struct NetRxPkt *pkt,
  222. const struct iovec *iov, int iovcnt,
  223. size_t iovoff, bool strip_vlan,
  224. uint16_t vet);
  225. /**
  226. * attach data to rx packet
  227. *
  228. * @pkt: packet
  229. * @data: pointer to the data buffer
  230. * @len: data length
  231. * @strip_vlan: should the module strip vlan from data
  232. *
  233. */
  234. static inline void
  235. net_rx_pkt_attach_data(struct NetRxPkt *pkt, const void *data,
  236. size_t len, bool strip_vlan)
  237. {
  238. const struct iovec iov = {
  239. .iov_base = (void *) data,
  240. .iov_len = len
  241. };
  242. net_rx_pkt_attach_iovec(pkt, &iov, 1, 0, strip_vlan);
  243. }
  244. /**
  245. * returns io vector that holds the attached data
  246. *
  247. * @pkt: packet
  248. * @ret: pointer to IOVec
  249. *
  250. */
  251. struct iovec *net_rx_pkt_get_iovec(struct NetRxPkt *pkt);
  252. /**
  253. * returns io vector length that holds the attached data
  254. *
  255. * @pkt: packet
  256. * @ret: IOVec length
  257. *
  258. */
  259. uint16_t net_rx_pkt_get_iovec_len(struct NetRxPkt *pkt);
  260. /**
  261. * prints rx packet data if debug is enabled
  262. *
  263. * @pkt: packet
  264. *
  265. */
  266. void net_rx_pkt_dump(struct NetRxPkt *pkt);
  267. /**
  268. * copy passed vhdr data to packet context
  269. *
  270. * @pkt: packet
  271. * @vhdr: VHDR buffer
  272. *
  273. */
  274. void net_rx_pkt_set_vhdr(struct NetRxPkt *pkt,
  275. struct virtio_net_hdr *vhdr);
  276. /**
  277. * copy passed vhdr data to packet context
  278. *
  279. * @pkt: packet
  280. * @iov: VHDR iov
  281. * @iovcnt: VHDR iov array size
  282. *
  283. */
  284. void net_rx_pkt_set_vhdr_iovec(struct NetRxPkt *pkt,
  285. const struct iovec *iov, int iovcnt);
  286. /**
  287. * save packet type in packet context
  288. *
  289. * @pkt: packet
  290. * @packet_type: the packet type
  291. *
  292. */
  293. void net_rx_pkt_set_packet_type(struct NetRxPkt *pkt,
  294. eth_pkt_types_e packet_type);
  295. /**
  296. * validate TCP/UDP checksum of the packet
  297. *
  298. * @pkt: packet
  299. * @csum_valid: checksum validation result
  300. * @ret: true if validation was performed, false in case packet is
  301. * not TCP/UDP or checksum validation is not possible
  302. *
  303. */
  304. bool net_rx_pkt_validate_l4_csum(struct NetRxPkt *pkt, bool *csum_valid);
  305. /**
  306. * validate IPv4 checksum of the packet
  307. *
  308. * @pkt: packet
  309. * @csum_valid: checksum validation result
  310. * @ret: true if validation was performed, false in case packet is
  311. * not TCP/UDP or checksum validation is not possible
  312. *
  313. */
  314. bool net_rx_pkt_validate_l3_csum(struct NetRxPkt *pkt, bool *csum_valid);
  315. /**
  316. * fix IPv4 checksum of the packet
  317. *
  318. * @pkt: packet
  319. * @ret: true if checksum was fixed, false in case packet is
  320. * not TCP/UDP or checksum correction is not possible
  321. *
  322. */
  323. bool net_rx_pkt_fix_l4_csum(struct NetRxPkt *pkt);
  324. #endif