target-arm: implement arm_debug_target_el()
[qemu/ar7.git] / hw / net / vmxnet_tx_pkt.c
blobeb88ddf2549b99e6e0f9f9fec4b68b23e3621750
1 /*
2 * QEMU VMWARE VMXNET* paravirtual NICs - TX packets abstractions
4 * Copyright (c) 2012 Ravello Systems LTD (http://ravellosystems.com)
6 * Developed by Daynix Computing LTD (http://www.daynix.com)
8 * Authors:
9 * Dmitry Fleytman <dmitry@daynix.com>
10 * Tamir Shomer <tamirs@daynix.com>
11 * Yan Vugenfirer <yan@daynix.com>
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.
18 #include "hw/hw.h"
19 #include "vmxnet_tx_pkt.h"
20 #include "net/eth.h"
21 #include "qemu-common.h"
22 #include "qemu/iov.h"
23 #include "net/checksum.h"
24 #include "net/tap.h"
25 #include "net/net.h"
27 enum {
28 VMXNET_TX_PKT_VHDR_FRAG = 0,
29 VMXNET_TX_PKT_L2HDR_FRAG,
30 VMXNET_TX_PKT_L3HDR_FRAG,
31 VMXNET_TX_PKT_PL_START_FRAG
34 /* TX packet private context */
35 struct VmxnetTxPkt {
36 struct virtio_net_hdr virt_hdr;
37 bool has_virt_hdr;
39 struct iovec *raw;
40 uint32_t raw_frags;
41 uint32_t max_raw_frags;
43 struct iovec *vec;
45 uint8_t l2_hdr[ETH_MAX_L2_HDR_LEN];
47 uint32_t payload_len;
49 uint32_t payload_frags;
50 uint32_t max_payload_frags;
52 uint16_t hdr_len;
53 eth_pkt_types_e packet_type;
54 uint8_t l4proto;
57 void vmxnet_tx_pkt_init(struct VmxnetTxPkt **pkt, uint32_t max_frags,
58 bool has_virt_hdr)
60 struct VmxnetTxPkt *p = g_malloc0(sizeof *p);
62 p->vec = g_malloc((sizeof *p->vec) *
63 (max_frags + VMXNET_TX_PKT_PL_START_FRAG));
65 p->raw = g_malloc((sizeof *p->raw) * max_frags);
67 p->max_payload_frags = max_frags;
68 p->max_raw_frags = max_frags;
69 p->has_virt_hdr = has_virt_hdr;
70 p->vec[VMXNET_TX_PKT_VHDR_FRAG].iov_base = &p->virt_hdr;
71 p->vec[VMXNET_TX_PKT_VHDR_FRAG].iov_len =
72 p->has_virt_hdr ? sizeof p->virt_hdr : 0;
73 p->vec[VMXNET_TX_PKT_L2HDR_FRAG].iov_base = &p->l2_hdr;
74 p->vec[VMXNET_TX_PKT_L3HDR_FRAG].iov_base = NULL;
75 p->vec[VMXNET_TX_PKT_L3HDR_FRAG].iov_len = 0;
77 *pkt = p;
80 void vmxnet_tx_pkt_uninit(struct VmxnetTxPkt *pkt)
82 if (pkt) {
83 g_free(pkt->vec);
84 g_free(pkt->raw);
85 g_free(pkt);
89 void vmxnet_tx_pkt_update_ip_checksums(struct VmxnetTxPkt *pkt)
91 uint16_t csum;
92 uint32_t ph_raw_csum;
93 assert(pkt);
94 uint8_t gso_type = pkt->virt_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN;
95 struct ip_header *ip_hdr;
97 if (VIRTIO_NET_HDR_GSO_TCPV4 != gso_type &&
98 VIRTIO_NET_HDR_GSO_UDP != gso_type) {
99 return;
102 ip_hdr = pkt->vec[VMXNET_TX_PKT_L3HDR_FRAG].iov_base;
104 if (pkt->payload_len + pkt->vec[VMXNET_TX_PKT_L3HDR_FRAG].iov_len >
105 ETH_MAX_IP_DGRAM_LEN) {
106 return;
109 ip_hdr->ip_len = cpu_to_be16(pkt->payload_len +
110 pkt->vec[VMXNET_TX_PKT_L3HDR_FRAG].iov_len);
112 /* Calculate IP header checksum */
113 ip_hdr->ip_sum = 0;
114 csum = net_raw_checksum((uint8_t *)ip_hdr,
115 pkt->vec[VMXNET_TX_PKT_L3HDR_FRAG].iov_len);
116 ip_hdr->ip_sum = cpu_to_be16(csum);
118 /* Calculate IP pseudo header checksum */
119 ph_raw_csum = eth_calc_pseudo_hdr_csum(ip_hdr, pkt->payload_len);
120 csum = cpu_to_be16(~net_checksum_finish(ph_raw_csum));
121 iov_from_buf(&pkt->vec[VMXNET_TX_PKT_PL_START_FRAG], pkt->payload_frags,
122 pkt->virt_hdr.csum_offset, &csum, sizeof(csum));
125 static void vmxnet_tx_pkt_calculate_hdr_len(struct VmxnetTxPkt *pkt)
127 pkt->hdr_len = pkt->vec[VMXNET_TX_PKT_L2HDR_FRAG].iov_len +
128 pkt->vec[VMXNET_TX_PKT_L3HDR_FRAG].iov_len;
131 static bool vmxnet_tx_pkt_parse_headers(struct VmxnetTxPkt *pkt)
133 struct iovec *l2_hdr, *l3_hdr;
134 size_t bytes_read;
135 size_t full_ip6hdr_len;
136 uint16_t l3_proto;
138 assert(pkt);
140 l2_hdr = &pkt->vec[VMXNET_TX_PKT_L2HDR_FRAG];
141 l3_hdr = &pkt->vec[VMXNET_TX_PKT_L3HDR_FRAG];
143 bytes_read = iov_to_buf(pkt->raw, pkt->raw_frags, 0, l2_hdr->iov_base,
144 ETH_MAX_L2_HDR_LEN);
145 if (bytes_read < sizeof(struct eth_header)) {
146 l2_hdr->iov_len = 0;
147 return false;
150 l2_hdr->iov_len = sizeof(struct eth_header);
151 switch (be16_to_cpu(PKT_GET_ETH_HDR(l2_hdr->iov_base)->h_proto)) {
152 case ETH_P_VLAN:
153 l2_hdr->iov_len += sizeof(struct vlan_header);
154 break;
155 case ETH_P_DVLAN:
156 l2_hdr->iov_len += 2 * sizeof(struct vlan_header);
157 break;
160 if (bytes_read < l2_hdr->iov_len) {
161 l2_hdr->iov_len = 0;
162 return false;
165 l3_proto = eth_get_l3_proto(l2_hdr->iov_base, l2_hdr->iov_len);
167 switch (l3_proto) {
168 case ETH_P_IP:
169 l3_hdr->iov_base = g_malloc(ETH_MAX_IP4_HDR_LEN);
171 bytes_read = iov_to_buf(pkt->raw, pkt->raw_frags, l2_hdr->iov_len,
172 l3_hdr->iov_base, sizeof(struct ip_header));
174 if (bytes_read < sizeof(struct ip_header)) {
175 l3_hdr->iov_len = 0;
176 return false;
179 l3_hdr->iov_len = IP_HDR_GET_LEN(l3_hdr->iov_base);
180 pkt->l4proto = ((struct ip_header *) l3_hdr->iov_base)->ip_p;
182 /* copy optional IPv4 header data */
183 bytes_read = iov_to_buf(pkt->raw, pkt->raw_frags,
184 l2_hdr->iov_len + sizeof(struct ip_header),
185 l3_hdr->iov_base + sizeof(struct ip_header),
186 l3_hdr->iov_len - sizeof(struct ip_header));
187 if (bytes_read < l3_hdr->iov_len - sizeof(struct ip_header)) {
188 l3_hdr->iov_len = 0;
189 return false;
191 break;
193 case ETH_P_IPV6:
194 if (!eth_parse_ipv6_hdr(pkt->raw, pkt->raw_frags, l2_hdr->iov_len,
195 &pkt->l4proto, &full_ip6hdr_len)) {
196 l3_hdr->iov_len = 0;
197 return false;
200 l3_hdr->iov_base = g_malloc(full_ip6hdr_len);
202 bytes_read = iov_to_buf(pkt->raw, pkt->raw_frags, l2_hdr->iov_len,
203 l3_hdr->iov_base, full_ip6hdr_len);
205 if (bytes_read < full_ip6hdr_len) {
206 l3_hdr->iov_len = 0;
207 return false;
208 } else {
209 l3_hdr->iov_len = full_ip6hdr_len;
211 break;
213 default:
214 l3_hdr->iov_len = 0;
215 break;
218 vmxnet_tx_pkt_calculate_hdr_len(pkt);
219 pkt->packet_type = get_eth_packet_type(l2_hdr->iov_base);
220 return true;
223 static bool vmxnet_tx_pkt_rebuild_payload(struct VmxnetTxPkt *pkt)
225 size_t payload_len = iov_size(pkt->raw, pkt->raw_frags) - pkt->hdr_len;
227 pkt->payload_frags = iov_copy(&pkt->vec[VMXNET_TX_PKT_PL_START_FRAG],
228 pkt->max_payload_frags,
229 pkt->raw, pkt->raw_frags,
230 pkt->hdr_len, payload_len);
232 if (pkt->payload_frags != (uint32_t) -1) {
233 pkt->payload_len = payload_len;
234 return true;
235 } else {
236 return false;
240 bool vmxnet_tx_pkt_parse(struct VmxnetTxPkt *pkt)
242 return vmxnet_tx_pkt_parse_headers(pkt) &&
243 vmxnet_tx_pkt_rebuild_payload(pkt);
246 struct virtio_net_hdr *vmxnet_tx_pkt_get_vhdr(struct VmxnetTxPkt *pkt)
248 assert(pkt);
249 return &pkt->virt_hdr;
252 static uint8_t vmxnet_tx_pkt_get_gso_type(struct VmxnetTxPkt *pkt,
253 bool tso_enable)
255 uint8_t rc = VIRTIO_NET_HDR_GSO_NONE;
256 uint16_t l3_proto;
258 l3_proto = eth_get_l3_proto(pkt->vec[VMXNET_TX_PKT_L2HDR_FRAG].iov_base,
259 pkt->vec[VMXNET_TX_PKT_L2HDR_FRAG].iov_len);
261 if (!tso_enable) {
262 goto func_exit;
265 rc = eth_get_gso_type(l3_proto, pkt->vec[VMXNET_TX_PKT_L3HDR_FRAG].iov_base,
266 pkt->l4proto);
268 func_exit:
269 return rc;
272 void vmxnet_tx_pkt_build_vheader(struct VmxnetTxPkt *pkt, bool tso_enable,
273 bool csum_enable, uint32_t gso_size)
275 struct tcp_hdr l4hdr;
276 assert(pkt);
278 /* csum has to be enabled if tso is. */
279 assert(csum_enable || !tso_enable);
281 pkt->virt_hdr.gso_type = vmxnet_tx_pkt_get_gso_type(pkt, tso_enable);
283 switch (pkt->virt_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
284 case VIRTIO_NET_HDR_GSO_NONE:
285 pkt->virt_hdr.hdr_len = 0;
286 pkt->virt_hdr.gso_size = 0;
287 break;
289 case VIRTIO_NET_HDR_GSO_UDP:
290 pkt->virt_hdr.gso_size = IP_FRAG_ALIGN_SIZE(gso_size);
291 pkt->virt_hdr.hdr_len = pkt->hdr_len + sizeof(struct udp_header);
292 break;
294 case VIRTIO_NET_HDR_GSO_TCPV4:
295 case VIRTIO_NET_HDR_GSO_TCPV6:
296 iov_to_buf(&pkt->vec[VMXNET_TX_PKT_PL_START_FRAG], pkt->payload_frags,
297 0, &l4hdr, sizeof(l4hdr));
298 pkt->virt_hdr.hdr_len = pkt->hdr_len + l4hdr.th_off * sizeof(uint32_t);
299 pkt->virt_hdr.gso_size = IP_FRAG_ALIGN_SIZE(gso_size);
300 break;
302 default:
303 g_assert_not_reached();
306 if (csum_enable) {
307 switch (pkt->l4proto) {
308 case IP_PROTO_TCP:
309 pkt->virt_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
310 pkt->virt_hdr.csum_start = pkt->hdr_len;
311 pkt->virt_hdr.csum_offset = offsetof(struct tcp_hdr, th_sum);
312 break;
313 case IP_PROTO_UDP:
314 pkt->virt_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
315 pkt->virt_hdr.csum_start = pkt->hdr_len;
316 pkt->virt_hdr.csum_offset = offsetof(struct udp_hdr, uh_sum);
317 break;
318 default:
319 break;
324 void vmxnet_tx_pkt_setup_vlan_header(struct VmxnetTxPkt *pkt, uint16_t vlan)
326 bool is_new;
327 assert(pkt);
329 eth_setup_vlan_headers(pkt->vec[VMXNET_TX_PKT_L2HDR_FRAG].iov_base,
330 vlan, &is_new);
332 /* update l2hdrlen */
333 if (is_new) {
334 pkt->hdr_len += sizeof(struct vlan_header);
335 pkt->vec[VMXNET_TX_PKT_L2HDR_FRAG].iov_len +=
336 sizeof(struct vlan_header);
340 bool vmxnet_tx_pkt_add_raw_fragment(struct VmxnetTxPkt *pkt, hwaddr pa,
341 size_t len)
343 hwaddr mapped_len = 0;
344 struct iovec *ventry;
345 assert(pkt);
346 assert(pkt->max_raw_frags > pkt->raw_frags);
348 if (!len) {
349 return true;
352 ventry = &pkt->raw[pkt->raw_frags];
353 mapped_len = len;
355 ventry->iov_base = cpu_physical_memory_map(pa, &mapped_len, false);
356 ventry->iov_len = mapped_len;
357 pkt->raw_frags += !!ventry->iov_base;
359 if ((ventry->iov_base == NULL) || (len != mapped_len)) {
360 return false;
363 return true;
366 eth_pkt_types_e vmxnet_tx_pkt_get_packet_type(struct VmxnetTxPkt *pkt)
368 assert(pkt);
370 return pkt->packet_type;
373 size_t vmxnet_tx_pkt_get_total_len(struct VmxnetTxPkt *pkt)
375 assert(pkt);
377 return pkt->hdr_len + pkt->payload_len;
380 void vmxnet_tx_pkt_dump(struct VmxnetTxPkt *pkt)
382 #ifdef VMXNET_TX_PKT_DEBUG
383 assert(pkt);
385 printf("TX PKT: hdr_len: %d, pkt_type: 0x%X, l2hdr_len: %lu, "
386 "l3hdr_len: %lu, payload_len: %u\n", pkt->hdr_len, pkt->packet_type,
387 pkt->vec[VMXNET_TX_PKT_L2HDR_FRAG].iov_len,
388 pkt->vec[VMXNET_TX_PKT_L3HDR_FRAG].iov_len, pkt->payload_len);
389 #endif
392 void vmxnet_tx_pkt_reset(struct VmxnetTxPkt *pkt)
394 int i;
396 /* no assert, as reset can be called before tx_pkt_init */
397 if (!pkt) {
398 return;
401 memset(&pkt->virt_hdr, 0, sizeof(pkt->virt_hdr));
403 g_free(pkt->vec[VMXNET_TX_PKT_L3HDR_FRAG].iov_base);
404 pkt->vec[VMXNET_TX_PKT_L3HDR_FRAG].iov_base = NULL;
406 assert(pkt->vec);
407 for (i = VMXNET_TX_PKT_L2HDR_FRAG;
408 i < pkt->payload_frags + VMXNET_TX_PKT_PL_START_FRAG; i++) {
409 pkt->vec[i].iov_len = 0;
411 pkt->payload_len = 0;
412 pkt->payload_frags = 0;
414 assert(pkt->raw);
415 for (i = 0; i < pkt->raw_frags; i++) {
416 assert(pkt->raw[i].iov_base);
417 cpu_physical_memory_unmap(pkt->raw[i].iov_base, pkt->raw[i].iov_len,
418 false, pkt->raw[i].iov_len);
419 pkt->raw[i].iov_len = 0;
421 pkt->raw_frags = 0;
423 pkt->hdr_len = 0;
424 pkt->packet_type = 0;
425 pkt->l4proto = 0;
428 static void vmxnet_tx_pkt_do_sw_csum(struct VmxnetTxPkt *pkt)
430 struct iovec *iov = &pkt->vec[VMXNET_TX_PKT_L2HDR_FRAG];
431 uint32_t csum_cntr;
432 uint16_t csum = 0;
433 /* num of iovec without vhdr */
434 uint32_t iov_len = pkt->payload_frags + VMXNET_TX_PKT_PL_START_FRAG - 1;
435 uint16_t csl;
436 struct ip_header *iphdr;
437 size_t csum_offset = pkt->virt_hdr.csum_start + pkt->virt_hdr.csum_offset;
439 /* Put zero to checksum field */
440 iov_from_buf(iov, iov_len, csum_offset, &csum, sizeof csum);
442 /* Calculate L4 TCP/UDP checksum */
443 csl = pkt->payload_len;
445 /* data checksum */
446 csum_cntr =
447 net_checksum_add_iov(iov, iov_len, pkt->virt_hdr.csum_start, csl);
448 /* add pseudo header to csum */
449 iphdr = pkt->vec[VMXNET_TX_PKT_L3HDR_FRAG].iov_base;
450 csum_cntr += eth_calc_pseudo_hdr_csum(iphdr, csl);
452 /* Put the checksum obtained into the packet */
453 csum = cpu_to_be16(net_checksum_finish(csum_cntr));
454 iov_from_buf(iov, iov_len, csum_offset, &csum, sizeof csum);
457 enum {
458 VMXNET_TX_PKT_FRAGMENT_L2_HDR_POS = 0,
459 VMXNET_TX_PKT_FRAGMENT_L3_HDR_POS,
460 VMXNET_TX_PKT_FRAGMENT_HEADER_NUM
463 #define VMXNET_MAX_FRAG_SG_LIST (64)
465 static size_t vmxnet_tx_pkt_fetch_fragment(struct VmxnetTxPkt *pkt,
466 int *src_idx, size_t *src_offset, struct iovec *dst, int *dst_idx)
468 size_t fetched = 0;
469 struct iovec *src = pkt->vec;
471 *dst_idx = VMXNET_TX_PKT_FRAGMENT_HEADER_NUM;
473 while (fetched < pkt->virt_hdr.gso_size) {
475 /* no more place in fragment iov */
476 if (*dst_idx == VMXNET_MAX_FRAG_SG_LIST) {
477 break;
480 /* no more data in iovec */
481 if (*src_idx == (pkt->payload_frags + VMXNET_TX_PKT_PL_START_FRAG)) {
482 break;
486 dst[*dst_idx].iov_base = src[*src_idx].iov_base + *src_offset;
487 dst[*dst_idx].iov_len = MIN(src[*src_idx].iov_len - *src_offset,
488 pkt->virt_hdr.gso_size - fetched);
490 *src_offset += dst[*dst_idx].iov_len;
491 fetched += dst[*dst_idx].iov_len;
493 if (*src_offset == src[*src_idx].iov_len) {
494 *src_offset = 0;
495 (*src_idx)++;
498 (*dst_idx)++;
501 return fetched;
504 static bool vmxnet_tx_pkt_do_sw_fragmentation(struct VmxnetTxPkt *pkt,
505 NetClientState *nc)
507 struct iovec fragment[VMXNET_MAX_FRAG_SG_LIST];
508 size_t fragment_len = 0;
509 bool more_frags = false;
511 /* some pointers for shorter code */
512 void *l2_iov_base, *l3_iov_base;
513 size_t l2_iov_len, l3_iov_len;
514 int src_idx = VMXNET_TX_PKT_PL_START_FRAG, dst_idx;
515 size_t src_offset = 0;
516 size_t fragment_offset = 0;
518 l2_iov_base = pkt->vec[VMXNET_TX_PKT_L2HDR_FRAG].iov_base;
519 l2_iov_len = pkt->vec[VMXNET_TX_PKT_L2HDR_FRAG].iov_len;
520 l3_iov_base = pkt->vec[VMXNET_TX_PKT_L3HDR_FRAG].iov_base;
521 l3_iov_len = pkt->vec[VMXNET_TX_PKT_L3HDR_FRAG].iov_len;
523 /* Copy headers */
524 fragment[VMXNET_TX_PKT_FRAGMENT_L2_HDR_POS].iov_base = l2_iov_base;
525 fragment[VMXNET_TX_PKT_FRAGMENT_L2_HDR_POS].iov_len = l2_iov_len;
526 fragment[VMXNET_TX_PKT_FRAGMENT_L3_HDR_POS].iov_base = l3_iov_base;
527 fragment[VMXNET_TX_PKT_FRAGMENT_L3_HDR_POS].iov_len = l3_iov_len;
530 /* Put as much data as possible and send */
531 do {
532 fragment_len = vmxnet_tx_pkt_fetch_fragment(pkt, &src_idx, &src_offset,
533 fragment, &dst_idx);
535 more_frags = (fragment_offset + fragment_len < pkt->payload_len);
537 eth_setup_ip4_fragmentation(l2_iov_base, l2_iov_len, l3_iov_base,
538 l3_iov_len, fragment_len, fragment_offset, more_frags);
540 eth_fix_ip4_checksum(l3_iov_base, l3_iov_len);
542 qemu_sendv_packet(nc, fragment, dst_idx);
544 fragment_offset += fragment_len;
546 } while (more_frags);
548 return true;
551 bool vmxnet_tx_pkt_send(struct VmxnetTxPkt *pkt, NetClientState *nc)
553 assert(pkt);
555 if (!pkt->has_virt_hdr &&
556 pkt->virt_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
557 vmxnet_tx_pkt_do_sw_csum(pkt);
561 * Since underlying infrastructure does not support IP datagrams longer
562 * than 64K we should drop such packets and don't even try to send
564 if (VIRTIO_NET_HDR_GSO_NONE != pkt->virt_hdr.gso_type) {
565 if (pkt->payload_len >
566 ETH_MAX_IP_DGRAM_LEN -
567 pkt->vec[VMXNET_TX_PKT_L3HDR_FRAG].iov_len) {
568 return false;
572 if (pkt->has_virt_hdr ||
573 pkt->virt_hdr.gso_type == VIRTIO_NET_HDR_GSO_NONE) {
574 qemu_sendv_packet(nc, pkt->vec,
575 pkt->payload_frags + VMXNET_TX_PKT_PL_START_FRAG);
576 return true;
579 return vmxnet_tx_pkt_do_sw_fragmentation(pkt, nc);