qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

net: Transmit zero UDP checksum as 0xFFFF

The checksum algorithm used by IPv4, TCP and UDP allows a zero value
to be represented by either 0x0000 and 0xFFFF. But per RFC 768, a zero
UDP checksum must be transmitted as 0xFFFF because 0x0000 is a special
value meaning no checksum.

Substitute 0xFFFF whenever a checksum is computed as zero when
modifying a UDP datagram header. Doing this on IPv4 and TCP checksums
is unnecessary but legal. Add a wrapper for net_checksum_finish() that
makes the substitution.

(We can't just change net_checksum_finish(), as that function is also
used by receivers to verify checksums, and in that case the expected
value is always 0x0000.)

Signed-off-by: Ed Swierk <eswierk@skyportsystems.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>

authored by

Ed Swierk and committed by
Jason Wang
0dacea92 ebc2327f

+11 -4
+1 -1
hw/net/e1000.c
··· 503 503 n = cse + 1; 504 504 if (sloc < n-1) { 505 505 sum = net_checksum_add(n-css, data+css); 506 - stw_be_p(data + sloc, net_checksum_finish(sum)); 506 + stw_be_p(data + sloc, net_checksum_finish_nozero(sum)); 507 507 } 508 508 } 509 509
+1 -1
hw/net/net_rx_pkt.c
··· 518 518 cntr += net_checksum_add_iov(pkt->vec, pkt->vec_len, 519 519 pkt->l4hdr_off, csl, cso); 520 520 521 - csum = net_checksum_finish(cntr); 521 + csum = net_checksum_finish_nozero(cntr); 522 522 523 523 trace_net_rx_pkt_l4_csum_calc_csum(pkt->l4hdr_off, csl, cntr, csum); 524 524
+1 -1
hw/net/net_tx_pkt.c
··· 486 486 net_checksum_add_iov(iov, iov_len, pkt->virt_hdr.csum_start, csl, cso); 487 487 488 488 /* Put the checksum obtained into the packet */ 489 - csum = cpu_to_be16(net_checksum_finish(csum_cntr)); 489 + csum = cpu_to_be16(net_checksum_finish_nozero(csum_cntr)); 490 490 iov_from_buf(iov, iov_len, csum_offset, &csum, sizeof csum); 491 491 } 492 492
+2 -1
hw/net/vmxnet3.c
··· 972 972 data = (uint8_t *)pkt_data + vhdr->csum_start; 973 973 len = pkt_len - vhdr->csum_start; 974 974 /* Put the checksum obtained into the packet */ 975 - stw_be_p(data + vhdr->csum_offset, net_raw_checksum(data, len)); 975 + stw_be_p(data + vhdr->csum_offset, 976 + net_checksum_finish_nozero(net_checksum_add(len, data))); 976 977 977 978 vhdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; 978 979 vhdr->flags |= VIRTIO_NET_HDR_F_DATA_VALID;
+6
include/net/checksum.h
··· 34 34 } 35 35 36 36 static inline uint16_t 37 + net_checksum_finish_nozero(uint32_t sum) 38 + { 39 + return net_checksum_finish(sum) ?: 0xFFFF; 40 + } 41 + 42 + static inline uint16_t 37 43 net_raw_checksum(uint8_t *data, int length) 38 44 { 39 45 return net_checksum_finish(net_checksum_add(length, data));