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]; 46eb700029SDmitry Fleytman uint8_t l3_hdr[ETH_MAX_IP_DGRAM_LEN]; 47605d52e6SDmitry Fleytman 48605d52e6SDmitry Fleytman uint32_t payload_len; 49605d52e6SDmitry Fleytman 50605d52e6SDmitry Fleytman uint32_t payload_frags; 51605d52e6SDmitry Fleytman uint32_t max_payload_frags; 52605d52e6SDmitry Fleytman 53605d52e6SDmitry Fleytman uint16_t hdr_len; 54605d52e6SDmitry Fleytman eth_pkt_types_e packet_type; 55605d52e6SDmitry Fleytman uint8_t l4proto; 56605d52e6SDmitry Fleytman }; 57605d52e6SDmitry Fleytman 5811171010SDmitry Fleytman void net_tx_pkt_init(struct NetTxPkt **pkt, PCIDevice *pci_dev, 5955daf493SAkihiko Odaki uint32_t max_frags) 60605d52e6SDmitry Fleytman { 61605d52e6SDmitry Fleytman struct NetTxPkt *p = g_malloc0(sizeof *p); 62605d52e6SDmitry Fleytman 6311171010SDmitry Fleytman p->pci_dev = pci_dev; 6411171010SDmitry Fleytman 6547882fa4SLi Qiang p->vec = g_new(struct iovec, max_frags + NET_TX_PKT_PL_START_FRAG); 66605d52e6SDmitry Fleytman 6747882fa4SLi Qiang p->raw = g_new(struct iovec, max_frags); 68605d52e6SDmitry Fleytman 69605d52e6SDmitry Fleytman p->max_payload_frags = max_frags; 70605d52e6SDmitry Fleytman p->max_raw_frags = max_frags; 71605d52e6SDmitry Fleytman p->vec[NET_TX_PKT_VHDR_FRAG].iov_base = &p->virt_hdr; 7255daf493SAkihiko Odaki p->vec[NET_TX_PKT_VHDR_FRAG].iov_len = sizeof p->virt_hdr; 73605d52e6SDmitry Fleytman p->vec[NET_TX_PKT_L2HDR_FRAG].iov_base = &p->l2_hdr; 74eb700029SDmitry Fleytman p->vec[NET_TX_PKT_L3HDR_FRAG].iov_base = &p->l3_hdr; 75605d52e6SDmitry Fleytman 76605d52e6SDmitry Fleytman *pkt = p; 77605d52e6SDmitry Fleytman } 78605d52e6SDmitry Fleytman 79605d52e6SDmitry Fleytman void net_tx_pkt_uninit(struct NetTxPkt *pkt) 80605d52e6SDmitry Fleytman { 81605d52e6SDmitry Fleytman if (pkt) { 82605d52e6SDmitry Fleytman g_free(pkt->vec); 83605d52e6SDmitry Fleytman g_free(pkt->raw); 84605d52e6SDmitry Fleytman g_free(pkt); 85605d52e6SDmitry Fleytman } 86605d52e6SDmitry Fleytman } 87605d52e6SDmitry Fleytman 88eb700029SDmitry Fleytman void net_tx_pkt_update_ip_hdr_checksum(struct NetTxPkt *pkt) 89eb700029SDmitry Fleytman { 90eb700029SDmitry Fleytman uint16_t csum; 91eb700029SDmitry Fleytman assert(pkt); 92eb700029SDmitry Fleytman struct ip_header *ip_hdr; 93eb700029SDmitry Fleytman ip_hdr = pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_base; 94eb700029SDmitry Fleytman 95eb700029SDmitry Fleytman ip_hdr->ip_len = cpu_to_be16(pkt->payload_len + 96eb700029SDmitry Fleytman pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len); 97eb700029SDmitry Fleytman 98eb700029SDmitry Fleytman ip_hdr->ip_sum = 0; 99eb700029SDmitry Fleytman csum = net_raw_checksum((uint8_t *)ip_hdr, 100eb700029SDmitry Fleytman pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len); 101eb700029SDmitry Fleytman ip_hdr->ip_sum = cpu_to_be16(csum); 102eb700029SDmitry Fleytman } 103eb700029SDmitry Fleytman 104605d52e6SDmitry Fleytman void net_tx_pkt_update_ip_checksums(struct NetTxPkt *pkt) 105605d52e6SDmitry Fleytman { 106605d52e6SDmitry Fleytman uint16_t csum; 107eb700029SDmitry Fleytman uint32_t cntr, cso; 108605d52e6SDmitry Fleytman assert(pkt); 109605d52e6SDmitry Fleytman uint8_t gso_type = pkt->virt_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN; 110eb700029SDmitry Fleytman void *ip_hdr = pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_base; 111605d52e6SDmitry Fleytman 112605d52e6SDmitry Fleytman if (pkt->payload_len + pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len > 113605d52e6SDmitry Fleytman ETH_MAX_IP_DGRAM_LEN) { 114605d52e6SDmitry Fleytman return; 115605d52e6SDmitry Fleytman } 116605d52e6SDmitry Fleytman 117eb700029SDmitry Fleytman if (gso_type == VIRTIO_NET_HDR_GSO_TCPV4 || 118eb700029SDmitry Fleytman gso_type == VIRTIO_NET_HDR_GSO_UDP) { 119605d52e6SDmitry Fleytman /* Calculate IP header checksum */ 120eb700029SDmitry Fleytman net_tx_pkt_update_ip_hdr_checksum(pkt); 121605d52e6SDmitry Fleytman 122605d52e6SDmitry Fleytman /* Calculate IP pseudo header checksum */ 123eb700029SDmitry Fleytman cntr = eth_calc_ip4_pseudo_hdr_csum(ip_hdr, pkt->payload_len, &cso); 124eb700029SDmitry Fleytman csum = cpu_to_be16(~net_checksum_finish(cntr)); 125eb700029SDmitry Fleytman } else if (gso_type == VIRTIO_NET_HDR_GSO_TCPV6) { 126eb700029SDmitry Fleytman /* Calculate IP pseudo header checksum */ 127eb700029SDmitry Fleytman cntr = eth_calc_ip6_pseudo_hdr_csum(ip_hdr, pkt->payload_len, 128eb700029SDmitry Fleytman IP_PROTO_TCP, &cso); 129eb700029SDmitry Fleytman csum = cpu_to_be16(~net_checksum_finish(cntr)); 130eb700029SDmitry Fleytman } else { 131eb700029SDmitry Fleytman return; 132eb700029SDmitry Fleytman } 133eb700029SDmitry Fleytman 134605d52e6SDmitry Fleytman iov_from_buf(&pkt->vec[NET_TX_PKT_PL_START_FRAG], pkt->payload_frags, 135605d52e6SDmitry Fleytman pkt->virt_hdr.csum_offset, &csum, sizeof(csum)); 136605d52e6SDmitry Fleytman } 137605d52e6SDmitry Fleytman 138605d52e6SDmitry Fleytman static void net_tx_pkt_calculate_hdr_len(struct NetTxPkt *pkt) 139605d52e6SDmitry Fleytman { 140605d52e6SDmitry Fleytman pkt->hdr_len = pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_len + 141605d52e6SDmitry Fleytman pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len; 142605d52e6SDmitry Fleytman } 143605d52e6SDmitry Fleytman 144605d52e6SDmitry Fleytman static bool net_tx_pkt_parse_headers(struct NetTxPkt *pkt) 145605d52e6SDmitry Fleytman { 146605d52e6SDmitry Fleytman struct iovec *l2_hdr, *l3_hdr; 147605d52e6SDmitry Fleytman size_t bytes_read; 148605d52e6SDmitry Fleytman size_t full_ip6hdr_len; 149605d52e6SDmitry Fleytman uint16_t l3_proto; 150605d52e6SDmitry Fleytman 151605d52e6SDmitry Fleytman assert(pkt); 152605d52e6SDmitry Fleytman 153605d52e6SDmitry Fleytman l2_hdr = &pkt->vec[NET_TX_PKT_L2HDR_FRAG]; 154605d52e6SDmitry Fleytman l3_hdr = &pkt->vec[NET_TX_PKT_L3HDR_FRAG]; 155605d52e6SDmitry Fleytman 156605d52e6SDmitry Fleytman bytes_read = iov_to_buf(pkt->raw, pkt->raw_frags, 0, l2_hdr->iov_base, 157605d52e6SDmitry Fleytman ETH_MAX_L2_HDR_LEN); 158605d52e6SDmitry Fleytman if (bytes_read < sizeof(struct eth_header)) { 159605d52e6SDmitry Fleytman l2_hdr->iov_len = 0; 160605d52e6SDmitry Fleytman return false; 161605d52e6SDmitry Fleytman } 162605d52e6SDmitry Fleytman 163605d52e6SDmitry Fleytman l2_hdr->iov_len = sizeof(struct eth_header); 164605d52e6SDmitry Fleytman switch (be16_to_cpu(PKT_GET_ETH_HDR(l2_hdr->iov_base)->h_proto)) { 165605d52e6SDmitry Fleytman case ETH_P_VLAN: 166605d52e6SDmitry Fleytman l2_hdr->iov_len += sizeof(struct vlan_header); 167605d52e6SDmitry Fleytman break; 168605d52e6SDmitry Fleytman case ETH_P_DVLAN: 169605d52e6SDmitry Fleytman l2_hdr->iov_len += 2 * sizeof(struct vlan_header); 170605d52e6SDmitry Fleytman break; 171605d52e6SDmitry Fleytman } 172605d52e6SDmitry Fleytman 173605d52e6SDmitry Fleytman if (bytes_read < l2_hdr->iov_len) { 174605d52e6SDmitry Fleytman l2_hdr->iov_len = 0; 175eb700029SDmitry Fleytman l3_hdr->iov_len = 0; 176eb700029SDmitry Fleytman pkt->packet_type = ETH_PKT_UCAST; 177605d52e6SDmitry Fleytman return false; 178eb700029SDmitry Fleytman } else { 179eb700029SDmitry Fleytman l2_hdr->iov_len = ETH_MAX_L2_HDR_LEN; 180eb700029SDmitry Fleytman l2_hdr->iov_len = eth_get_l2_hdr_length(l2_hdr->iov_base); 181eb700029SDmitry Fleytman pkt->packet_type = get_eth_packet_type(l2_hdr->iov_base); 182605d52e6SDmitry Fleytman } 183605d52e6SDmitry Fleytman 184eb700029SDmitry Fleytman l3_proto = eth_get_l3_proto(l2_hdr, 1, l2_hdr->iov_len); 185605d52e6SDmitry Fleytman 186605d52e6SDmitry Fleytman switch (l3_proto) { 187605d52e6SDmitry Fleytman case ETH_P_IP: 188605d52e6SDmitry Fleytman bytes_read = iov_to_buf(pkt->raw, pkt->raw_frags, l2_hdr->iov_len, 189605d52e6SDmitry Fleytman l3_hdr->iov_base, sizeof(struct ip_header)); 190605d52e6SDmitry Fleytman 191605d52e6SDmitry Fleytman if (bytes_read < sizeof(struct ip_header)) { 192605d52e6SDmitry Fleytman l3_hdr->iov_len = 0; 193605d52e6SDmitry Fleytman return false; 194605d52e6SDmitry Fleytman } 195605d52e6SDmitry Fleytman 196605d52e6SDmitry Fleytman l3_hdr->iov_len = IP_HDR_GET_LEN(l3_hdr->iov_base); 197eb700029SDmitry Fleytman 198eb700029SDmitry Fleytman if (l3_hdr->iov_len < sizeof(struct ip_header)) { 199eb700029SDmitry Fleytman l3_hdr->iov_len = 0; 200eb700029SDmitry Fleytman return false; 201eb700029SDmitry Fleytman } 202eb700029SDmitry Fleytman 2034f51e1d3SMarc-André Lureau pkt->l4proto = IP_HDR_GET_P(l3_hdr->iov_base); 204605d52e6SDmitry Fleytman 205eb700029SDmitry Fleytman if (IP_HDR_GET_LEN(l3_hdr->iov_base) != sizeof(struct ip_header)) { 206eb700029SDmitry Fleytman /* copy optional IPv4 header data if any*/ 207605d52e6SDmitry Fleytman bytes_read = iov_to_buf(pkt->raw, pkt->raw_frags, 208605d52e6SDmitry Fleytman l2_hdr->iov_len + sizeof(struct ip_header), 209605d52e6SDmitry Fleytman l3_hdr->iov_base + sizeof(struct ip_header), 210605d52e6SDmitry Fleytman l3_hdr->iov_len - sizeof(struct ip_header)); 211605d52e6SDmitry Fleytman if (bytes_read < l3_hdr->iov_len - sizeof(struct ip_header)) { 212605d52e6SDmitry Fleytman l3_hdr->iov_len = 0; 213605d52e6SDmitry Fleytman return false; 214605d52e6SDmitry Fleytman } 215eb700029SDmitry Fleytman } 216eb700029SDmitry Fleytman 217605d52e6SDmitry Fleytman break; 218605d52e6SDmitry Fleytman 219605d52e6SDmitry Fleytman case ETH_P_IPV6: 220eb700029SDmitry Fleytman { 221eb700029SDmitry Fleytman eth_ip6_hdr_info hdrinfo; 222eb700029SDmitry Fleytman 223605d52e6SDmitry Fleytman if (!eth_parse_ipv6_hdr(pkt->raw, pkt->raw_frags, l2_hdr->iov_len, 224eb700029SDmitry Fleytman &hdrinfo)) { 225605d52e6SDmitry Fleytman l3_hdr->iov_len = 0; 226605d52e6SDmitry Fleytman return false; 227605d52e6SDmitry Fleytman } 228605d52e6SDmitry Fleytman 229eb700029SDmitry Fleytman pkt->l4proto = hdrinfo.l4proto; 230eb700029SDmitry Fleytman full_ip6hdr_len = hdrinfo.full_hdr_len; 231eb700029SDmitry Fleytman 232eb700029SDmitry Fleytman if (full_ip6hdr_len > ETH_MAX_IP_DGRAM_LEN) { 233eb700029SDmitry Fleytman l3_hdr->iov_len = 0; 234eb700029SDmitry Fleytman return false; 235eb700029SDmitry Fleytman } 236605d52e6SDmitry Fleytman 237605d52e6SDmitry Fleytman bytes_read = iov_to_buf(pkt->raw, pkt->raw_frags, l2_hdr->iov_len, 238605d52e6SDmitry Fleytman l3_hdr->iov_base, full_ip6hdr_len); 239605d52e6SDmitry Fleytman 240605d52e6SDmitry Fleytman if (bytes_read < full_ip6hdr_len) { 241605d52e6SDmitry Fleytman l3_hdr->iov_len = 0; 242605d52e6SDmitry Fleytman return false; 243605d52e6SDmitry Fleytman } else { 244605d52e6SDmitry Fleytman l3_hdr->iov_len = full_ip6hdr_len; 245605d52e6SDmitry Fleytman } 246605d52e6SDmitry Fleytman break; 247eb700029SDmitry Fleytman } 248605d52e6SDmitry Fleytman default: 249605d52e6SDmitry Fleytman l3_hdr->iov_len = 0; 250605d52e6SDmitry Fleytman break; 251605d52e6SDmitry Fleytman } 252605d52e6SDmitry Fleytman 253605d52e6SDmitry Fleytman net_tx_pkt_calculate_hdr_len(pkt); 254605d52e6SDmitry Fleytman return true; 255605d52e6SDmitry Fleytman } 256605d52e6SDmitry Fleytman 257eb700029SDmitry Fleytman static void net_tx_pkt_rebuild_payload(struct NetTxPkt *pkt) 258605d52e6SDmitry Fleytman { 259eb700029SDmitry Fleytman pkt->payload_len = iov_size(pkt->raw, pkt->raw_frags) - pkt->hdr_len; 260605d52e6SDmitry Fleytman pkt->payload_frags = iov_copy(&pkt->vec[NET_TX_PKT_PL_START_FRAG], 261605d52e6SDmitry Fleytman pkt->max_payload_frags, 262605d52e6SDmitry Fleytman pkt->raw, pkt->raw_frags, 263eb700029SDmitry Fleytman pkt->hdr_len, pkt->payload_len); 264605d52e6SDmitry Fleytman } 265605d52e6SDmitry Fleytman 266605d52e6SDmitry Fleytman bool net_tx_pkt_parse(struct NetTxPkt *pkt) 267605d52e6SDmitry Fleytman { 268eb700029SDmitry Fleytman if (net_tx_pkt_parse_headers(pkt)) { 269605d52e6SDmitry Fleytman net_tx_pkt_rebuild_payload(pkt); 270eb700029SDmitry Fleytman return true; 271eb700029SDmitry Fleytman } else { 272eb700029SDmitry Fleytman return false; 273eb700029SDmitry Fleytman } 274605d52e6SDmitry Fleytman } 275605d52e6SDmitry Fleytman 276605d52e6SDmitry Fleytman struct virtio_net_hdr *net_tx_pkt_get_vhdr(struct NetTxPkt *pkt) 277605d52e6SDmitry Fleytman { 278605d52e6SDmitry Fleytman assert(pkt); 279605d52e6SDmitry Fleytman return &pkt->virt_hdr; 280605d52e6SDmitry Fleytman } 281605d52e6SDmitry Fleytman 282605d52e6SDmitry Fleytman static uint8_t net_tx_pkt_get_gso_type(struct NetTxPkt *pkt, 283605d52e6SDmitry Fleytman bool tso_enable) 284605d52e6SDmitry Fleytman { 285605d52e6SDmitry Fleytman uint8_t rc = VIRTIO_NET_HDR_GSO_NONE; 286605d52e6SDmitry Fleytman uint16_t l3_proto; 287605d52e6SDmitry Fleytman 288eb700029SDmitry Fleytman l3_proto = eth_get_l3_proto(&pkt->vec[NET_TX_PKT_L2HDR_FRAG], 1, 289605d52e6SDmitry Fleytman pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_len); 290605d52e6SDmitry Fleytman 291605d52e6SDmitry Fleytman if (!tso_enable) { 292605d52e6SDmitry Fleytman goto func_exit; 293605d52e6SDmitry Fleytman } 294605d52e6SDmitry Fleytman 295605d52e6SDmitry Fleytman rc = eth_get_gso_type(l3_proto, pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_base, 296605d52e6SDmitry Fleytman pkt->l4proto); 297605d52e6SDmitry Fleytman 298605d52e6SDmitry Fleytman func_exit: 299605d52e6SDmitry Fleytman return rc; 300605d52e6SDmitry Fleytman } 301605d52e6SDmitry Fleytman 302f9a9eb16SAkihiko Odaki bool net_tx_pkt_build_vheader(struct NetTxPkt *pkt, bool tso_enable, 303605d52e6SDmitry Fleytman bool csum_enable, uint32_t gso_size) 304605d52e6SDmitry Fleytman { 305605d52e6SDmitry Fleytman struct tcp_hdr l4hdr; 306f9a9eb16SAkihiko Odaki size_t bytes_read; 307605d52e6SDmitry Fleytman assert(pkt); 308605d52e6SDmitry Fleytman 309605d52e6SDmitry Fleytman /* csum has to be enabled if tso is. */ 310605d52e6SDmitry Fleytman assert(csum_enable || !tso_enable); 311605d52e6SDmitry Fleytman 312605d52e6SDmitry Fleytman pkt->virt_hdr.gso_type = net_tx_pkt_get_gso_type(pkt, tso_enable); 313605d52e6SDmitry Fleytman 314605d52e6SDmitry Fleytman switch (pkt->virt_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { 315605d52e6SDmitry Fleytman case VIRTIO_NET_HDR_GSO_NONE: 316605d52e6SDmitry Fleytman pkt->virt_hdr.hdr_len = 0; 317605d52e6SDmitry Fleytman pkt->virt_hdr.gso_size = 0; 318605d52e6SDmitry Fleytman break; 319605d52e6SDmitry Fleytman 320605d52e6SDmitry Fleytman case VIRTIO_NET_HDR_GSO_UDP: 321eb700029SDmitry Fleytman pkt->virt_hdr.gso_size = gso_size; 322605d52e6SDmitry Fleytman pkt->virt_hdr.hdr_len = pkt->hdr_len + sizeof(struct udp_header); 323605d52e6SDmitry Fleytman break; 324605d52e6SDmitry Fleytman 325605d52e6SDmitry Fleytman case VIRTIO_NET_HDR_GSO_TCPV4: 326605d52e6SDmitry Fleytman case VIRTIO_NET_HDR_GSO_TCPV6: 327f9a9eb16SAkihiko Odaki bytes_read = iov_to_buf(&pkt->vec[NET_TX_PKT_PL_START_FRAG], 328f9a9eb16SAkihiko Odaki pkt->payload_frags, 0, &l4hdr, sizeof(l4hdr)); 329f9a9eb16SAkihiko Odaki if (bytes_read < sizeof(l4hdr)) { 330f9a9eb16SAkihiko Odaki return false; 331f9a9eb16SAkihiko Odaki } 332f9a9eb16SAkihiko Odaki 333605d52e6SDmitry Fleytman pkt->virt_hdr.hdr_len = pkt->hdr_len + l4hdr.th_off * sizeof(uint32_t); 334eb700029SDmitry Fleytman pkt->virt_hdr.gso_size = gso_size; 335605d52e6SDmitry Fleytman break; 336605d52e6SDmitry Fleytman 337605d52e6SDmitry Fleytman default: 338605d52e6SDmitry Fleytman g_assert_not_reached(); 339605d52e6SDmitry Fleytman } 340605d52e6SDmitry Fleytman 341605d52e6SDmitry Fleytman if (csum_enable) { 342605d52e6SDmitry Fleytman switch (pkt->l4proto) { 343605d52e6SDmitry Fleytman case IP_PROTO_TCP: 344605d52e6SDmitry Fleytman pkt->virt_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; 345605d52e6SDmitry Fleytman pkt->virt_hdr.csum_start = pkt->hdr_len; 346605d52e6SDmitry Fleytman pkt->virt_hdr.csum_offset = offsetof(struct tcp_hdr, th_sum); 347605d52e6SDmitry Fleytman break; 348605d52e6SDmitry Fleytman case IP_PROTO_UDP: 349605d52e6SDmitry Fleytman pkt->virt_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; 350605d52e6SDmitry Fleytman pkt->virt_hdr.csum_start = pkt->hdr_len; 351605d52e6SDmitry Fleytman pkt->virt_hdr.csum_offset = offsetof(struct udp_hdr, uh_sum); 352605d52e6SDmitry Fleytman break; 353605d52e6SDmitry Fleytman default: 354605d52e6SDmitry Fleytman break; 355605d52e6SDmitry Fleytman } 356605d52e6SDmitry Fleytman } 357f9a9eb16SAkihiko Odaki 358f9a9eb16SAkihiko Odaki return true; 359605d52e6SDmitry Fleytman } 360605d52e6SDmitry Fleytman 361eb700029SDmitry Fleytman void net_tx_pkt_setup_vlan_header_ex(struct NetTxPkt *pkt, 362eb700029SDmitry Fleytman uint16_t vlan, uint16_t vlan_ethtype) 363605d52e6SDmitry Fleytman { 364605d52e6SDmitry Fleytman bool is_new; 365605d52e6SDmitry Fleytman assert(pkt); 366605d52e6SDmitry Fleytman 367eb700029SDmitry Fleytman eth_setup_vlan_headers_ex(pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_base, 368eb700029SDmitry Fleytman vlan, vlan_ethtype, &is_new); 369605d52e6SDmitry Fleytman 370605d52e6SDmitry Fleytman /* update l2hdrlen */ 371605d52e6SDmitry Fleytman if (is_new) { 372605d52e6SDmitry Fleytman pkt->hdr_len += sizeof(struct vlan_header); 373605d52e6SDmitry Fleytman pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_len += 374605d52e6SDmitry Fleytman sizeof(struct vlan_header); 375605d52e6SDmitry Fleytman } 376605d52e6SDmitry Fleytman } 377605d52e6SDmitry Fleytman 378605d52e6SDmitry Fleytman bool net_tx_pkt_add_raw_fragment(struct NetTxPkt *pkt, hwaddr pa, 379605d52e6SDmitry Fleytman size_t len) 380605d52e6SDmitry Fleytman { 381605d52e6SDmitry Fleytman hwaddr mapped_len = 0; 382605d52e6SDmitry Fleytman struct iovec *ventry; 383605d52e6SDmitry Fleytman assert(pkt); 384035e69b0SMauro Matteo Cascella 385035e69b0SMauro Matteo Cascella if (pkt->raw_frags >= pkt->max_raw_frags) { 386035e69b0SMauro Matteo Cascella return false; 387035e69b0SMauro Matteo Cascella } 388605d52e6SDmitry Fleytman 389605d52e6SDmitry Fleytman if (!len) { 390605d52e6SDmitry Fleytman return true; 391605d52e6SDmitry Fleytman } 392605d52e6SDmitry Fleytman 393605d52e6SDmitry Fleytman ventry = &pkt->raw[pkt->raw_frags]; 394605d52e6SDmitry Fleytman mapped_len = len; 395605d52e6SDmitry Fleytman 39611171010SDmitry Fleytman ventry->iov_base = pci_dma_map(pkt->pci_dev, pa, 39711171010SDmitry Fleytman &mapped_len, DMA_DIRECTION_TO_DEVICE); 398605d52e6SDmitry Fleytman 399eb700029SDmitry Fleytman if ((ventry->iov_base != NULL) && (len == mapped_len)) { 400eb700029SDmitry Fleytman ventry->iov_len = mapped_len; 401eb700029SDmitry Fleytman pkt->raw_frags++; 402eb700029SDmitry Fleytman return true; 403eb700029SDmitry Fleytman } else { 404605d52e6SDmitry Fleytman return false; 405605d52e6SDmitry Fleytman } 406eb700029SDmitry Fleytman } 407605d52e6SDmitry Fleytman 408eb700029SDmitry Fleytman bool net_tx_pkt_has_fragments(struct NetTxPkt *pkt) 409eb700029SDmitry Fleytman { 410eb700029SDmitry Fleytman return pkt->raw_frags > 0; 411605d52e6SDmitry Fleytman } 412605d52e6SDmitry Fleytman 413605d52e6SDmitry Fleytman eth_pkt_types_e net_tx_pkt_get_packet_type(struct NetTxPkt *pkt) 414605d52e6SDmitry Fleytman { 415605d52e6SDmitry Fleytman assert(pkt); 416605d52e6SDmitry Fleytman 417605d52e6SDmitry Fleytman return pkt->packet_type; 418605d52e6SDmitry Fleytman } 419605d52e6SDmitry Fleytman 420605d52e6SDmitry Fleytman size_t net_tx_pkt_get_total_len(struct NetTxPkt *pkt) 421605d52e6SDmitry Fleytman { 422605d52e6SDmitry Fleytman assert(pkt); 423605d52e6SDmitry Fleytman 424605d52e6SDmitry Fleytman return pkt->hdr_len + pkt->payload_len; 425605d52e6SDmitry Fleytman } 426605d52e6SDmitry Fleytman 427605d52e6SDmitry Fleytman void net_tx_pkt_dump(struct NetTxPkt *pkt) 428605d52e6SDmitry Fleytman { 429605d52e6SDmitry Fleytman #ifdef NET_TX_PKT_DEBUG 430605d52e6SDmitry Fleytman assert(pkt); 431605d52e6SDmitry Fleytman 432605d52e6SDmitry Fleytman printf("TX PKT: hdr_len: %d, pkt_type: 0x%X, l2hdr_len: %lu, " 433605d52e6SDmitry Fleytman "l3hdr_len: %lu, payload_len: %u\n", pkt->hdr_len, pkt->packet_type, 434605d52e6SDmitry Fleytman pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_len, 435605d52e6SDmitry Fleytman pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len, pkt->payload_len); 436605d52e6SDmitry Fleytman #endif 437605d52e6SDmitry Fleytman } 438605d52e6SDmitry Fleytman 439605d52e6SDmitry Fleytman void net_tx_pkt_reset(struct NetTxPkt *pkt) 440605d52e6SDmitry Fleytman { 441605d52e6SDmitry Fleytman int i; 442605d52e6SDmitry Fleytman 443605d52e6SDmitry Fleytman /* no assert, as reset can be called before tx_pkt_init */ 444605d52e6SDmitry Fleytman if (!pkt) { 445605d52e6SDmitry Fleytman return; 446605d52e6SDmitry Fleytman } 447605d52e6SDmitry Fleytman 448605d52e6SDmitry Fleytman memset(&pkt->virt_hdr, 0, sizeof(pkt->virt_hdr)); 449605d52e6SDmitry Fleytman 450605d52e6SDmitry Fleytman assert(pkt->vec); 451eb700029SDmitry Fleytman 452605d52e6SDmitry Fleytman pkt->payload_len = 0; 453605d52e6SDmitry Fleytman pkt->payload_frags = 0; 454605d52e6SDmitry Fleytman 455283f0a05SThomas Huth if (pkt->max_raw_frags > 0) { 456605d52e6SDmitry Fleytman assert(pkt->raw); 457605d52e6SDmitry Fleytman for (i = 0; i < pkt->raw_frags; i++) { 458605d52e6SDmitry Fleytman assert(pkt->raw[i].iov_base); 459283f0a05SThomas Huth pci_dma_unmap(pkt->pci_dev, pkt->raw[i].iov_base, 460283f0a05SThomas Huth pkt->raw[i].iov_len, DMA_DIRECTION_TO_DEVICE, 0); 461283f0a05SThomas Huth } 462605d52e6SDmitry Fleytman } 463605d52e6SDmitry Fleytman pkt->raw_frags = 0; 464605d52e6SDmitry Fleytman 465605d52e6SDmitry Fleytman pkt->hdr_len = 0; 466605d52e6SDmitry Fleytman pkt->l4proto = 0; 467605d52e6SDmitry Fleytman } 468605d52e6SDmitry Fleytman 469605d52e6SDmitry Fleytman static void net_tx_pkt_do_sw_csum(struct NetTxPkt *pkt) 470605d52e6SDmitry Fleytman { 471605d52e6SDmitry Fleytman struct iovec *iov = &pkt->vec[NET_TX_PKT_L2HDR_FRAG]; 472605d52e6SDmitry Fleytman uint32_t csum_cntr; 473605d52e6SDmitry Fleytman uint16_t csum = 0; 474eb700029SDmitry Fleytman uint32_t cso; 475605d52e6SDmitry Fleytman /* num of iovec without vhdr */ 476605d52e6SDmitry Fleytman uint32_t iov_len = pkt->payload_frags + NET_TX_PKT_PL_START_FRAG - 1; 477605d52e6SDmitry Fleytman uint16_t csl; 478605d52e6SDmitry Fleytman size_t csum_offset = pkt->virt_hdr.csum_start + pkt->virt_hdr.csum_offset; 4799a8d9492SAndrew uint16_t l3_proto = eth_get_l3_proto(iov, 1, iov->iov_len); 480605d52e6SDmitry Fleytman 481605d52e6SDmitry Fleytman /* Put zero to checksum field */ 482605d52e6SDmitry Fleytman iov_from_buf(iov, iov_len, csum_offset, &csum, sizeof csum); 483605d52e6SDmitry Fleytman 484605d52e6SDmitry Fleytman /* Calculate L4 TCP/UDP checksum */ 485605d52e6SDmitry Fleytman csl = pkt->payload_len; 486605d52e6SDmitry Fleytman 4879a8d9492SAndrew csum_cntr = 0; 4889a8d9492SAndrew cso = 0; 489605d52e6SDmitry Fleytman /* add pseudo header to csum */ 4909a8d9492SAndrew if (l3_proto == ETH_P_IP) { 4919a8d9492SAndrew csum_cntr = eth_calc_ip4_pseudo_hdr_csum( 4929a8d9492SAndrew pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_base, 4939a8d9492SAndrew csl, &cso); 4949a8d9492SAndrew } else if (l3_proto == ETH_P_IPV6) { 4959a8d9492SAndrew csum_cntr = eth_calc_ip6_pseudo_hdr_csum( 4969a8d9492SAndrew pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_base, 4979a8d9492SAndrew csl, pkt->l4proto, &cso); 4989a8d9492SAndrew } 499eb700029SDmitry Fleytman 500eb700029SDmitry Fleytman /* data checksum */ 501eb700029SDmitry Fleytman csum_cntr += 502eb700029SDmitry Fleytman net_checksum_add_iov(iov, iov_len, pkt->virt_hdr.csum_start, csl, cso); 503605d52e6SDmitry Fleytman 504605d52e6SDmitry Fleytman /* Put the checksum obtained into the packet */ 5050dacea92SEd Swierk csum = cpu_to_be16(net_checksum_finish_nozero(csum_cntr)); 506605d52e6SDmitry Fleytman iov_from_buf(iov, iov_len, csum_offset, &csum, sizeof csum); 507605d52e6SDmitry Fleytman } 508605d52e6SDmitry Fleytman 509605d52e6SDmitry Fleytman #define NET_MAX_FRAG_SG_LIST (64) 510605d52e6SDmitry Fleytman 511605d52e6SDmitry Fleytman static size_t net_tx_pkt_fetch_fragment(struct NetTxPkt *pkt, 512605d52e6SDmitry Fleytman int *src_idx, size_t *src_offset, struct iovec *dst, int *dst_idx) 513605d52e6SDmitry Fleytman { 514605d52e6SDmitry Fleytman size_t fetched = 0; 515605d52e6SDmitry Fleytman struct iovec *src = pkt->vec; 516605d52e6SDmitry Fleytman 517*ffbd2dbdSAkihiko Odaki *dst_idx = NET_TX_PKT_PL_START_FRAG; 518605d52e6SDmitry Fleytman 519eb700029SDmitry Fleytman while (fetched < IP_FRAG_ALIGN_SIZE(pkt->virt_hdr.gso_size)) { 520605d52e6SDmitry Fleytman 521605d52e6SDmitry Fleytman /* no more place in fragment iov */ 522605d52e6SDmitry Fleytman if (*dst_idx == NET_MAX_FRAG_SG_LIST) { 523605d52e6SDmitry Fleytman break; 524605d52e6SDmitry Fleytman } 525605d52e6SDmitry Fleytman 526605d52e6SDmitry Fleytman /* no more data in iovec */ 527605d52e6SDmitry Fleytman if (*src_idx == (pkt->payload_frags + NET_TX_PKT_PL_START_FRAG)) { 528605d52e6SDmitry Fleytman break; 529605d52e6SDmitry Fleytman } 530605d52e6SDmitry Fleytman 531605d52e6SDmitry Fleytman 532605d52e6SDmitry Fleytman dst[*dst_idx].iov_base = src[*src_idx].iov_base + *src_offset; 533605d52e6SDmitry Fleytman dst[*dst_idx].iov_len = MIN(src[*src_idx].iov_len - *src_offset, 534eb700029SDmitry Fleytman IP_FRAG_ALIGN_SIZE(pkt->virt_hdr.gso_size) - fetched); 535605d52e6SDmitry Fleytman 536605d52e6SDmitry Fleytman *src_offset += dst[*dst_idx].iov_len; 537605d52e6SDmitry Fleytman fetched += dst[*dst_idx].iov_len; 538605d52e6SDmitry Fleytman 539605d52e6SDmitry Fleytman if (*src_offset == src[*src_idx].iov_len) { 540605d52e6SDmitry Fleytman *src_offset = 0; 541605d52e6SDmitry Fleytman (*src_idx)++; 542605d52e6SDmitry Fleytman } 543605d52e6SDmitry Fleytman 544605d52e6SDmitry Fleytman (*dst_idx)++; 545605d52e6SDmitry Fleytman } 546605d52e6SDmitry Fleytman 547605d52e6SDmitry Fleytman return fetched; 548605d52e6SDmitry Fleytman } 549605d52e6SDmitry Fleytman 550*ffbd2dbdSAkihiko Odaki static void net_tx_pkt_sendv( 551*ffbd2dbdSAkihiko Odaki void *opaque, const struct iovec *iov, int iov_cnt, 552*ffbd2dbdSAkihiko Odaki const struct iovec *virt_iov, int virt_iov_cnt) 553eb700029SDmitry Fleytman { 554*ffbd2dbdSAkihiko Odaki NetClientState *nc = opaque; 555*ffbd2dbdSAkihiko Odaki 556*ffbd2dbdSAkihiko Odaki if (qemu_get_using_vnet_hdr(nc->peer)) { 557*ffbd2dbdSAkihiko Odaki qemu_sendv_packet(nc, virt_iov, virt_iov_cnt); 558eb700029SDmitry Fleytman } else { 559eb700029SDmitry Fleytman qemu_sendv_packet(nc, iov, iov_cnt); 560eb700029SDmitry Fleytman } 561eb700029SDmitry Fleytman } 562eb700029SDmitry Fleytman 563605d52e6SDmitry Fleytman static bool net_tx_pkt_do_sw_fragmentation(struct NetTxPkt *pkt, 564*ffbd2dbdSAkihiko Odaki NetTxPktCallback callback, 565*ffbd2dbdSAkihiko Odaki void *context) 566605d52e6SDmitry Fleytman { 567605d52e6SDmitry Fleytman struct iovec fragment[NET_MAX_FRAG_SG_LIST]; 568605d52e6SDmitry Fleytman size_t fragment_len = 0; 569605d52e6SDmitry Fleytman bool more_frags = false; 570605d52e6SDmitry Fleytman 571605d52e6SDmitry Fleytman /* some pointers for shorter code */ 572605d52e6SDmitry Fleytman void *l2_iov_base, *l3_iov_base; 573605d52e6SDmitry Fleytman size_t l2_iov_len, l3_iov_len; 574605d52e6SDmitry Fleytman int src_idx = NET_TX_PKT_PL_START_FRAG, dst_idx; 575605d52e6SDmitry Fleytman size_t src_offset = 0; 576605d52e6SDmitry Fleytman size_t fragment_offset = 0; 577*ffbd2dbdSAkihiko Odaki struct virtio_net_hdr virt_hdr = { 578*ffbd2dbdSAkihiko Odaki .flags = pkt->virt_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM ? 579*ffbd2dbdSAkihiko Odaki VIRTIO_NET_HDR_F_DATA_VALID : 0 580*ffbd2dbdSAkihiko Odaki }; 581605d52e6SDmitry Fleytman 582605d52e6SDmitry Fleytman l2_iov_base = pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_base; 583605d52e6SDmitry Fleytman l2_iov_len = pkt->vec[NET_TX_PKT_L2HDR_FRAG].iov_len; 584605d52e6SDmitry Fleytman l3_iov_base = pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_base; 585605d52e6SDmitry Fleytman l3_iov_len = pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len; 586605d52e6SDmitry Fleytman 587605d52e6SDmitry Fleytman /* Copy headers */ 588*ffbd2dbdSAkihiko Odaki fragment[NET_TX_PKT_VHDR_FRAG].iov_base = &virt_hdr; 589*ffbd2dbdSAkihiko Odaki fragment[NET_TX_PKT_VHDR_FRAG].iov_len = sizeof(virt_hdr); 590*ffbd2dbdSAkihiko Odaki fragment[NET_TX_PKT_L2HDR_FRAG].iov_base = l2_iov_base; 591*ffbd2dbdSAkihiko Odaki fragment[NET_TX_PKT_L2HDR_FRAG].iov_len = l2_iov_len; 592*ffbd2dbdSAkihiko Odaki fragment[NET_TX_PKT_L3HDR_FRAG].iov_base = l3_iov_base; 593*ffbd2dbdSAkihiko Odaki fragment[NET_TX_PKT_L3HDR_FRAG].iov_len = l3_iov_len; 594605d52e6SDmitry Fleytman 595605d52e6SDmitry Fleytman 596605d52e6SDmitry Fleytman /* Put as much data as possible and send */ 597605d52e6SDmitry Fleytman do { 598605d52e6SDmitry Fleytman fragment_len = net_tx_pkt_fetch_fragment(pkt, &src_idx, &src_offset, 599605d52e6SDmitry Fleytman fragment, &dst_idx); 600605d52e6SDmitry Fleytman 601605d52e6SDmitry Fleytman more_frags = (fragment_offset + fragment_len < pkt->payload_len); 602605d52e6SDmitry Fleytman 603605d52e6SDmitry Fleytman eth_setup_ip4_fragmentation(l2_iov_base, l2_iov_len, l3_iov_base, 604605d52e6SDmitry Fleytman l3_iov_len, fragment_len, fragment_offset, more_frags); 605605d52e6SDmitry Fleytman 606605d52e6SDmitry Fleytman eth_fix_ip4_checksum(l3_iov_base, l3_iov_len); 607605d52e6SDmitry Fleytman 608*ffbd2dbdSAkihiko Odaki callback(context, 609*ffbd2dbdSAkihiko Odaki fragment + NET_TX_PKT_L2HDR_FRAG, dst_idx - NET_TX_PKT_L2HDR_FRAG, 610*ffbd2dbdSAkihiko Odaki fragment + NET_TX_PKT_VHDR_FRAG, dst_idx - NET_TX_PKT_VHDR_FRAG); 611605d52e6SDmitry Fleytman 612605d52e6SDmitry Fleytman fragment_offset += fragment_len; 613605d52e6SDmitry Fleytman 614ead315e4SPrasad J Pandit } while (fragment_len && more_frags); 615605d52e6SDmitry Fleytman 616605d52e6SDmitry Fleytman return true; 617605d52e6SDmitry Fleytman } 618605d52e6SDmitry Fleytman 619605d52e6SDmitry Fleytman bool net_tx_pkt_send(struct NetTxPkt *pkt, NetClientState *nc) 620605d52e6SDmitry Fleytman { 621*ffbd2dbdSAkihiko Odaki bool offload = qemu_get_using_vnet_hdr(nc->peer); 622*ffbd2dbdSAkihiko Odaki return net_tx_pkt_send_custom(pkt, offload, net_tx_pkt_sendv, nc); 623*ffbd2dbdSAkihiko Odaki } 62455daf493SAkihiko Odaki 625*ffbd2dbdSAkihiko Odaki bool net_tx_pkt_send_custom(struct NetTxPkt *pkt, bool offload, 626*ffbd2dbdSAkihiko Odaki NetTxPktCallback callback, void *context) 627*ffbd2dbdSAkihiko Odaki { 628605d52e6SDmitry Fleytman assert(pkt); 629605d52e6SDmitry Fleytman 630*ffbd2dbdSAkihiko Odaki if (!offload && pkt->virt_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { 631605d52e6SDmitry Fleytman net_tx_pkt_do_sw_csum(pkt); 632605d52e6SDmitry Fleytman } 633605d52e6SDmitry Fleytman 634605d52e6SDmitry Fleytman /* 635605d52e6SDmitry Fleytman * Since underlying infrastructure does not support IP datagrams longer 636605d52e6SDmitry Fleytman * than 64K we should drop such packets and don't even try to send 637605d52e6SDmitry Fleytman */ 638605d52e6SDmitry Fleytman if (VIRTIO_NET_HDR_GSO_NONE != pkt->virt_hdr.gso_type) { 639605d52e6SDmitry Fleytman if (pkt->payload_len > 640605d52e6SDmitry Fleytman ETH_MAX_IP_DGRAM_LEN - 641605d52e6SDmitry Fleytman pkt->vec[NET_TX_PKT_L3HDR_FRAG].iov_len) { 642605d52e6SDmitry Fleytman return false; 643605d52e6SDmitry Fleytman } 644605d52e6SDmitry Fleytman } 645605d52e6SDmitry Fleytman 646*ffbd2dbdSAkihiko Odaki if (offload || pkt->virt_hdr.gso_type == VIRTIO_NET_HDR_GSO_NONE) { 647e219d309SAndrew net_tx_pkt_fix_ip6_payload_len(pkt); 648*ffbd2dbdSAkihiko Odaki callback(context, pkt->vec + NET_TX_PKT_L2HDR_FRAG, 649*ffbd2dbdSAkihiko Odaki pkt->payload_frags + NET_TX_PKT_PL_START_FRAG - NET_TX_PKT_L2HDR_FRAG, 650*ffbd2dbdSAkihiko Odaki pkt->vec + NET_TX_PKT_VHDR_FRAG, 651*ffbd2dbdSAkihiko Odaki pkt->payload_frags + NET_TX_PKT_PL_START_FRAG - NET_TX_PKT_VHDR_FRAG); 652605d52e6SDmitry Fleytman return true; 653605d52e6SDmitry Fleytman } 654605d52e6SDmitry Fleytman 655*ffbd2dbdSAkihiko Odaki return net_tx_pkt_do_sw_fragmentation(pkt, callback, context); 656eb700029SDmitry Fleytman } 657e219d309SAndrew 658e219d309SAndrew void net_tx_pkt_fix_ip6_payload_len(struct NetTxPkt *pkt) 659e219d309SAndrew { 660e219d309SAndrew struct iovec *l2 = &pkt->vec[NET_TX_PKT_L2HDR_FRAG]; 661e219d309SAndrew if (eth_get_l3_proto(l2, 1, l2->iov_len) == ETH_P_IPV6) { 662e219d309SAndrew struct ip6_header *ip6 = (struct ip6_header *) pkt->l3_hdr; 663e219d309SAndrew /* 664e219d309SAndrew * TODO: if qemu would support >64K packets - add jumbo option check 665e219d309SAndrew * something like that: 666e219d309SAndrew * 'if (ip6->ip6_plen == 0 && !has_jumbo_option(ip6)) {' 667e219d309SAndrew */ 668e219d309SAndrew if (ip6->ip6_plen == 0) { 669e219d309SAndrew if (pkt->payload_len <= ETH_MAX_IP_DGRAM_LEN) { 670e219d309SAndrew ip6->ip6_plen = htons(pkt->payload_len); 671e219d309SAndrew } 672e219d309SAndrew /* 673e219d309SAndrew * TODO: if qemu would support >64K packets 674e219d309SAndrew * add jumbo option for packets greater then 65,535 bytes 675e219d309SAndrew */ 676e219d309SAndrew } 677e219d309SAndrew } 678e219d309SAndrew } 679