]>
Commit | Line | Data |
---|---|---|
e263cd49 | 1 | /* |
605d52e6 | 2 | * QEMU TX packets abstractions |
e263cd49 DF |
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 <[email protected]> | |
10 | * Tamir Shomer <[email protected]> | |
11 | * Yan Vugenfirer <[email protected]> | |
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 | ||
e9abfcb5 | 18 | #include "qemu/osdep.h" |
605d52e6 | 19 | #include "net_tx_pkt.h" |
e263cd49 | 20 | #include "net/eth.h" |
e263cd49 DF |
21 | #include "net/checksum.h" |
22 | #include "net/tap.h" | |
23 | #include "net/net.h" | |
11171010 | 24 | #include "hw/pci/pci.h" |
e263cd49 DF |
25 | |
26 | enum { | |
605d52e6 DF |
27 | NET_TX_PKT_VHDR_FRAG = 0, |
28 | NET_TX_PKT_L2HDR_FRAG, | |
29 | NET_TX_PKT_L3HDR_FRAG, | |
30 | NET_TX_PKT_PL_START_FRAG | |
e263cd49 DF |
31 | }; |
32 | ||
33 | /* TX packet private context */ | |
605d52e6 | 34 | struct NetTxPkt { |
11171010 DF |
35 | PCIDevice *pci_dev; |
36 | ||
e263cd49 DF |
37 | struct virtio_net_hdr virt_hdr; |
38 | bool has_virt_hdr; | |
39 | ||
40 | struct iovec *raw; | |
41 | uint32_t raw_frags; | |
42 | uint32_t max_raw_frags; | |
43 | ||
44 | struct iovec *vec; | |
45 | ||
46 | uint8_t l2_hdr[ETH_MAX_L2_HDR_LEN]; | |
eb700029 | 47 | uint8_t l3_hdr[ETH_MAX_IP_DGRAM_LEN]; |
e263cd49 DF |
48 | |
49 | uint32_t payload_len; | |
50 | ||
51 | uint32_t payload_frags; | |
52 | uint32_t max_payload_frags; | |
53 | ||
54 | uint16_t hdr_len; | |
55 | eth_pkt_types_e packet_type; | |
56 | uint8_t l4proto; | |
eb700029 DF |
57 | |
58 | bool is_loopback; | |
e263cd49 DF |
59 | }; |
60 | ||
11171010 DF |
61 | void net_tx_pkt_init(struct NetTxPkt **pkt, PCIDevice *pci_dev, |
62 | uint32_t max_frags, bool has_virt_hdr) | |
e263cd49 | 63 | { |
605d52e6 | 64 | struct NetTxPkt *p = g_malloc0(sizeof *p); |
e263cd49 | 65 | |
11171010 DF |
66 | p->pci_dev = pci_dev; |
67 | ||
47882fa4 | 68 | p->vec = g_new(struct iovec, max_frags + NET_TX_PKT_PL_START_FRAG); |
e263cd49 | 69 | |
47882fa4 | 70 | p->raw = g_new(struct iovec, max_frags); |
e263cd49 DF |
71 | |
72 | p->max_payload_frags = max_frags; | |
73 | p->max_raw_frags = max_frags; | |
74 | p->has_virt_hdr = has_virt_hdr; | |
605d52e6 DF |
75 | p->vec[NET_TX_PKT_VHDR_FRAG].iov_base = &p->virt_hdr; |
76 | p->vec[NET_TX_PKT_VHDR_FRAG].iov_len = | |
e263cd49 | 77 | p->has_virt_hdr ? sizeof p->virt_hdr : 0; |
605d52e6 | 78 | p->vec[NET_TX_PKT_L2HDR_FRAG].iov_base = &p->l2_hdr; |
eb700029 | 79 | p->vec[NET_TX_PKT_L3HDR_FRAG].iov_base = &p->l3_hdr; |
e263cd49 DF |
80 | |
81 | *pkt = p; | |
82 | } | |
83 | ||
605d52e6 | 84 | void net_tx_pkt_uninit(struct NetTxPkt *pkt) |
e263cd49 DF |
85 | { |
86 | if (pkt) { | |
87 | g_free(pkt->vec); | |
88 | g_free(pkt->raw); | |
89 | g_free(pkt); | |
90 | } | |
91 | } | |
92 | ||
eb700029 | 93 | void net_tx_pkt_update_ip_hdr_checksum(struct NetTxPkt *pkt) |
e263cd49 DF |
94 | { |
95 | uint16_t csum; | |
e263cd49 | 96 | assert(pkt); |
e263cd49 | 97 | struct ip_header *ip_hdr; |
605d52e6 | 98 | ip_hdr = pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_base; |
e263cd49 | 99 | |
e263cd49 | 100 | ip_hdr->ip_len = cpu_to_be16(pkt->payload_len + |
605d52e6 | 101 | pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len); |
e263cd49 | 102 | |
e263cd49 DF |
103 | ip_hdr->ip_sum = 0; |
104 | csum = net_raw_checksum((uint8_t *)ip_hdr, | |
605d52e6 | 105 | pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len); |
e263cd49 | 106 | ip_hdr->ip_sum = cpu_to_be16(csum); |
eb700029 DF |
107 | } |
108 | ||
109 | void net_tx_pkt_update_ip_checksums(struct NetTxPkt *pkt) | |
110 | { | |
111 | uint16_t csum; | |
112 | uint32_t cntr, cso; | |
113 | assert(pkt); | |
114 | uint8_t gso_type = pkt->virt_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN; | |
115 | void *ip_hdr = pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_base; | |
116 | ||
117 | if (pkt->payload_len + pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len > | |
118 | ETH_MAX_IP_DGRAM_LEN) { | |
119 | return; | |
120 | } | |
121 | ||
122 | if (gso_type == VIRTIO_NET_HDR_GSO_TCPV4 || | |
123 | gso_type == VIRTIO_NET_HDR_GSO_UDP) { | |
124 | /* Calculate IP header checksum */ | |
125 | net_tx_pkt_update_ip_hdr_checksum(pkt); | |
126 | ||
127 | /* Calculate IP pseudo header checksum */ | |
128 | cntr = eth_calc_ip4_pseudo_hdr_csum(ip_hdr, pkt->payload_len, &cso); | |
129 | csum = cpu_to_be16(~net_checksum_finish(cntr)); | |
130 | } else if (gso_type == VIRTIO_NET_HDR_GSO_TCPV6) { | |
131 | /* Calculate IP pseudo header checksum */ | |
132 | cntr = eth_calc_ip6_pseudo_hdr_csum(ip_hdr, pkt->payload_len, | |
133 | IP_PROTO_TCP, &cso); | |
134 | csum = cpu_to_be16(~net_checksum_finish(cntr)); | |
135 | } else { | |
136 | return; | |
137 | } | |
e263cd49 | 138 | |
605d52e6 | 139 | iov_from_buf(&pkt->vec[NET_TX_PKT_PL_START_FRAG], pkt->payload_frags, |
e263cd49 DF |
140 | pkt->virt_hdr.csum_offset, &csum, sizeof(csum)); |
141 | } | |
142 | ||
605d52e6 | 143 | static void net_tx_pkt_calculate_hdr_len(struct NetTxPkt *pkt) |
e263cd49 | 144 | { |
605d52e6 DF |
145 | pkt->hdr_len = pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_len + |
146 | pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len; | |
e263cd49 DF |
147 | } |
148 | ||
605d52e6 | 149 | static bool net_tx_pkt_parse_headers(struct NetTxPkt *pkt) |
e263cd49 DF |
150 | { |
151 | struct iovec *l2_hdr, *l3_hdr; | |
152 | size_t bytes_read; | |
153 | size_t full_ip6hdr_len; | |
154 | uint16_t l3_proto; | |
155 | ||
156 | assert(pkt); | |
157 | ||
605d52e6 DF |
158 | l2_hdr = &pkt->vec[NET_TX_PKT_L2HDR_FRAG]; |
159 | l3_hdr = &pkt->vec[NET_TX_PKT_L3HDR_FRAG]; | |
e263cd49 DF |
160 | |
161 | bytes_read = iov_to_buf(pkt->raw, pkt->raw_frags, 0, l2_hdr->iov_base, | |
162 | ETH_MAX_L2_HDR_LEN); | |
a7278b36 DR |
163 | if (bytes_read < sizeof(struct eth_header)) { |
164 | l2_hdr->iov_len = 0; | |
165 | return false; | |
166 | } | |
167 | ||
168 | l2_hdr->iov_len = sizeof(struct eth_header); | |
169 | switch (be16_to_cpu(PKT_GET_ETH_HDR(l2_hdr->iov_base)->h_proto)) { | |
170 | case ETH_P_VLAN: | |
171 | l2_hdr->iov_len += sizeof(struct vlan_header); | |
172 | break; | |
173 | case ETH_P_DVLAN: | |
174 | l2_hdr->iov_len += 2 * sizeof(struct vlan_header); | |
175 | break; | |
176 | } | |
177 | ||
178 | if (bytes_read < l2_hdr->iov_len) { | |
e263cd49 | 179 | l2_hdr->iov_len = 0; |
eb700029 DF |
180 | l3_hdr->iov_len = 0; |
181 | pkt->packet_type = ETH_PKT_UCAST; | |
e263cd49 | 182 | return false; |
eb700029 DF |
183 | } else { |
184 | l2_hdr->iov_len = ETH_MAX_L2_HDR_LEN; | |
185 | l2_hdr->iov_len = eth_get_l2_hdr_length(l2_hdr->iov_base); | |
186 | pkt->packet_type = get_eth_packet_type(l2_hdr->iov_base); | |
e263cd49 DF |
187 | } |
188 | ||
eb700029 | 189 | l3_proto = eth_get_l3_proto(l2_hdr, 1, l2_hdr->iov_len); |
e263cd49 DF |
190 | |
191 | switch (l3_proto) { | |
192 | case ETH_P_IP: | |
e263cd49 DF |
193 | bytes_read = iov_to_buf(pkt->raw, pkt->raw_frags, l2_hdr->iov_len, |
194 | l3_hdr->iov_base, sizeof(struct ip_header)); | |
195 | ||
196 | if (bytes_read < sizeof(struct ip_header)) { | |
197 | l3_hdr->iov_len = 0; | |
198 | return false; | |
199 | } | |
200 | ||
201 | l3_hdr->iov_len = IP_HDR_GET_LEN(l3_hdr->iov_base); | |
e263cd49 | 202 | |
eb700029 | 203 | if (l3_hdr->iov_len < sizeof(struct ip_header)) { |
e263cd49 DF |
204 | l3_hdr->iov_len = 0; |
205 | return false; | |
206 | } | |
eb700029 DF |
207 | |
208 | pkt->l4proto = ((struct ip_header *) l3_hdr->iov_base)->ip_p; | |
209 | ||
210 | if (IP_HDR_GET_LEN(l3_hdr->iov_base) != sizeof(struct ip_header)) { | |
211 | /* copy optional IPv4 header data if any*/ | |
212 | bytes_read = iov_to_buf(pkt->raw, pkt->raw_frags, | |
213 | l2_hdr->iov_len + sizeof(struct ip_header), | |
214 | l3_hdr->iov_base + sizeof(struct ip_header), | |
215 | l3_hdr->iov_len - sizeof(struct ip_header)); | |
216 | if (bytes_read < l3_hdr->iov_len - sizeof(struct ip_header)) { | |
217 | l3_hdr->iov_len = 0; | |
218 | return false; | |
219 | } | |
220 | } | |
221 | ||
e263cd49 DF |
222 | break; |
223 | ||
224 | case ETH_P_IPV6: | |
eb700029 DF |
225 | { |
226 | eth_ip6_hdr_info hdrinfo; | |
227 | ||
e263cd49 | 228 | if (!eth_parse_ipv6_hdr(pkt->raw, pkt->raw_frags, l2_hdr->iov_len, |
eb700029 | 229 | &hdrinfo)) { |
e263cd49 DF |
230 | l3_hdr->iov_len = 0; |
231 | return false; | |
232 | } | |
233 | ||
eb700029 DF |
234 | pkt->l4proto = hdrinfo.l4proto; |
235 | full_ip6hdr_len = hdrinfo.full_hdr_len; | |
236 | ||
237 | if (full_ip6hdr_len > ETH_MAX_IP_DGRAM_LEN) { | |
238 | l3_hdr->iov_len = 0; | |
239 | return false; | |
240 | } | |
e263cd49 DF |
241 | |
242 | bytes_read = iov_to_buf(pkt->raw, pkt->raw_frags, l2_hdr->iov_len, | |
243 | l3_hdr->iov_base, full_ip6hdr_len); | |
244 | ||
245 | if (bytes_read < full_ip6hdr_len) { | |
246 | l3_hdr->iov_len = 0; | |
247 | return false; | |
248 | } else { | |
249 | l3_hdr->iov_len = full_ip6hdr_len; | |
250 | } | |
251 | break; | |
eb700029 | 252 | } |
e263cd49 DF |
253 | default: |
254 | l3_hdr->iov_len = 0; | |
255 | break; | |
256 | } | |
257 | ||
605d52e6 | 258 | net_tx_pkt_calculate_hdr_len(pkt); |
e263cd49 DF |
259 | return true; |
260 | } | |
261 | ||
eb700029 | 262 | static void net_tx_pkt_rebuild_payload(struct NetTxPkt *pkt) |
e263cd49 | 263 | { |
eb700029 | 264 | pkt->payload_len = iov_size(pkt->raw, pkt->raw_frags) - pkt->hdr_len; |
605d52e6 | 265 | pkt->payload_frags = iov_copy(&pkt->vec[NET_TX_PKT_PL_START_FRAG], |
e263cd49 DF |
266 | pkt->max_payload_frags, |
267 | pkt->raw, pkt->raw_frags, | |
eb700029 DF |
268 | pkt->hdr_len, pkt->payload_len); |
269 | } | |
e263cd49 | 270 | |
eb700029 DF |
271 | bool net_tx_pkt_parse(struct NetTxPkt *pkt) |
272 | { | |
273 | if (net_tx_pkt_parse_headers(pkt)) { | |
274 | net_tx_pkt_rebuild_payload(pkt); | |
e263cd49 DF |
275 | return true; |
276 | } else { | |
277 | return false; | |
278 | } | |
279 | } | |
280 | ||
605d52e6 | 281 | struct virtio_net_hdr *net_tx_pkt_get_vhdr(struct NetTxPkt *pkt) |
e263cd49 DF |
282 | { |
283 | assert(pkt); | |
284 | return &pkt->virt_hdr; | |
285 | } | |
286 | ||
605d52e6 | 287 | static uint8_t net_tx_pkt_get_gso_type(struct NetTxPkt *pkt, |
e263cd49 DF |
288 | bool tso_enable) |
289 | { | |
290 | uint8_t rc = VIRTIO_NET_HDR_GSO_NONE; | |
291 | uint16_t l3_proto; | |
292 | ||
eb700029 | 293 | l3_proto = eth_get_l3_proto(&pkt->vec[NET_TX_PKT_L2HDR_FRAG], 1, |
605d52e6 | 294 | pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_len); |
e263cd49 DF |
295 | |
296 | if (!tso_enable) { | |
297 | goto func_exit; | |
298 | } | |
299 | ||
605d52e6 | 300 | rc = eth_get_gso_type(l3_proto, pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_base, |
e263cd49 DF |
301 | pkt->l4proto); |
302 | ||
303 | func_exit: | |
304 | return rc; | |
305 | } | |
306 | ||
605d52e6 | 307 | void net_tx_pkt_build_vheader(struct NetTxPkt *pkt, bool tso_enable, |
e263cd49 DF |
308 | bool csum_enable, uint32_t gso_size) |
309 | { | |
310 | struct tcp_hdr l4hdr; | |
311 | assert(pkt); | |
312 | ||
313 | /* csum has to be enabled if tso is. */ | |
314 | assert(csum_enable || !tso_enable); | |
315 | ||
605d52e6 | 316 | pkt->virt_hdr.gso_type = net_tx_pkt_get_gso_type(pkt, tso_enable); |
e263cd49 DF |
317 | |
318 | switch (pkt->virt_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { | |
319 | case VIRTIO_NET_HDR_GSO_NONE: | |
320 | pkt->virt_hdr.hdr_len = 0; | |
321 | pkt->virt_hdr.gso_size = 0; | |
322 | break; | |
323 | ||
324 | case VIRTIO_NET_HDR_GSO_UDP: | |
eb700029 | 325 | pkt->virt_hdr.gso_size = gso_size; |
e263cd49 DF |
326 | pkt->virt_hdr.hdr_len = pkt->hdr_len + sizeof(struct udp_header); |
327 | break; | |
328 | ||
329 | case VIRTIO_NET_HDR_GSO_TCPV4: | |
330 | case VIRTIO_NET_HDR_GSO_TCPV6: | |
605d52e6 | 331 | iov_to_buf(&pkt->vec[NET_TX_PKT_PL_START_FRAG], pkt->payload_frags, |
e263cd49 DF |
332 | 0, &l4hdr, sizeof(l4hdr)); |
333 | pkt->virt_hdr.hdr_len = pkt->hdr_len + l4hdr.th_off * sizeof(uint32_t); | |
eb700029 | 334 | pkt->virt_hdr.gso_size = gso_size; |
e263cd49 DF |
335 | break; |
336 | ||
337 | default: | |
dfc6f865 | 338 | g_assert_not_reached(); |
e263cd49 DF |
339 | } |
340 | ||
341 | if (csum_enable) { | |
342 | switch (pkt->l4proto) { | |
343 | case IP_PROTO_TCP: | |
344 | pkt->virt_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; | |
345 | pkt->virt_hdr.csum_start = pkt->hdr_len; | |
346 | pkt->virt_hdr.csum_offset = offsetof(struct tcp_hdr, th_sum); | |
347 | break; | |
348 | case IP_PROTO_UDP: | |
349 | pkt->virt_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; | |
350 | pkt->virt_hdr.csum_start = pkt->hdr_len; | |
351 | pkt->virt_hdr.csum_offset = offsetof(struct udp_hdr, uh_sum); | |
352 | break; | |
353 | default: | |
354 | break; | |
355 | } | |
356 | } | |
357 | } | |
358 | ||
eb700029 DF |
359 | void net_tx_pkt_setup_vlan_header_ex(struct NetTxPkt *pkt, |
360 | uint16_t vlan, uint16_t vlan_ethtype) | |
e263cd49 DF |
361 | { |
362 | bool is_new; | |
363 | assert(pkt); | |
364 | ||
eb700029 DF |
365 | eth_setup_vlan_headers_ex(pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_base, |
366 | vlan, vlan_ethtype, &is_new); | |
e263cd49 DF |
367 | |
368 | /* update l2hdrlen */ | |
369 | if (is_new) { | |
370 | pkt->hdr_len += sizeof(struct vlan_header); | |
605d52e6 | 371 | pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_len += |
e263cd49 DF |
372 | sizeof(struct vlan_header); |
373 | } | |
374 | } | |
375 | ||
605d52e6 | 376 | bool net_tx_pkt_add_raw_fragment(struct NetTxPkt *pkt, hwaddr pa, |
e263cd49 DF |
377 | size_t len) |
378 | { | |
379 | hwaddr mapped_len = 0; | |
380 | struct iovec *ventry; | |
381 | assert(pkt); | |
382 | assert(pkt->max_raw_frags > pkt->raw_frags); | |
383 | ||
384 | if (!len) { | |
385 | return true; | |
386 | } | |
387 | ||
388 | ventry = &pkt->raw[pkt->raw_frags]; | |
389 | mapped_len = len; | |
390 | ||
11171010 DF |
391 | ventry->iov_base = pci_dma_map(pkt->pci_dev, pa, |
392 | &mapped_len, DMA_DIRECTION_TO_DEVICE); | |
e263cd49 | 393 | |
eb700029 DF |
394 | if ((ventry->iov_base != NULL) && (len == mapped_len)) { |
395 | ventry->iov_len = mapped_len; | |
396 | pkt->raw_frags++; | |
397 | return true; | |
398 | } else { | |
e263cd49 DF |
399 | return false; |
400 | } | |
eb700029 | 401 | } |
e263cd49 | 402 | |
eb700029 DF |
403 | bool net_tx_pkt_has_fragments(struct NetTxPkt *pkt) |
404 | { | |
405 | return pkt->raw_frags > 0; | |
e263cd49 DF |
406 | } |
407 | ||
605d52e6 | 408 | eth_pkt_types_e net_tx_pkt_get_packet_type(struct NetTxPkt *pkt) |
e263cd49 DF |
409 | { |
410 | assert(pkt); | |
411 | ||
412 | return pkt->packet_type; | |
413 | } | |
414 | ||
605d52e6 | 415 | size_t net_tx_pkt_get_total_len(struct NetTxPkt *pkt) |
e263cd49 DF |
416 | { |
417 | assert(pkt); | |
418 | ||
419 | return pkt->hdr_len + pkt->payload_len; | |
420 | } | |
421 | ||
605d52e6 | 422 | void net_tx_pkt_dump(struct NetTxPkt *pkt) |
e263cd49 | 423 | { |
605d52e6 | 424 | #ifdef NET_TX_PKT_DEBUG |
e263cd49 DF |
425 | assert(pkt); |
426 | ||
427 | printf("TX PKT: hdr_len: %d, pkt_type: 0x%X, l2hdr_len: %lu, " | |
428 | "l3hdr_len: %lu, payload_len: %u\n", pkt->hdr_len, pkt->packet_type, | |
605d52e6 DF |
429 | pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_len, |
430 | pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len, pkt->payload_len); | |
e263cd49 DF |
431 | #endif |
432 | } | |
433 | ||
605d52e6 | 434 | void net_tx_pkt_reset(struct NetTxPkt *pkt) |
e263cd49 DF |
435 | { |
436 | int i; | |
437 | ||
438 | /* no assert, as reset can be called before tx_pkt_init */ | |
439 | if (!pkt) { | |
440 | return; | |
441 | } | |
442 | ||
443 | memset(&pkt->virt_hdr, 0, sizeof(pkt->virt_hdr)); | |
444 | ||
e263cd49 | 445 | assert(pkt->vec); |
eb700029 | 446 | |
e263cd49 DF |
447 | pkt->payload_len = 0; |
448 | pkt->payload_frags = 0; | |
449 | ||
450 | assert(pkt->raw); | |
451 | for (i = 0; i < pkt->raw_frags; i++) { | |
452 | assert(pkt->raw[i].iov_base); | |
11171010 DF |
453 | pci_dma_unmap(pkt->pci_dev, pkt->raw[i].iov_base, pkt->raw[i].iov_len, |
454 | DMA_DIRECTION_TO_DEVICE, 0); | |
e263cd49 DF |
455 | } |
456 | pkt->raw_frags = 0; | |
457 | ||
458 | pkt->hdr_len = 0; | |
e263cd49 DF |
459 | pkt->l4proto = 0; |
460 | } | |
461 | ||
605d52e6 | 462 | static void net_tx_pkt_do_sw_csum(struct NetTxPkt *pkt) |
e263cd49 | 463 | { |
605d52e6 | 464 | struct iovec *iov = &pkt->vec[NET_TX_PKT_L2HDR_FRAG]; |
e263cd49 DF |
465 | uint32_t csum_cntr; |
466 | uint16_t csum = 0; | |
eb700029 | 467 | uint32_t cso; |
e263cd49 | 468 | /* num of iovec without vhdr */ |
605d52e6 | 469 | uint32_t iov_len = pkt->payload_frags + NET_TX_PKT_PL_START_FRAG - 1; |
e263cd49 DF |
470 | uint16_t csl; |
471 | struct ip_header *iphdr; | |
472 | size_t csum_offset = pkt->virt_hdr.csum_start + pkt->virt_hdr.csum_offset; | |
473 | ||
474 | /* Put zero to checksum field */ | |
475 | iov_from_buf(iov, iov_len, csum_offset, &csum, sizeof csum); | |
476 | ||
477 | /* Calculate L4 TCP/UDP checksum */ | |
478 | csl = pkt->payload_len; | |
479 | ||
e263cd49 | 480 | /* add pseudo header to csum */ |
605d52e6 | 481 | iphdr = pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_base; |
eb700029 DF |
482 | csum_cntr = eth_calc_ip4_pseudo_hdr_csum(iphdr, csl, &cso); |
483 | ||
484 | /* data checksum */ | |
485 | csum_cntr += | |
486 | net_checksum_add_iov(iov, iov_len, pkt->virt_hdr.csum_start, csl, cso); | |
e263cd49 DF |
487 | |
488 | /* Put the checksum obtained into the packet */ | |
489 | csum = cpu_to_be16(net_checksum_finish(csum_cntr)); | |
490 | iov_from_buf(iov, iov_len, csum_offset, &csum, sizeof csum); | |
491 | } | |
492 | ||
493 | enum { | |
605d52e6 DF |
494 | NET_TX_PKT_FRAGMENT_L2_HDR_POS = 0, |
495 | NET_TX_PKT_FRAGMENT_L3_HDR_POS, | |
496 | NET_TX_PKT_FRAGMENT_HEADER_NUM | |
e263cd49 DF |
497 | }; |
498 | ||
605d52e6 | 499 | #define NET_MAX_FRAG_SG_LIST (64) |
e263cd49 | 500 | |
605d52e6 | 501 | static size_t net_tx_pkt_fetch_fragment(struct NetTxPkt *pkt, |
e263cd49 DF |
502 | int *src_idx, size_t *src_offset, struct iovec *dst, int *dst_idx) |
503 | { | |
504 | size_t fetched = 0; | |
505 | struct iovec *src = pkt->vec; | |
506 | ||
605d52e6 | 507 | *dst_idx = NET_TX_PKT_FRAGMENT_HEADER_NUM; |
e263cd49 | 508 | |
eb700029 | 509 | while (fetched < IP_FRAG_ALIGN_SIZE(pkt->virt_hdr.gso_size)) { |
e263cd49 DF |
510 | |
511 | /* no more place in fragment iov */ | |
605d52e6 | 512 | if (*dst_idx == NET_MAX_FRAG_SG_LIST) { |
e263cd49 DF |
513 | break; |
514 | } | |
515 | ||
516 | /* no more data in iovec */ | |
605d52e6 | 517 | if (*src_idx == (pkt->payload_frags + NET_TX_PKT_PL_START_FRAG)) { |
e263cd49 DF |
518 | break; |
519 | } | |
520 | ||
521 | ||
522 | dst[*dst_idx].iov_base = src[*src_idx].iov_base + *src_offset; | |
523 | dst[*dst_idx].iov_len = MIN(src[*src_idx].iov_len - *src_offset, | |
eb700029 | 524 | IP_FRAG_ALIGN_SIZE(pkt->virt_hdr.gso_size) - fetched); |
e263cd49 DF |
525 | |
526 | *src_offset += dst[*dst_idx].iov_len; | |
527 | fetched += dst[*dst_idx].iov_len; | |
528 | ||
529 | if (*src_offset == src[*src_idx].iov_len) { | |
530 | *src_offset = 0; | |
531 | (*src_idx)++; | |
532 | } | |
533 | ||
534 | (*dst_idx)++; | |
535 | } | |
536 | ||
537 | return fetched; | |
538 | } | |
539 | ||
eb700029 DF |
540 | static inline void net_tx_pkt_sendv(struct NetTxPkt *pkt, |
541 | NetClientState *nc, const struct iovec *iov, int iov_cnt) | |
542 | { | |
543 | if (pkt->is_loopback) { | |
544 | nc->info->receive_iov(nc, iov, iov_cnt); | |
545 | } else { | |
546 | qemu_sendv_packet(nc, iov, iov_cnt); | |
547 | } | |
548 | } | |
549 | ||
605d52e6 | 550 | static bool net_tx_pkt_do_sw_fragmentation(struct NetTxPkt *pkt, |
e263cd49 DF |
551 | NetClientState *nc) |
552 | { | |
605d52e6 | 553 | struct iovec fragment[NET_MAX_FRAG_SG_LIST]; |
e263cd49 DF |
554 | size_t fragment_len = 0; |
555 | bool more_frags = false; | |
556 | ||
557 | /* some pointers for shorter code */ | |
558 | void *l2_iov_base, *l3_iov_base; | |
559 | size_t l2_iov_len, l3_iov_len; | |
605d52e6 | 560 | int src_idx = NET_TX_PKT_PL_START_FRAG, dst_idx; |
e263cd49 DF |
561 | size_t src_offset = 0; |
562 | size_t fragment_offset = 0; | |
563 | ||
605d52e6 DF |
564 | l2_iov_base = pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_base; |
565 | l2_iov_len = pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_len; | |
566 | l3_iov_base = pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_base; | |
567 | l3_iov_len = pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len; | |
e263cd49 DF |
568 | |
569 | /* Copy headers */ | |
605d52e6 DF |
570 | fragment[NET_TX_PKT_FRAGMENT_L2_HDR_POS].iov_base = l2_iov_base; |
571 | fragment[NET_TX_PKT_FRAGMENT_L2_HDR_POS].iov_len = l2_iov_len; | |
572 | fragment[NET_TX_PKT_FRAGMENT_L3_HDR_POS].iov_base = l3_iov_base; | |
573 | fragment[NET_TX_PKT_FRAGMENT_L3_HDR_POS].iov_len = l3_iov_len; | |
e263cd49 DF |
574 | |
575 | ||
576 | /* Put as much data as possible and send */ | |
577 | do { | |
605d52e6 | 578 | fragment_len = net_tx_pkt_fetch_fragment(pkt, &src_idx, &src_offset, |
e263cd49 DF |
579 | fragment, &dst_idx); |
580 | ||
581 | more_frags = (fragment_offset + fragment_len < pkt->payload_len); | |
582 | ||
583 | eth_setup_ip4_fragmentation(l2_iov_base, l2_iov_len, l3_iov_base, | |
584 | l3_iov_len, fragment_len, fragment_offset, more_frags); | |
585 | ||
586 | eth_fix_ip4_checksum(l3_iov_base, l3_iov_len); | |
587 | ||
eb700029 | 588 | net_tx_pkt_sendv(pkt, nc, fragment, dst_idx); |
e263cd49 DF |
589 | |
590 | fragment_offset += fragment_len; | |
591 | ||
ead315e4 | 592 | } while (fragment_len && more_frags); |
e263cd49 DF |
593 | |
594 | return true; | |
595 | } | |
596 | ||
605d52e6 | 597 | bool net_tx_pkt_send(struct NetTxPkt *pkt, NetClientState *nc) |
e263cd49 DF |
598 | { |
599 | assert(pkt); | |
600 | ||
601 | if (!pkt->has_virt_hdr && | |
602 | pkt->virt_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { | |
605d52e6 | 603 | net_tx_pkt_do_sw_csum(pkt); |
e263cd49 DF |
604 | } |
605 | ||
606 | /* | |
607 | * Since underlying infrastructure does not support IP datagrams longer | |
608 | * than 64K we should drop such packets and don't even try to send | |
609 | */ | |
610 | if (VIRTIO_NET_HDR_GSO_NONE != pkt->virt_hdr.gso_type) { | |
611 | if (pkt->payload_len > | |
612 | ETH_MAX_IP_DGRAM_LEN - | |
605d52e6 | 613 | pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len) { |
e263cd49 DF |
614 | return false; |
615 | } | |
616 | } | |
617 | ||
618 | if (pkt->has_virt_hdr || | |
619 | pkt->virt_hdr.gso_type == VIRTIO_NET_HDR_GSO_NONE) { | |
eb700029 | 620 | net_tx_pkt_sendv(pkt, nc, pkt->vec, |
605d52e6 | 621 | pkt->payload_frags + NET_TX_PKT_PL_START_FRAG); |
e263cd49 DF |
622 | return true; |
623 | } | |
624 | ||
605d52e6 | 625 | return net_tx_pkt_do_sw_fragmentation(pkt, nc); |
e263cd49 | 626 | } |
eb700029 DF |
627 | |
628 | bool net_tx_pkt_send_loopback(struct NetTxPkt *pkt, NetClientState *nc) | |
629 | { | |
630 | bool res; | |
631 | ||
632 | pkt->is_loopback = true; | |
633 | res = net_tx_pkt_send(pkt, nc); | |
634 | pkt->is_loopback = false; | |
635 | ||
636 | return res; | |
637 | } |