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