1605d52e6SDmitry Fleytman /* 2605d52e6SDmitry Fleytman * QEMU TX packets abstractions 3605d52e6SDmitry Fleytman * 4605d52e6SDmitry Fleytman * Copyright (c) 2012 Ravello Systems LTD (http://ravellosystems.com) 5605d52e6SDmitry Fleytman * 6605d52e6SDmitry Fleytman * Developed by Daynix Computing LTD (http://www.daynix.com) 7605d52e6SDmitry Fleytman * 8605d52e6SDmitry Fleytman * Authors: 9605d52e6SDmitry Fleytman * Dmitry Fleytman <dmitry@daynix.com> 10605d52e6SDmitry Fleytman * Tamir Shomer <tamirs@daynix.com> 11605d52e6SDmitry Fleytman * Yan Vugenfirer <yan@daynix.com> 12605d52e6SDmitry Fleytman * 13605d52e6SDmitry Fleytman * This work is licensed under the terms of the GNU GPL, version 2 or later. 14605d52e6SDmitry Fleytman * See the COPYING file in the top-level directory. 15605d52e6SDmitry Fleytman * 16605d52e6SDmitry Fleytman */ 17605d52e6SDmitry Fleytman 18e9abfcb5SPaolo Bonzini #include "qemu/osdep.h" 19605d52e6SDmitry Fleytman #include "net_tx_pkt.h" 20605d52e6SDmitry Fleytman #include "net/eth.h" 21605d52e6SDmitry Fleytman #include "net/checksum.h" 22605d52e6SDmitry Fleytman #include "net/tap.h" 23605d52e6SDmitry Fleytman #include "net/net.h" 24edf5ca5dSMarkus Armbruster #include "hw/pci/pci_device.h" 25605d52e6SDmitry Fleytman 26605d52e6SDmitry Fleytman enum { 27605d52e6SDmitry Fleytman NET_TX_PKT_VHDR_FRAG = 0, 28605d52e6SDmitry Fleytman NET_TX_PKT_L2HDR_FRAG, 29605d52e6SDmitry Fleytman NET_TX_PKT_L3HDR_FRAG, 30605d52e6SDmitry Fleytman NET_TX_PKT_PL_START_FRAG 31605d52e6SDmitry Fleytman }; 32605d52e6SDmitry Fleytman 33605d52e6SDmitry Fleytman /* TX packet private context */ 34605d52e6SDmitry Fleytman struct NetTxPkt { 3511171010SDmitry Fleytman PCIDevice *pci_dev; 3611171010SDmitry Fleytman 37605d52e6SDmitry Fleytman struct virtio_net_hdr virt_hdr; 38605d52e6SDmitry Fleytman 39605d52e6SDmitry Fleytman struct iovec *raw; 40605d52e6SDmitry Fleytman uint32_t raw_frags; 41605d52e6SDmitry Fleytman uint32_t max_raw_frags; 42605d52e6SDmitry Fleytman 43605d52e6SDmitry Fleytman struct iovec *vec; 44605d52e6SDmitry Fleytman 45605d52e6SDmitry Fleytman uint8_t l2_hdr[ETH_MAX_L2_HDR_LEN]; 46*2a5f744eSAkihiko Odaki union { 47*2a5f744eSAkihiko Odaki struct ip_header ip; 48*2a5f744eSAkihiko Odaki struct ip6_header ip6; 49*2a5f744eSAkihiko Odaki uint8_t octets[ETH_MAX_IP_DGRAM_LEN]; 50*2a5f744eSAkihiko Odaki } l3_hdr; 51605d52e6SDmitry Fleytman 52605d52e6SDmitry Fleytman uint32_t payload_len; 53605d52e6SDmitry Fleytman 54605d52e6SDmitry Fleytman uint32_t payload_frags; 55605d52e6SDmitry Fleytman uint32_t max_payload_frags; 56605d52e6SDmitry Fleytman 57605d52e6SDmitry Fleytman uint16_t hdr_len; 58605d52e6SDmitry Fleytman eth_pkt_types_e packet_type; 59605d52e6SDmitry Fleytman uint8_t l4proto; 60605d52e6SDmitry Fleytman }; 61605d52e6SDmitry Fleytman 6211171010SDmitry Fleytman void net_tx_pkt_init(struct NetTxPkt **pkt, PCIDevice *pci_dev, 6355daf493SAkihiko Odaki uint32_t max_frags) 64605d52e6SDmitry Fleytman { 65605d52e6SDmitry Fleytman struct NetTxPkt *p = g_malloc0(sizeof *p); 66605d52e6SDmitry Fleytman 6711171010SDmitry Fleytman p->pci_dev = pci_dev; 6811171010SDmitry Fleytman 6947882fa4SLi Qiang p->vec = g_new(struct iovec, max_frags + NET_TX_PKT_PL_START_FRAG); 70605d52e6SDmitry Fleytman 7147882fa4SLi Qiang p->raw = g_new(struct iovec, max_frags); 72605d52e6SDmitry Fleytman 73605d52e6SDmitry Fleytman p->max_payload_frags = max_frags; 74605d52e6SDmitry Fleytman p->max_raw_frags = max_frags; 75605d52e6SDmitry Fleytman p->vec[NET_TX_PKT_VHDR_FRAG].iov_base = &p->virt_hdr; 7655daf493SAkihiko Odaki p->vec[NET_TX_PKT_VHDR_FRAG].iov_len = sizeof p->virt_hdr; 77605d52e6SDmitry Fleytman p->vec[NET_TX_PKT_L2HDR_FRAG].iov_base = &p->l2_hdr; 78eb700029SDmitry Fleytman p->vec[NET_TX_PKT_L3HDR_FRAG].iov_base = &p->l3_hdr; 79605d52e6SDmitry Fleytman 80605d52e6SDmitry Fleytman *pkt = p; 81605d52e6SDmitry Fleytman } 82605d52e6SDmitry Fleytman 83605d52e6SDmitry Fleytman void net_tx_pkt_uninit(struct NetTxPkt *pkt) 84605d52e6SDmitry Fleytman { 85605d52e6SDmitry Fleytman if (pkt) { 86605d52e6SDmitry Fleytman g_free(pkt->vec); 87605d52e6SDmitry Fleytman g_free(pkt->raw); 88605d52e6SDmitry Fleytman g_free(pkt); 89605d52e6SDmitry Fleytman } 90605d52e6SDmitry Fleytman } 91605d52e6SDmitry Fleytman 92eb700029SDmitry Fleytman void net_tx_pkt_update_ip_hdr_checksum(struct NetTxPkt *pkt) 93eb700029SDmitry Fleytman { 94eb700029SDmitry Fleytman uint16_t csum; 95eb700029SDmitry Fleytman assert(pkt); 96eb700029SDmitry Fleytman 97*2a5f744eSAkihiko Odaki pkt->l3_hdr.ip.ip_len = cpu_to_be16(pkt->payload_len + 98eb700029SDmitry Fleytman pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len); 99eb700029SDmitry Fleytman 100*2a5f744eSAkihiko Odaki pkt->l3_hdr.ip.ip_sum = 0; 101*2a5f744eSAkihiko Odaki csum = net_raw_checksum(pkt->l3_hdr.octets, 102eb700029SDmitry Fleytman pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len); 103*2a5f744eSAkihiko Odaki pkt->l3_hdr.ip.ip_sum = cpu_to_be16(csum); 104eb700029SDmitry Fleytman } 105eb700029SDmitry Fleytman 106605d52e6SDmitry Fleytman void net_tx_pkt_update_ip_checksums(struct NetTxPkt *pkt) 107605d52e6SDmitry Fleytman { 108605d52e6SDmitry Fleytman uint16_t csum; 109eb700029SDmitry Fleytman uint32_t cntr, cso; 110605d52e6SDmitry Fleytman assert(pkt); 111605d52e6SDmitry Fleytman uint8_t gso_type = pkt->virt_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN; 112eb700029SDmitry Fleytman void *ip_hdr = pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_base; 113605d52e6SDmitry Fleytman 114605d52e6SDmitry Fleytman if (pkt->payload_len + pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len > 115605d52e6SDmitry Fleytman ETH_MAX_IP_DGRAM_LEN) { 116605d52e6SDmitry Fleytman return; 117605d52e6SDmitry Fleytman } 118605d52e6SDmitry Fleytman 119eb700029SDmitry Fleytman if (gso_type == VIRTIO_NET_HDR_GSO_TCPV4 || 120eb700029SDmitry Fleytman gso_type == VIRTIO_NET_HDR_GSO_UDP) { 121605d52e6SDmitry Fleytman /* Calculate IP header checksum */ 122eb700029SDmitry Fleytman net_tx_pkt_update_ip_hdr_checksum(pkt); 123605d52e6SDmitry Fleytman 124605d52e6SDmitry Fleytman /* Calculate IP pseudo header checksum */ 125eb700029SDmitry Fleytman cntr = eth_calc_ip4_pseudo_hdr_csum(ip_hdr, pkt->payload_len, &cso); 126eb700029SDmitry Fleytman csum = cpu_to_be16(~net_checksum_finish(cntr)); 127eb700029SDmitry Fleytman } else if (gso_type == VIRTIO_NET_HDR_GSO_TCPV6) { 128eb700029SDmitry Fleytman /* Calculate IP pseudo header checksum */ 129eb700029SDmitry Fleytman cntr = eth_calc_ip6_pseudo_hdr_csum(ip_hdr, pkt->payload_len, 130eb700029SDmitry Fleytman IP_PROTO_TCP, &cso); 131eb700029SDmitry Fleytman csum = cpu_to_be16(~net_checksum_finish(cntr)); 132eb700029SDmitry Fleytman } else { 133eb700029SDmitry Fleytman return; 134eb700029SDmitry Fleytman } 135eb700029SDmitry Fleytman 136605d52e6SDmitry Fleytman iov_from_buf(&pkt->vec[NET_TX_PKT_PL_START_FRAG], pkt->payload_frags, 137605d52e6SDmitry Fleytman pkt->virt_hdr.csum_offset, &csum, sizeof(csum)); 138605d52e6SDmitry Fleytman } 139605d52e6SDmitry Fleytman 140605d52e6SDmitry Fleytman static void net_tx_pkt_calculate_hdr_len(struct NetTxPkt *pkt) 141605d52e6SDmitry Fleytman { 142605d52e6SDmitry Fleytman pkt->hdr_len = pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_len + 143605d52e6SDmitry Fleytman pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len; 144605d52e6SDmitry Fleytman } 145605d52e6SDmitry Fleytman 146605d52e6SDmitry Fleytman static bool net_tx_pkt_parse_headers(struct NetTxPkt *pkt) 147605d52e6SDmitry Fleytman { 148605d52e6SDmitry Fleytman struct iovec *l2_hdr, *l3_hdr; 149605d52e6SDmitry Fleytman size_t bytes_read; 150605d52e6SDmitry Fleytman size_t full_ip6hdr_len; 151605d52e6SDmitry Fleytman uint16_t l3_proto; 152605d52e6SDmitry Fleytman 153605d52e6SDmitry Fleytman assert(pkt); 154605d52e6SDmitry Fleytman 155605d52e6SDmitry Fleytman l2_hdr = &pkt->vec[NET_TX_PKT_L2HDR_FRAG]; 156605d52e6SDmitry Fleytman l3_hdr = &pkt->vec[NET_TX_PKT_L3HDR_FRAG]; 157605d52e6SDmitry Fleytman 158605d52e6SDmitry Fleytman bytes_read = iov_to_buf(pkt->raw, pkt->raw_frags, 0, l2_hdr->iov_base, 159605d52e6SDmitry Fleytman ETH_MAX_L2_HDR_LEN); 160605d52e6SDmitry Fleytman if (bytes_read < sizeof(struct eth_header)) { 161605d52e6SDmitry Fleytman l2_hdr->iov_len = 0; 162605d52e6SDmitry Fleytman return false; 163605d52e6SDmitry Fleytman } 164605d52e6SDmitry Fleytman 165605d52e6SDmitry Fleytman l2_hdr->iov_len = sizeof(struct eth_header); 166605d52e6SDmitry Fleytman switch (be16_to_cpu(PKT_GET_ETH_HDR(l2_hdr->iov_base)->h_proto)) { 167605d52e6SDmitry Fleytman case ETH_P_VLAN: 168605d52e6SDmitry Fleytman l2_hdr->iov_len += sizeof(struct vlan_header); 169605d52e6SDmitry Fleytman break; 170605d52e6SDmitry Fleytman case ETH_P_DVLAN: 171605d52e6SDmitry Fleytman l2_hdr->iov_len += 2 * sizeof(struct vlan_header); 172605d52e6SDmitry Fleytman break; 173605d52e6SDmitry Fleytman } 174605d52e6SDmitry Fleytman 175605d52e6SDmitry Fleytman if (bytes_read < l2_hdr->iov_len) { 176605d52e6SDmitry Fleytman l2_hdr->iov_len = 0; 177eb700029SDmitry Fleytman l3_hdr->iov_len = 0; 178eb700029SDmitry Fleytman pkt->packet_type = ETH_PKT_UCAST; 179605d52e6SDmitry Fleytman return false; 180eb700029SDmitry Fleytman } else { 181eb700029SDmitry Fleytman l2_hdr->iov_len = ETH_MAX_L2_HDR_LEN; 182eb700029SDmitry Fleytman l2_hdr->iov_len = eth_get_l2_hdr_length(l2_hdr->iov_base); 183eb700029SDmitry Fleytman pkt->packet_type = get_eth_packet_type(l2_hdr->iov_base); 184605d52e6SDmitry Fleytman } 185605d52e6SDmitry Fleytman 186eb700029SDmitry Fleytman l3_proto = eth_get_l3_proto(l2_hdr, 1, l2_hdr->iov_len); 187605d52e6SDmitry Fleytman 188605d52e6SDmitry Fleytman switch (l3_proto) { 189605d52e6SDmitry Fleytman case ETH_P_IP: 190605d52e6SDmitry Fleytman bytes_read = iov_to_buf(pkt->raw, pkt->raw_frags, l2_hdr->iov_len, 191605d52e6SDmitry Fleytman l3_hdr->iov_base, sizeof(struct ip_header)); 192605d52e6SDmitry Fleytman 193605d52e6SDmitry Fleytman if (bytes_read < sizeof(struct ip_header)) { 194605d52e6SDmitry Fleytman l3_hdr->iov_len = 0; 195605d52e6SDmitry Fleytman return false; 196605d52e6SDmitry Fleytman } 197605d52e6SDmitry Fleytman 198605d52e6SDmitry Fleytman l3_hdr->iov_len = IP_HDR_GET_LEN(l3_hdr->iov_base); 199eb700029SDmitry Fleytman 200eb700029SDmitry Fleytman if (l3_hdr->iov_len < sizeof(struct ip_header)) { 201eb700029SDmitry Fleytman l3_hdr->iov_len = 0; 202eb700029SDmitry Fleytman return false; 203eb700029SDmitry Fleytman } 204eb700029SDmitry Fleytman 2054f51e1d3SMarc-André Lureau pkt->l4proto = IP_HDR_GET_P(l3_hdr->iov_base); 206605d52e6SDmitry Fleytman 207eb700029SDmitry Fleytman if (IP_HDR_GET_LEN(l3_hdr->iov_base) != sizeof(struct ip_header)) { 208eb700029SDmitry Fleytman /* copy optional IPv4 header data if any*/ 209605d52e6SDmitry Fleytman bytes_read = iov_to_buf(pkt->raw, pkt->raw_frags, 210605d52e6SDmitry Fleytman l2_hdr->iov_len + sizeof(struct ip_header), 211605d52e6SDmitry Fleytman l3_hdr->iov_base + sizeof(struct ip_header), 212605d52e6SDmitry Fleytman l3_hdr->iov_len - sizeof(struct ip_header)); 213605d52e6SDmitry Fleytman if (bytes_read < l3_hdr->iov_len - sizeof(struct ip_header)) { 214605d52e6SDmitry Fleytman l3_hdr->iov_len = 0; 215605d52e6SDmitry Fleytman return false; 216605d52e6SDmitry Fleytman } 217eb700029SDmitry Fleytman } 218eb700029SDmitry Fleytman 219605d52e6SDmitry Fleytman break; 220605d52e6SDmitry Fleytman 221605d52e6SDmitry Fleytman case ETH_P_IPV6: 222eb700029SDmitry Fleytman { 223eb700029SDmitry Fleytman eth_ip6_hdr_info hdrinfo; 224eb700029SDmitry Fleytman 225605d52e6SDmitry Fleytman if (!eth_parse_ipv6_hdr(pkt->raw, pkt->raw_frags, l2_hdr->iov_len, 226eb700029SDmitry Fleytman &hdrinfo)) { 227605d52e6SDmitry Fleytman l3_hdr->iov_len = 0; 228605d52e6SDmitry Fleytman return false; 229605d52e6SDmitry Fleytman } 230605d52e6SDmitry Fleytman 231eb700029SDmitry Fleytman pkt->l4proto = hdrinfo.l4proto; 232eb700029SDmitry Fleytman full_ip6hdr_len = hdrinfo.full_hdr_len; 233eb700029SDmitry Fleytman 234eb700029SDmitry Fleytman if (full_ip6hdr_len > ETH_MAX_IP_DGRAM_LEN) { 235eb700029SDmitry Fleytman l3_hdr->iov_len = 0; 236eb700029SDmitry Fleytman return false; 237eb700029SDmitry Fleytman } 238605d52e6SDmitry Fleytman 239605d52e6SDmitry Fleytman bytes_read = iov_to_buf(pkt->raw, pkt->raw_frags, l2_hdr->iov_len, 240605d52e6SDmitry Fleytman l3_hdr->iov_base, full_ip6hdr_len); 241605d52e6SDmitry Fleytman 242605d52e6SDmitry Fleytman if (bytes_read < full_ip6hdr_len) { 243605d52e6SDmitry Fleytman l3_hdr->iov_len = 0; 244605d52e6SDmitry Fleytman return false; 245605d52e6SDmitry Fleytman } else { 246605d52e6SDmitry Fleytman l3_hdr->iov_len = full_ip6hdr_len; 247605d52e6SDmitry Fleytman } 248605d52e6SDmitry Fleytman break; 249eb700029SDmitry Fleytman } 250605d52e6SDmitry Fleytman default: 251605d52e6SDmitry Fleytman l3_hdr->iov_len = 0; 252605d52e6SDmitry Fleytman break; 253605d52e6SDmitry Fleytman } 254605d52e6SDmitry Fleytman 255605d52e6SDmitry Fleytman net_tx_pkt_calculate_hdr_len(pkt); 256605d52e6SDmitry Fleytman return true; 257605d52e6SDmitry Fleytman } 258605d52e6SDmitry Fleytman 259eb700029SDmitry Fleytman static void net_tx_pkt_rebuild_payload(struct NetTxPkt *pkt) 260605d52e6SDmitry Fleytman { 261eb700029SDmitry Fleytman pkt->payload_len = iov_size(pkt->raw, pkt->raw_frags) - pkt->hdr_len; 262605d52e6SDmitry Fleytman pkt->payload_frags = iov_copy(&pkt->vec[NET_TX_PKT_PL_START_FRAG], 263605d52e6SDmitry Fleytman pkt->max_payload_frags, 264605d52e6SDmitry Fleytman pkt->raw, pkt->raw_frags, 265eb700029SDmitry Fleytman pkt->hdr_len, pkt->payload_len); 266605d52e6SDmitry Fleytman } 267605d52e6SDmitry Fleytman 268605d52e6SDmitry Fleytman bool net_tx_pkt_parse(struct NetTxPkt *pkt) 269605d52e6SDmitry Fleytman { 270eb700029SDmitry Fleytman if (net_tx_pkt_parse_headers(pkt)) { 271605d52e6SDmitry Fleytman net_tx_pkt_rebuild_payload(pkt); 272eb700029SDmitry Fleytman return true; 273eb700029SDmitry Fleytman } else { 274eb700029SDmitry Fleytman return false; 275eb700029SDmitry Fleytman } 276605d52e6SDmitry Fleytman } 277605d52e6SDmitry Fleytman 278605d52e6SDmitry Fleytman struct virtio_net_hdr *net_tx_pkt_get_vhdr(struct NetTxPkt *pkt) 279605d52e6SDmitry Fleytman { 280605d52e6SDmitry Fleytman assert(pkt); 281605d52e6SDmitry Fleytman return &pkt->virt_hdr; 282605d52e6SDmitry Fleytman } 283605d52e6SDmitry Fleytman 284605d52e6SDmitry Fleytman static uint8_t net_tx_pkt_get_gso_type(struct NetTxPkt *pkt, 285605d52e6SDmitry Fleytman bool tso_enable) 286605d52e6SDmitry Fleytman { 287605d52e6SDmitry Fleytman uint8_t rc = VIRTIO_NET_HDR_GSO_NONE; 288605d52e6SDmitry Fleytman uint16_t l3_proto; 289605d52e6SDmitry Fleytman 290eb700029SDmitry Fleytman l3_proto = eth_get_l3_proto(&pkt->vec[NET_TX_PKT_L2HDR_FRAG], 1, 291605d52e6SDmitry Fleytman pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_len); 292605d52e6SDmitry Fleytman 293605d52e6SDmitry Fleytman if (!tso_enable) { 294605d52e6SDmitry Fleytman goto func_exit; 295605d52e6SDmitry Fleytman } 296605d52e6SDmitry Fleytman 297605d52e6SDmitry Fleytman rc = eth_get_gso_type(l3_proto, pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_base, 298605d52e6SDmitry Fleytman pkt->l4proto); 299605d52e6SDmitry Fleytman 300605d52e6SDmitry Fleytman func_exit: 301605d52e6SDmitry Fleytman return rc; 302605d52e6SDmitry Fleytman } 303605d52e6SDmitry Fleytman 304f9a9eb16SAkihiko Odaki bool net_tx_pkt_build_vheader(struct NetTxPkt *pkt, bool tso_enable, 305605d52e6SDmitry Fleytman bool csum_enable, uint32_t gso_size) 306605d52e6SDmitry Fleytman { 307605d52e6SDmitry Fleytman struct tcp_hdr l4hdr; 308f9a9eb16SAkihiko Odaki size_t bytes_read; 309605d52e6SDmitry Fleytman assert(pkt); 310605d52e6SDmitry Fleytman 311605d52e6SDmitry Fleytman /* csum has to be enabled if tso is. */ 312605d52e6SDmitry Fleytman assert(csum_enable || !tso_enable); 313605d52e6SDmitry Fleytman 314605d52e6SDmitry Fleytman pkt->virt_hdr.gso_type = net_tx_pkt_get_gso_type(pkt, tso_enable); 315605d52e6SDmitry Fleytman 316605d52e6SDmitry Fleytman switch (pkt->virt_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { 317605d52e6SDmitry Fleytman case VIRTIO_NET_HDR_GSO_NONE: 318605d52e6SDmitry Fleytman pkt->virt_hdr.hdr_len = 0; 319605d52e6SDmitry Fleytman pkt->virt_hdr.gso_size = 0; 320605d52e6SDmitry Fleytman break; 321605d52e6SDmitry Fleytman 322605d52e6SDmitry Fleytman case VIRTIO_NET_HDR_GSO_UDP: 323eb700029SDmitry Fleytman pkt->virt_hdr.gso_size = gso_size; 324605d52e6SDmitry Fleytman pkt->virt_hdr.hdr_len = pkt->hdr_len + sizeof(struct udp_header); 325605d52e6SDmitry Fleytman break; 326605d52e6SDmitry Fleytman 327605d52e6SDmitry Fleytman case VIRTIO_NET_HDR_GSO_TCPV4: 328605d52e6SDmitry Fleytman case VIRTIO_NET_HDR_GSO_TCPV6: 329f9a9eb16SAkihiko Odaki bytes_read = iov_to_buf(&pkt->vec[NET_TX_PKT_PL_START_FRAG], 330f9a9eb16SAkihiko Odaki pkt->payload_frags, 0, &l4hdr, sizeof(l4hdr)); 33102ef5fdcSAkihiko Odaki if (bytes_read < sizeof(l4hdr) || 33202ef5fdcSAkihiko Odaki l4hdr.th_off * sizeof(uint32_t) < sizeof(l4hdr)) { 333f9a9eb16SAkihiko Odaki return false; 334f9a9eb16SAkihiko Odaki } 335f9a9eb16SAkihiko Odaki 336605d52e6SDmitry Fleytman pkt->virt_hdr.hdr_len = pkt->hdr_len + l4hdr.th_off * sizeof(uint32_t); 337eb700029SDmitry Fleytman pkt->virt_hdr.gso_size = gso_size; 338605d52e6SDmitry Fleytman break; 339605d52e6SDmitry Fleytman 340605d52e6SDmitry Fleytman default: 341605d52e6SDmitry Fleytman g_assert_not_reached(); 342605d52e6SDmitry Fleytman } 343605d52e6SDmitry Fleytman 344605d52e6SDmitry Fleytman if (csum_enable) { 345605d52e6SDmitry Fleytman switch (pkt->l4proto) { 346605d52e6SDmitry Fleytman case IP_PROTO_TCP: 347dd32b5eaSAkihiko Odaki if (pkt->payload_len < sizeof(struct tcp_hdr)) { 348dd32b5eaSAkihiko Odaki return false; 349dd32b5eaSAkihiko Odaki } 350605d52e6SDmitry Fleytman pkt->virt_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; 351605d52e6SDmitry Fleytman pkt->virt_hdr.csum_start = pkt->hdr_len; 352605d52e6SDmitry Fleytman pkt->virt_hdr.csum_offset = offsetof(struct tcp_hdr, th_sum); 353605d52e6SDmitry Fleytman break; 354605d52e6SDmitry Fleytman case IP_PROTO_UDP: 355dd32b5eaSAkihiko Odaki if (pkt->payload_len < sizeof(struct udp_hdr)) { 356dd32b5eaSAkihiko Odaki return false; 357dd32b5eaSAkihiko Odaki } 358605d52e6SDmitry Fleytman pkt->virt_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; 359605d52e6SDmitry Fleytman pkt->virt_hdr.csum_start = pkt->hdr_len; 360605d52e6SDmitry Fleytman pkt->virt_hdr.csum_offset = offsetof(struct udp_hdr, uh_sum); 361605d52e6SDmitry Fleytman break; 362605d52e6SDmitry Fleytman default: 363605d52e6SDmitry Fleytman break; 364605d52e6SDmitry Fleytman } 365605d52e6SDmitry Fleytman } 366f9a9eb16SAkihiko Odaki 367f9a9eb16SAkihiko Odaki return true; 368605d52e6SDmitry Fleytman } 369605d52e6SDmitry Fleytman 370eb700029SDmitry Fleytman void net_tx_pkt_setup_vlan_header_ex(struct NetTxPkt *pkt, 371eb700029SDmitry Fleytman uint16_t vlan, uint16_t vlan_ethtype) 372605d52e6SDmitry Fleytman { 373605d52e6SDmitry Fleytman bool is_new; 374605d52e6SDmitry Fleytman assert(pkt); 375605d52e6SDmitry Fleytman 376eb700029SDmitry Fleytman eth_setup_vlan_headers_ex(pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_base, 377eb700029SDmitry Fleytman vlan, vlan_ethtype, &is_new); 378605d52e6SDmitry Fleytman 379605d52e6SDmitry Fleytman /* update l2hdrlen */ 380605d52e6SDmitry Fleytman if (is_new) { 381605d52e6SDmitry Fleytman pkt->hdr_len += sizeof(struct vlan_header); 382605d52e6SDmitry Fleytman pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_len += 383605d52e6SDmitry Fleytman sizeof(struct vlan_header); 384605d52e6SDmitry Fleytman } 385605d52e6SDmitry Fleytman } 386605d52e6SDmitry Fleytman 387605d52e6SDmitry Fleytman bool net_tx_pkt_add_raw_fragment(struct NetTxPkt *pkt, hwaddr pa, 388605d52e6SDmitry Fleytman size_t len) 389605d52e6SDmitry Fleytman { 390605d52e6SDmitry Fleytman hwaddr mapped_len = 0; 391605d52e6SDmitry Fleytman struct iovec *ventry; 392605d52e6SDmitry Fleytman assert(pkt); 393035e69b0SMauro Matteo Cascella 394035e69b0SMauro Matteo Cascella if (pkt->raw_frags >= pkt->max_raw_frags) { 395035e69b0SMauro Matteo Cascella return false; 396035e69b0SMauro Matteo Cascella } 397605d52e6SDmitry Fleytman 398605d52e6SDmitry Fleytman if (!len) { 399605d52e6SDmitry Fleytman return true; 400605d52e6SDmitry Fleytman } 401605d52e6SDmitry Fleytman 402605d52e6SDmitry Fleytman ventry = &pkt->raw[pkt->raw_frags]; 403605d52e6SDmitry Fleytman mapped_len = len; 404605d52e6SDmitry Fleytman 40511171010SDmitry Fleytman ventry->iov_base = pci_dma_map(pkt->pci_dev, pa, 40611171010SDmitry Fleytman &mapped_len, DMA_DIRECTION_TO_DEVICE); 407605d52e6SDmitry Fleytman 408eb700029SDmitry Fleytman if ((ventry->iov_base != NULL) && (len == mapped_len)) { 409eb700029SDmitry Fleytman ventry->iov_len = mapped_len; 410eb700029SDmitry Fleytman pkt->raw_frags++; 411eb700029SDmitry Fleytman return true; 412eb700029SDmitry Fleytman } else { 413605d52e6SDmitry Fleytman return false; 414605d52e6SDmitry Fleytman } 415eb700029SDmitry Fleytman } 416605d52e6SDmitry Fleytman 417eb700029SDmitry Fleytman bool net_tx_pkt_has_fragments(struct NetTxPkt *pkt) 418eb700029SDmitry Fleytman { 419eb700029SDmitry Fleytman return pkt->raw_frags > 0; 420605d52e6SDmitry Fleytman } 421605d52e6SDmitry Fleytman 422605d52e6SDmitry Fleytman eth_pkt_types_e net_tx_pkt_get_packet_type(struct NetTxPkt *pkt) 423605d52e6SDmitry Fleytman { 424605d52e6SDmitry Fleytman assert(pkt); 425605d52e6SDmitry Fleytman 426605d52e6SDmitry Fleytman return pkt->packet_type; 427605d52e6SDmitry Fleytman } 428605d52e6SDmitry Fleytman 429605d52e6SDmitry Fleytman size_t net_tx_pkt_get_total_len(struct NetTxPkt *pkt) 430605d52e6SDmitry Fleytman { 431605d52e6SDmitry Fleytman assert(pkt); 432605d52e6SDmitry Fleytman 433605d52e6SDmitry Fleytman return pkt->hdr_len + pkt->payload_len; 434605d52e6SDmitry Fleytman } 435605d52e6SDmitry Fleytman 436605d52e6SDmitry Fleytman void net_tx_pkt_dump(struct NetTxPkt *pkt) 437605d52e6SDmitry Fleytman { 438605d52e6SDmitry Fleytman #ifdef NET_TX_PKT_DEBUG 439605d52e6SDmitry Fleytman assert(pkt); 440605d52e6SDmitry Fleytman 441605d52e6SDmitry Fleytman printf("TX PKT: hdr_len: %d, pkt_type: 0x%X, l2hdr_len: %lu, " 442605d52e6SDmitry Fleytman "l3hdr_len: %lu, payload_len: %u\n", pkt->hdr_len, pkt->packet_type, 443605d52e6SDmitry Fleytman pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_len, 444605d52e6SDmitry Fleytman pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len, pkt->payload_len); 445605d52e6SDmitry Fleytman #endif 446605d52e6SDmitry Fleytman } 447605d52e6SDmitry Fleytman 448f4fdaf00SAkihiko Odaki void net_tx_pkt_reset(struct NetTxPkt *pkt, PCIDevice *pci_dev) 449605d52e6SDmitry Fleytman { 450605d52e6SDmitry Fleytman int i; 451605d52e6SDmitry Fleytman 452605d52e6SDmitry Fleytman /* no assert, as reset can be called before tx_pkt_init */ 453605d52e6SDmitry Fleytman if (!pkt) { 454605d52e6SDmitry Fleytman return; 455605d52e6SDmitry Fleytman } 456605d52e6SDmitry Fleytman 457605d52e6SDmitry Fleytman memset(&pkt->virt_hdr, 0, sizeof(pkt->virt_hdr)); 458605d52e6SDmitry Fleytman 459605d52e6SDmitry Fleytman assert(pkt->vec); 460eb700029SDmitry Fleytman 461605d52e6SDmitry Fleytman pkt->payload_len = 0; 462605d52e6SDmitry Fleytman pkt->payload_frags = 0; 463605d52e6SDmitry Fleytman 464283f0a05SThomas Huth if (pkt->max_raw_frags > 0) { 465605d52e6SDmitry Fleytman assert(pkt->raw); 466605d52e6SDmitry Fleytman for (i = 0; i < pkt->raw_frags; i++) { 467605d52e6SDmitry Fleytman assert(pkt->raw[i].iov_base); 468283f0a05SThomas Huth pci_dma_unmap(pkt->pci_dev, pkt->raw[i].iov_base, 469283f0a05SThomas Huth pkt->raw[i].iov_len, DMA_DIRECTION_TO_DEVICE, 0); 470283f0a05SThomas Huth } 471605d52e6SDmitry Fleytman } 472f4fdaf00SAkihiko Odaki pkt->pci_dev = pci_dev; 473605d52e6SDmitry Fleytman pkt->raw_frags = 0; 474605d52e6SDmitry Fleytman 475605d52e6SDmitry Fleytman pkt->hdr_len = 0; 476605d52e6SDmitry Fleytman pkt->l4proto = 0; 477605d52e6SDmitry Fleytman } 478605d52e6SDmitry Fleytman 47902ef5fdcSAkihiko Odaki static void net_tx_pkt_do_sw_csum(struct NetTxPkt *pkt, 48002ef5fdcSAkihiko Odaki struct iovec *iov, uint32_t iov_len, 48102ef5fdcSAkihiko Odaki uint16_t csl) 482605d52e6SDmitry Fleytman { 483605d52e6SDmitry Fleytman uint32_t csum_cntr; 484605d52e6SDmitry Fleytman uint16_t csum = 0; 485eb700029SDmitry Fleytman uint32_t cso; 486605d52e6SDmitry Fleytman /* num of iovec without vhdr */ 487605d52e6SDmitry Fleytman size_t csum_offset = pkt->virt_hdr.csum_start + pkt->virt_hdr.csum_offset; 4889a8d9492SAndrew uint16_t l3_proto = eth_get_l3_proto(iov, 1, iov->iov_len); 489605d52e6SDmitry Fleytman 490605d52e6SDmitry Fleytman /* Put zero to checksum field */ 491605d52e6SDmitry Fleytman iov_from_buf(iov, iov_len, csum_offset, &csum, sizeof csum); 492605d52e6SDmitry Fleytman 493605d52e6SDmitry Fleytman /* Calculate L4 TCP/UDP checksum */ 4949a8d9492SAndrew csum_cntr = 0; 4959a8d9492SAndrew cso = 0; 496605d52e6SDmitry Fleytman /* add pseudo header to csum */ 4979a8d9492SAndrew if (l3_proto == ETH_P_IP) { 4989a8d9492SAndrew csum_cntr = eth_calc_ip4_pseudo_hdr_csum( 4999a8d9492SAndrew pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_base, 5009a8d9492SAndrew csl, &cso); 5019a8d9492SAndrew } else if (l3_proto == ETH_P_IPV6) { 5029a8d9492SAndrew csum_cntr = eth_calc_ip6_pseudo_hdr_csum( 5039a8d9492SAndrew pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_base, 5049a8d9492SAndrew csl, pkt->l4proto, &cso); 5059a8d9492SAndrew } 506eb700029SDmitry Fleytman 507eb700029SDmitry Fleytman /* data checksum */ 508eb700029SDmitry Fleytman csum_cntr += 509eb700029SDmitry Fleytman net_checksum_add_iov(iov, iov_len, pkt->virt_hdr.csum_start, csl, cso); 510605d52e6SDmitry Fleytman 511605d52e6SDmitry Fleytman /* Put the checksum obtained into the packet */ 5120dacea92SEd Swierk csum = cpu_to_be16(net_checksum_finish_nozero(csum_cntr)); 513605d52e6SDmitry Fleytman iov_from_buf(iov, iov_len, csum_offset, &csum, sizeof csum); 514605d52e6SDmitry Fleytman } 515605d52e6SDmitry Fleytman 516605d52e6SDmitry Fleytman #define NET_MAX_FRAG_SG_LIST (64) 517605d52e6SDmitry Fleytman 518605d52e6SDmitry Fleytman static size_t net_tx_pkt_fetch_fragment(struct NetTxPkt *pkt, 51902ef5fdcSAkihiko Odaki int *src_idx, size_t *src_offset, size_t src_len, 52002ef5fdcSAkihiko Odaki struct iovec *dst, int *dst_idx) 521605d52e6SDmitry Fleytman { 522605d52e6SDmitry Fleytman size_t fetched = 0; 523605d52e6SDmitry Fleytman struct iovec *src = pkt->vec; 524605d52e6SDmitry Fleytman 52502ef5fdcSAkihiko Odaki while (fetched < src_len) { 526605d52e6SDmitry Fleytman 527605d52e6SDmitry Fleytman /* no more place in fragment iov */ 528605d52e6SDmitry Fleytman if (*dst_idx == NET_MAX_FRAG_SG_LIST) { 529605d52e6SDmitry Fleytman break; 530605d52e6SDmitry Fleytman } 531605d52e6SDmitry Fleytman 532605d52e6SDmitry Fleytman /* no more data in iovec */ 533605d52e6SDmitry Fleytman if (*src_idx == (pkt->payload_frags + NET_TX_PKT_PL_START_FRAG)) { 534605d52e6SDmitry Fleytman break; 535605d52e6SDmitry Fleytman } 536605d52e6SDmitry Fleytman 537605d52e6SDmitry Fleytman 538605d52e6SDmitry Fleytman dst[*dst_idx].iov_base = src[*src_idx].iov_base + *src_offset; 539605d52e6SDmitry Fleytman dst[*dst_idx].iov_len = MIN(src[*src_idx].iov_len - *src_offset, 54002ef5fdcSAkihiko Odaki src_len - fetched); 541605d52e6SDmitry Fleytman 542605d52e6SDmitry Fleytman *src_offset += dst[*dst_idx].iov_len; 543605d52e6SDmitry Fleytman fetched += dst[*dst_idx].iov_len; 544605d52e6SDmitry Fleytman 545605d52e6SDmitry Fleytman if (*src_offset == src[*src_idx].iov_len) { 546605d52e6SDmitry Fleytman *src_offset = 0; 547605d52e6SDmitry Fleytman (*src_idx)++; 548605d52e6SDmitry Fleytman } 549605d52e6SDmitry Fleytman 550605d52e6SDmitry Fleytman (*dst_idx)++; 551605d52e6SDmitry Fleytman } 552605d52e6SDmitry Fleytman 553605d52e6SDmitry Fleytman return fetched; 554605d52e6SDmitry Fleytman } 555605d52e6SDmitry Fleytman 556ffbd2dbdSAkihiko Odaki static void net_tx_pkt_sendv( 557ffbd2dbdSAkihiko Odaki void *opaque, const struct iovec *iov, int iov_cnt, 558ffbd2dbdSAkihiko Odaki const struct iovec *virt_iov, int virt_iov_cnt) 559eb700029SDmitry Fleytman { 560ffbd2dbdSAkihiko Odaki NetClientState *nc = opaque; 561ffbd2dbdSAkihiko Odaki 562ffbd2dbdSAkihiko Odaki if (qemu_get_using_vnet_hdr(nc->peer)) { 563ffbd2dbdSAkihiko Odaki qemu_sendv_packet(nc, virt_iov, virt_iov_cnt); 564eb700029SDmitry Fleytman } else { 565eb700029SDmitry Fleytman qemu_sendv_packet(nc, iov, iov_cnt); 566eb700029SDmitry Fleytman } 567eb700029SDmitry Fleytman } 568eb700029SDmitry Fleytman 56902ef5fdcSAkihiko Odaki static bool net_tx_pkt_tcp_fragment_init(struct NetTxPkt *pkt, 57002ef5fdcSAkihiko Odaki struct iovec *fragment, 57102ef5fdcSAkihiko Odaki int *pl_idx, 57202ef5fdcSAkihiko Odaki size_t *l4hdr_len, 57302ef5fdcSAkihiko Odaki int *src_idx, 57402ef5fdcSAkihiko Odaki size_t *src_offset, 57502ef5fdcSAkihiko Odaki size_t *src_len) 57602ef5fdcSAkihiko Odaki { 57702ef5fdcSAkihiko Odaki struct iovec *l4 = fragment + NET_TX_PKT_PL_START_FRAG; 57802ef5fdcSAkihiko Odaki size_t bytes_read = 0; 57902ef5fdcSAkihiko Odaki struct tcp_hdr *th; 58002ef5fdcSAkihiko Odaki 58102ef5fdcSAkihiko Odaki if (!pkt->payload_frags) { 58202ef5fdcSAkihiko Odaki return false; 58302ef5fdcSAkihiko Odaki } 58402ef5fdcSAkihiko Odaki 58502ef5fdcSAkihiko Odaki l4->iov_len = pkt->virt_hdr.hdr_len - pkt->hdr_len; 58602ef5fdcSAkihiko Odaki l4->iov_base = g_malloc(l4->iov_len); 58702ef5fdcSAkihiko Odaki 58802ef5fdcSAkihiko Odaki *src_idx = NET_TX_PKT_PL_START_FRAG; 58902ef5fdcSAkihiko Odaki while (pkt->vec[*src_idx].iov_len < l4->iov_len - bytes_read) { 59002ef5fdcSAkihiko Odaki memcpy((char *)l4->iov_base + bytes_read, pkt->vec[*src_idx].iov_base, 59102ef5fdcSAkihiko Odaki pkt->vec[*src_idx].iov_len); 59202ef5fdcSAkihiko Odaki 59302ef5fdcSAkihiko Odaki bytes_read += pkt->vec[*src_idx].iov_len; 59402ef5fdcSAkihiko Odaki 59502ef5fdcSAkihiko Odaki (*src_idx)++; 59602ef5fdcSAkihiko Odaki if (*src_idx >= pkt->payload_frags + NET_TX_PKT_PL_START_FRAG) { 59702ef5fdcSAkihiko Odaki g_free(l4->iov_base); 59802ef5fdcSAkihiko Odaki return false; 59902ef5fdcSAkihiko Odaki } 60002ef5fdcSAkihiko Odaki } 60102ef5fdcSAkihiko Odaki 60202ef5fdcSAkihiko Odaki *src_offset = l4->iov_len - bytes_read; 60302ef5fdcSAkihiko Odaki memcpy((char *)l4->iov_base + bytes_read, pkt->vec[*src_idx].iov_base, 60402ef5fdcSAkihiko Odaki *src_offset); 60502ef5fdcSAkihiko Odaki 60602ef5fdcSAkihiko Odaki th = l4->iov_base; 60702ef5fdcSAkihiko Odaki th->th_flags &= ~(TH_FIN | TH_PUSH); 60802ef5fdcSAkihiko Odaki 60902ef5fdcSAkihiko Odaki *pl_idx = NET_TX_PKT_PL_START_FRAG + 1; 61002ef5fdcSAkihiko Odaki *l4hdr_len = l4->iov_len; 61102ef5fdcSAkihiko Odaki *src_len = pkt->virt_hdr.gso_size; 61202ef5fdcSAkihiko Odaki 61302ef5fdcSAkihiko Odaki return true; 61402ef5fdcSAkihiko Odaki } 61502ef5fdcSAkihiko Odaki 61602ef5fdcSAkihiko Odaki static void net_tx_pkt_tcp_fragment_deinit(struct iovec *fragment) 61702ef5fdcSAkihiko Odaki { 61802ef5fdcSAkihiko Odaki g_free(fragment[NET_TX_PKT_PL_START_FRAG].iov_base); 61902ef5fdcSAkihiko Odaki } 62002ef5fdcSAkihiko Odaki 62102ef5fdcSAkihiko Odaki static void net_tx_pkt_tcp_fragment_fix(struct NetTxPkt *pkt, 62202ef5fdcSAkihiko Odaki struct iovec *fragment, 62302ef5fdcSAkihiko Odaki size_t fragment_len, 62402ef5fdcSAkihiko Odaki uint8_t gso_type) 62502ef5fdcSAkihiko Odaki { 62602ef5fdcSAkihiko Odaki struct iovec *l3hdr = fragment + NET_TX_PKT_L3HDR_FRAG; 62702ef5fdcSAkihiko Odaki struct iovec *l4hdr = fragment + NET_TX_PKT_PL_START_FRAG; 62802ef5fdcSAkihiko Odaki struct ip_header *ip = l3hdr->iov_base; 62902ef5fdcSAkihiko Odaki struct ip6_header *ip6 = l3hdr->iov_base; 63002ef5fdcSAkihiko Odaki size_t len = l3hdr->iov_len + l4hdr->iov_len + fragment_len; 63102ef5fdcSAkihiko Odaki 63202ef5fdcSAkihiko Odaki switch (gso_type) { 63302ef5fdcSAkihiko Odaki case VIRTIO_NET_HDR_GSO_TCPV4: 63402ef5fdcSAkihiko Odaki ip->ip_len = cpu_to_be16(len); 63502ef5fdcSAkihiko Odaki eth_fix_ip4_checksum(l3hdr->iov_base, l3hdr->iov_len); 63602ef5fdcSAkihiko Odaki break; 63702ef5fdcSAkihiko Odaki 63802ef5fdcSAkihiko Odaki case VIRTIO_NET_HDR_GSO_TCPV6: 63902ef5fdcSAkihiko Odaki len -= sizeof(struct ip6_header); 64002ef5fdcSAkihiko Odaki ip6->ip6_ctlun.ip6_un1.ip6_un1_plen = cpu_to_be16(len); 64102ef5fdcSAkihiko Odaki break; 64202ef5fdcSAkihiko Odaki } 64302ef5fdcSAkihiko Odaki } 64402ef5fdcSAkihiko Odaki 64502ef5fdcSAkihiko Odaki static void net_tx_pkt_tcp_fragment_advance(struct NetTxPkt *pkt, 64602ef5fdcSAkihiko Odaki struct iovec *fragment, 64702ef5fdcSAkihiko Odaki size_t fragment_len, 64802ef5fdcSAkihiko Odaki uint8_t gso_type) 64902ef5fdcSAkihiko Odaki { 65002ef5fdcSAkihiko Odaki struct iovec *l3hdr = fragment + NET_TX_PKT_L3HDR_FRAG; 65102ef5fdcSAkihiko Odaki struct iovec *l4hdr = fragment + NET_TX_PKT_PL_START_FRAG; 65202ef5fdcSAkihiko Odaki struct ip_header *ip = l3hdr->iov_base; 65302ef5fdcSAkihiko Odaki struct tcp_hdr *th = l4hdr->iov_base; 65402ef5fdcSAkihiko Odaki 65502ef5fdcSAkihiko Odaki if (gso_type == VIRTIO_NET_HDR_GSO_TCPV4) { 65602ef5fdcSAkihiko Odaki ip->ip_id = cpu_to_be16(be16_to_cpu(ip->ip_id) + 1); 65702ef5fdcSAkihiko Odaki } 65802ef5fdcSAkihiko Odaki 65902ef5fdcSAkihiko Odaki th->th_seq = cpu_to_be32(be32_to_cpu(th->th_seq) + fragment_len); 66002ef5fdcSAkihiko Odaki th->th_flags &= ~TH_CWR; 66102ef5fdcSAkihiko Odaki } 66202ef5fdcSAkihiko Odaki 66302ef5fdcSAkihiko Odaki static void net_tx_pkt_udp_fragment_init(struct NetTxPkt *pkt, 66402ef5fdcSAkihiko Odaki int *pl_idx, 66502ef5fdcSAkihiko Odaki size_t *l4hdr_len, 66602ef5fdcSAkihiko Odaki int *src_idx, size_t *src_offset, 66702ef5fdcSAkihiko Odaki size_t *src_len) 66802ef5fdcSAkihiko Odaki { 66902ef5fdcSAkihiko Odaki *pl_idx = NET_TX_PKT_PL_START_FRAG; 67002ef5fdcSAkihiko Odaki *l4hdr_len = 0; 67102ef5fdcSAkihiko Odaki *src_idx = NET_TX_PKT_PL_START_FRAG; 67202ef5fdcSAkihiko Odaki *src_offset = 0; 67302ef5fdcSAkihiko Odaki *src_len = IP_FRAG_ALIGN_SIZE(pkt->virt_hdr.gso_size); 67402ef5fdcSAkihiko Odaki } 67502ef5fdcSAkihiko Odaki 67602ef5fdcSAkihiko Odaki static void net_tx_pkt_udp_fragment_fix(struct NetTxPkt *pkt, 67702ef5fdcSAkihiko Odaki struct iovec *fragment, 67802ef5fdcSAkihiko Odaki size_t fragment_offset, 67902ef5fdcSAkihiko Odaki size_t fragment_len) 68002ef5fdcSAkihiko Odaki { 68102ef5fdcSAkihiko Odaki bool more_frags = fragment_offset + fragment_len < pkt->payload_len; 68202ef5fdcSAkihiko Odaki uint16_t orig_flags; 68302ef5fdcSAkihiko Odaki struct iovec *l3hdr = fragment + NET_TX_PKT_L3HDR_FRAG; 68402ef5fdcSAkihiko Odaki struct ip_header *ip = l3hdr->iov_base; 68502ef5fdcSAkihiko Odaki uint16_t frag_off_units = fragment_offset / IP_FRAG_UNIT_SIZE; 68602ef5fdcSAkihiko Odaki uint16_t new_ip_off; 68702ef5fdcSAkihiko Odaki 68802ef5fdcSAkihiko Odaki assert(fragment_offset % IP_FRAG_UNIT_SIZE == 0); 68902ef5fdcSAkihiko Odaki assert((frag_off_units & ~IP_OFFMASK) == 0); 69002ef5fdcSAkihiko Odaki 69102ef5fdcSAkihiko Odaki orig_flags = be16_to_cpu(ip->ip_off) & ~(IP_OFFMASK | IP_MF); 69202ef5fdcSAkihiko Odaki new_ip_off = frag_off_units | orig_flags | (more_frags ? IP_MF : 0); 69302ef5fdcSAkihiko Odaki ip->ip_off = cpu_to_be16(new_ip_off); 69402ef5fdcSAkihiko Odaki ip->ip_len = cpu_to_be16(l3hdr->iov_len + fragment_len); 69502ef5fdcSAkihiko Odaki 69602ef5fdcSAkihiko Odaki eth_fix_ip4_checksum(l3hdr->iov_base, l3hdr->iov_len); 69702ef5fdcSAkihiko Odaki } 69802ef5fdcSAkihiko Odaki 699605d52e6SDmitry Fleytman static bool net_tx_pkt_do_sw_fragmentation(struct NetTxPkt *pkt, 700ffbd2dbdSAkihiko Odaki NetTxPktCallback callback, 701ffbd2dbdSAkihiko Odaki void *context) 702605d52e6SDmitry Fleytman { 70302ef5fdcSAkihiko Odaki uint8_t gso_type = pkt->virt_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN; 704605d52e6SDmitry Fleytman 70502ef5fdcSAkihiko Odaki struct iovec fragment[NET_MAX_FRAG_SG_LIST]; 70602ef5fdcSAkihiko Odaki size_t fragment_len; 70702ef5fdcSAkihiko Odaki size_t l4hdr_len; 70802ef5fdcSAkihiko Odaki size_t src_len; 70902ef5fdcSAkihiko Odaki 71002ef5fdcSAkihiko Odaki int src_idx, dst_idx, pl_idx; 71102ef5fdcSAkihiko Odaki size_t src_offset; 712605d52e6SDmitry Fleytman size_t fragment_offset = 0; 713ffbd2dbdSAkihiko Odaki struct virtio_net_hdr virt_hdr = { 714ffbd2dbdSAkihiko Odaki .flags = pkt->virt_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM ? 715ffbd2dbdSAkihiko Odaki VIRTIO_NET_HDR_F_DATA_VALID : 0 716ffbd2dbdSAkihiko Odaki }; 717605d52e6SDmitry Fleytman 718605d52e6SDmitry Fleytman /* Copy headers */ 719ffbd2dbdSAkihiko Odaki fragment[NET_TX_PKT_VHDR_FRAG].iov_base = &virt_hdr; 720ffbd2dbdSAkihiko Odaki fragment[NET_TX_PKT_VHDR_FRAG].iov_len = sizeof(virt_hdr); 72102ef5fdcSAkihiko Odaki fragment[NET_TX_PKT_L2HDR_FRAG] = pkt->vec[NET_TX_PKT_L2HDR_FRAG]; 72202ef5fdcSAkihiko Odaki fragment[NET_TX_PKT_L3HDR_FRAG] = pkt->vec[NET_TX_PKT_L3HDR_FRAG]; 723605d52e6SDmitry Fleytman 72402ef5fdcSAkihiko Odaki switch (gso_type) { 72502ef5fdcSAkihiko Odaki case VIRTIO_NET_HDR_GSO_TCPV4: 72602ef5fdcSAkihiko Odaki case VIRTIO_NET_HDR_GSO_TCPV6: 72702ef5fdcSAkihiko Odaki if (!net_tx_pkt_tcp_fragment_init(pkt, fragment, &pl_idx, &l4hdr_len, 72802ef5fdcSAkihiko Odaki &src_idx, &src_offset, &src_len)) { 72902ef5fdcSAkihiko Odaki return false; 73002ef5fdcSAkihiko Odaki } 73102ef5fdcSAkihiko Odaki break; 73202ef5fdcSAkihiko Odaki 73302ef5fdcSAkihiko Odaki case VIRTIO_NET_HDR_GSO_UDP: 73402ef5fdcSAkihiko Odaki net_tx_pkt_do_sw_csum(pkt, &pkt->vec[NET_TX_PKT_L2HDR_FRAG], 73502ef5fdcSAkihiko Odaki pkt->payload_frags + NET_TX_PKT_PL_START_FRAG - 1, 73602ef5fdcSAkihiko Odaki pkt->payload_len); 73702ef5fdcSAkihiko Odaki net_tx_pkt_udp_fragment_init(pkt, &pl_idx, &l4hdr_len, 73802ef5fdcSAkihiko Odaki &src_idx, &src_offset, &src_len); 73902ef5fdcSAkihiko Odaki break; 74002ef5fdcSAkihiko Odaki 74102ef5fdcSAkihiko Odaki default: 74202ef5fdcSAkihiko Odaki abort(); 74302ef5fdcSAkihiko Odaki } 744605d52e6SDmitry Fleytman 745605d52e6SDmitry Fleytman /* Put as much data as possible and send */ 74602ef5fdcSAkihiko Odaki while (true) { 74702ef5fdcSAkihiko Odaki dst_idx = pl_idx; 74802ef5fdcSAkihiko Odaki fragment_len = net_tx_pkt_fetch_fragment(pkt, 74902ef5fdcSAkihiko Odaki &src_idx, &src_offset, src_len, fragment, &dst_idx); 75002ef5fdcSAkihiko Odaki if (!fragment_len) { 75102ef5fdcSAkihiko Odaki break; 75202ef5fdcSAkihiko Odaki } 753605d52e6SDmitry Fleytman 75402ef5fdcSAkihiko Odaki switch (gso_type) { 75502ef5fdcSAkihiko Odaki case VIRTIO_NET_HDR_GSO_TCPV4: 75602ef5fdcSAkihiko Odaki case VIRTIO_NET_HDR_GSO_TCPV6: 75702ef5fdcSAkihiko Odaki net_tx_pkt_tcp_fragment_fix(pkt, fragment, fragment_len, gso_type); 75802ef5fdcSAkihiko Odaki net_tx_pkt_do_sw_csum(pkt, fragment + NET_TX_PKT_L2HDR_FRAG, 75902ef5fdcSAkihiko Odaki dst_idx - NET_TX_PKT_L2HDR_FRAG, 76002ef5fdcSAkihiko Odaki l4hdr_len + fragment_len); 76102ef5fdcSAkihiko Odaki break; 762605d52e6SDmitry Fleytman 76302ef5fdcSAkihiko Odaki case VIRTIO_NET_HDR_GSO_UDP: 76402ef5fdcSAkihiko Odaki net_tx_pkt_udp_fragment_fix(pkt, fragment, fragment_offset, 76502ef5fdcSAkihiko Odaki fragment_len); 76602ef5fdcSAkihiko Odaki break; 76702ef5fdcSAkihiko Odaki } 768605d52e6SDmitry Fleytman 769ffbd2dbdSAkihiko Odaki callback(context, 770ffbd2dbdSAkihiko Odaki fragment + NET_TX_PKT_L2HDR_FRAG, dst_idx - NET_TX_PKT_L2HDR_FRAG, 771ffbd2dbdSAkihiko Odaki fragment + NET_TX_PKT_VHDR_FRAG, dst_idx - NET_TX_PKT_VHDR_FRAG); 772605d52e6SDmitry Fleytman 77302ef5fdcSAkihiko Odaki if (gso_type == VIRTIO_NET_HDR_GSO_TCPV4 || 77402ef5fdcSAkihiko Odaki gso_type == VIRTIO_NET_HDR_GSO_TCPV6) { 77502ef5fdcSAkihiko Odaki net_tx_pkt_tcp_fragment_advance(pkt, fragment, fragment_len, 77602ef5fdcSAkihiko Odaki gso_type); 77702ef5fdcSAkihiko Odaki } 778605d52e6SDmitry Fleytman 77902ef5fdcSAkihiko Odaki fragment_offset += fragment_len; 78002ef5fdcSAkihiko Odaki } 78102ef5fdcSAkihiko Odaki 78202ef5fdcSAkihiko Odaki if (gso_type == VIRTIO_NET_HDR_GSO_TCPV4 || 78302ef5fdcSAkihiko Odaki gso_type == VIRTIO_NET_HDR_GSO_TCPV6) { 78402ef5fdcSAkihiko Odaki net_tx_pkt_tcp_fragment_deinit(fragment); 78502ef5fdcSAkihiko Odaki } 786605d52e6SDmitry Fleytman 787605d52e6SDmitry Fleytman return true; 788605d52e6SDmitry Fleytman } 789605d52e6SDmitry Fleytman 790605d52e6SDmitry Fleytman bool net_tx_pkt_send(struct NetTxPkt *pkt, NetClientState *nc) 791605d52e6SDmitry Fleytman { 792ffbd2dbdSAkihiko Odaki bool offload = qemu_get_using_vnet_hdr(nc->peer); 793ffbd2dbdSAkihiko Odaki return net_tx_pkt_send_custom(pkt, offload, net_tx_pkt_sendv, nc); 794ffbd2dbdSAkihiko Odaki } 79555daf493SAkihiko Odaki 796ffbd2dbdSAkihiko Odaki bool net_tx_pkt_send_custom(struct NetTxPkt *pkt, bool offload, 797ffbd2dbdSAkihiko Odaki NetTxPktCallback callback, void *context) 798ffbd2dbdSAkihiko Odaki { 799605d52e6SDmitry Fleytman assert(pkt); 800605d52e6SDmitry Fleytman 8014cf3a638SAkihiko Odaki uint8_t gso_type = pkt->virt_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN; 8024cf3a638SAkihiko Odaki 803605d52e6SDmitry Fleytman /* 804605d52e6SDmitry Fleytman * Since underlying infrastructure does not support IP datagrams longer 805605d52e6SDmitry Fleytman * than 64K we should drop such packets and don't even try to send 806605d52e6SDmitry Fleytman */ 8074cf3a638SAkihiko Odaki if (VIRTIO_NET_HDR_GSO_NONE != gso_type) { 808605d52e6SDmitry Fleytman if (pkt->payload_len > 809605d52e6SDmitry Fleytman ETH_MAX_IP_DGRAM_LEN - 810605d52e6SDmitry Fleytman pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len) { 811605d52e6SDmitry Fleytman return false; 812605d52e6SDmitry Fleytman } 813605d52e6SDmitry Fleytman } 814605d52e6SDmitry Fleytman 8154cf3a638SAkihiko Odaki if (offload || gso_type == VIRTIO_NET_HDR_GSO_NONE) { 81602ef5fdcSAkihiko Odaki if (!offload && pkt->virt_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { 81702ef5fdcSAkihiko Odaki net_tx_pkt_do_sw_csum(pkt, &pkt->vec[NET_TX_PKT_L2HDR_FRAG], 81802ef5fdcSAkihiko Odaki pkt->payload_frags + NET_TX_PKT_PL_START_FRAG - 1, 81902ef5fdcSAkihiko Odaki pkt->payload_len); 82002ef5fdcSAkihiko Odaki } 82102ef5fdcSAkihiko Odaki 822e219d309SAndrew net_tx_pkt_fix_ip6_payload_len(pkt); 823ffbd2dbdSAkihiko Odaki callback(context, pkt->vec + NET_TX_PKT_L2HDR_FRAG, 824ffbd2dbdSAkihiko Odaki pkt->payload_frags + NET_TX_PKT_PL_START_FRAG - NET_TX_PKT_L2HDR_FRAG, 825ffbd2dbdSAkihiko Odaki pkt->vec + NET_TX_PKT_VHDR_FRAG, 826ffbd2dbdSAkihiko Odaki pkt->payload_frags + NET_TX_PKT_PL_START_FRAG - NET_TX_PKT_VHDR_FRAG); 827605d52e6SDmitry Fleytman return true; 828605d52e6SDmitry Fleytman } 829605d52e6SDmitry Fleytman 830ffbd2dbdSAkihiko Odaki return net_tx_pkt_do_sw_fragmentation(pkt, callback, context); 831eb700029SDmitry Fleytman } 832e219d309SAndrew 833e219d309SAndrew void net_tx_pkt_fix_ip6_payload_len(struct NetTxPkt *pkt) 834e219d309SAndrew { 835e219d309SAndrew struct iovec *l2 = &pkt->vec[NET_TX_PKT_L2HDR_FRAG]; 836e219d309SAndrew if (eth_get_l3_proto(l2, 1, l2->iov_len) == ETH_P_IPV6) { 837e219d309SAndrew /* 838e219d309SAndrew * TODO: if qemu would support >64K packets - add jumbo option check 839e219d309SAndrew * something like that: 840e219d309SAndrew * 'if (ip6->ip6_plen == 0 && !has_jumbo_option(ip6)) {' 841e219d309SAndrew */ 842*2a5f744eSAkihiko Odaki if (pkt->l3_hdr.ip6.ip6_plen == 0) { 843e219d309SAndrew if (pkt->payload_len <= ETH_MAX_IP_DGRAM_LEN) { 844*2a5f744eSAkihiko Odaki pkt->l3_hdr.ip6.ip6_plen = htons(pkt->payload_len); 845e219d309SAndrew } 846e219d309SAndrew /* 847e219d309SAndrew * TODO: if qemu would support >64K packets 848e219d309SAndrew * add jumbo option for packets greater then 65,535 bytes 849e219d309SAndrew */ 850e219d309SAndrew } 851e219d309SAndrew } 852e219d309SAndrew } 853