net_tx_pkt.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * QEMU TX 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_TX_PKT_H
  18. #define NET_TX_PKT_H
  19. #include "net/eth.h"
  20. #include "exec/hwaddr.h"
  21. /* define to enable packet dump functions */
  22. /*#define NET_TX_PKT_DEBUG*/
  23. struct NetTxPkt;
  24. typedef void (*NetTxPktFreeFrag)(void *, void *, size_t);
  25. typedef void (*NetTxPktSend)(void *, const struct iovec *, int, const struct iovec *, int);
  26. /**
  27. * Init function for tx packet functionality
  28. *
  29. * @pkt: packet pointer
  30. * @max_frags: max tx ip fragments
  31. */
  32. void net_tx_pkt_init(struct NetTxPkt **pkt, uint32_t max_frags);
  33. /**
  34. * Clean all tx packet resources.
  35. *
  36. * @pkt: packet.
  37. */
  38. void net_tx_pkt_uninit(struct NetTxPkt *pkt);
  39. /**
  40. * get virtio header
  41. *
  42. * @pkt: packet
  43. * @ret: virtio header
  44. */
  45. struct virtio_net_hdr *net_tx_pkt_get_vhdr(struct NetTxPkt *pkt);
  46. /**
  47. * build virtio header (will be stored in module context)
  48. *
  49. * @pkt: packet
  50. * @tso_enable: TSO enabled
  51. * @csum_enable: CSO enabled
  52. * @gso_size: MSS size for TSO
  53. * @ret: operation result
  54. *
  55. */
  56. bool net_tx_pkt_build_vheader(struct NetTxPkt *pkt, bool tso_enable,
  57. bool csum_enable, uint32_t gso_size);
  58. /**
  59. * updates vlan tag, and adds vlan header with custom ethernet type
  60. * in case it is missing.
  61. *
  62. * @pkt: packet
  63. * @vlan: VLAN tag
  64. * @vlan_ethtype: VLAN header Ethernet type
  65. *
  66. */
  67. void net_tx_pkt_setup_vlan_header_ex(struct NetTxPkt *pkt,
  68. uint16_t vlan, uint16_t vlan_ethtype);
  69. /**
  70. * updates vlan tag, and adds vlan header in case it is missing
  71. *
  72. * @pkt: packet
  73. * @vlan: VLAN tag
  74. *
  75. */
  76. static inline void
  77. net_tx_pkt_setup_vlan_header(struct NetTxPkt *pkt, uint16_t vlan)
  78. {
  79. net_tx_pkt_setup_vlan_header_ex(pkt, vlan, ETH_P_VLAN);
  80. }
  81. /**
  82. * populate data fragment into pkt context.
  83. *
  84. * @pkt: packet
  85. * @base: pointer to fragment
  86. * @len: length of fragment
  87. *
  88. */
  89. bool net_tx_pkt_add_raw_fragment(struct NetTxPkt *pkt, void *base, size_t len);
  90. /**
  91. * Fix ip header fields and calculate IP header and pseudo header checksums.
  92. *
  93. * @pkt: packet
  94. *
  95. */
  96. void net_tx_pkt_update_ip_checksums(struct NetTxPkt *pkt);
  97. /**
  98. * Calculate the IP header checksum.
  99. *
  100. * @pkt: packet
  101. *
  102. */
  103. void net_tx_pkt_update_ip_hdr_checksum(struct NetTxPkt *pkt);
  104. /**
  105. * Calculate the SCTP checksum.
  106. *
  107. * @pkt: packet
  108. *
  109. */
  110. bool net_tx_pkt_update_sctp_checksum(struct NetTxPkt *pkt);
  111. /**
  112. * get length of all populated data.
  113. *
  114. * @pkt: packet
  115. * @ret: total data length
  116. *
  117. */
  118. size_t net_tx_pkt_get_total_len(struct NetTxPkt *pkt);
  119. /**
  120. * get packet type
  121. *
  122. * @pkt: packet
  123. * @ret: packet type
  124. *
  125. */
  126. eth_pkt_types_e net_tx_pkt_get_packet_type(struct NetTxPkt *pkt);
  127. /**
  128. * prints packet data if debug is enabled
  129. *
  130. * @pkt: packet
  131. *
  132. */
  133. void net_tx_pkt_dump(struct NetTxPkt *pkt);
  134. /**
  135. * reset tx packet private context (needed to be called between packets)
  136. *
  137. * @pkt: packet
  138. * @callback: function to free the fragments
  139. * @context: pointer to be passed to the callback
  140. */
  141. void net_tx_pkt_reset(struct NetTxPkt *pkt,
  142. NetTxPktFreeFrag callback, void *context);
  143. /**
  144. * Unmap a fragment mapped from a PCI device.
  145. *
  146. * @context: PCI device owning fragment
  147. * @base: pointer to fragment
  148. * @len: length of fragment
  149. */
  150. void net_tx_pkt_unmap_frag_pci(void *context, void *base, size_t len);
  151. /**
  152. * map data fragment from PCI device and populate it into pkt context.
  153. *
  154. * @pci_dev: PCI device owning fragment
  155. * @pa: physical address of fragment
  156. * @len: length of fragment
  157. */
  158. bool net_tx_pkt_add_raw_fragment_pci(struct NetTxPkt *pkt, PCIDevice *pci_dev,
  159. dma_addr_t pa, size_t len);
  160. /**
  161. * Send packet to qemu. handles sw offloads if vhdr is not supported.
  162. *
  163. * @pkt: packet
  164. * @nc: NetClientState
  165. * @ret: operation result
  166. *
  167. */
  168. bool net_tx_pkt_send(struct NetTxPkt *pkt, NetClientState *nc);
  169. /**
  170. * Send packet with a custom function.
  171. *
  172. * @pkt: packet
  173. * @offload: whether the callback implements offloading
  174. * @callback: a function to be called back for each transformed packet
  175. * @context: a pointer to be passed to the callback.
  176. * @ret: operation result
  177. */
  178. bool net_tx_pkt_send_custom(struct NetTxPkt *pkt, bool offload,
  179. NetTxPktSend callback, void *context);
  180. /**
  181. * parse raw packet data and analyze offload requirements.
  182. *
  183. * @pkt: packet
  184. *
  185. */
  186. bool net_tx_pkt_parse(struct NetTxPkt *pkt);
  187. /**
  188. * indicates if there are data fragments held by this packet object.
  189. *
  190. * @pkt: packet
  191. *
  192. */
  193. bool net_tx_pkt_has_fragments(struct NetTxPkt *pkt);
  194. /**
  195. * Fix IPv6 'plen' field.
  196. * If ipv6 payload length field is 0 - then there should be Hop-by-Hop
  197. * option for packets greater than 65,535.
  198. * For packets with a payload less than 65,535: fix 'plen' field.
  199. * For backends with vheader, we need just one packet with proper
  200. * payload size. For now, qemu drops every packet with size greater 64K
  201. * (see net_tx_pkt_send()) so, there is no reason to add jumbo option to ip6
  202. * hop-by-hop extension if it's missed
  203. *
  204. * @pkt packet
  205. */
  206. void net_tx_pkt_fix_ip6_payload_len(struct NetTxPkt *pkt);
  207. #endif