2 * QEMU RX packets abstractions
4 * Copyright (c) 2012 Ravello Systems LTD (http://ravellosystems.com)
6 * Developed by Daynix Computing LTD (http://www.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 "qemu/osdep.h"
20 #include "net_rx_pkt.h"
21 #include "net/checksum.h"
25 struct virtio_net_hdr virt_hdr;
26 uint8_t ehdr_buf[sizeof(struct eth_header)];
28 uint16_t vec_len_total;
34 eth_pkt_types_e packet_type;
36 /* Analysis results */
46 eth_ip6_hdr_info ip6hdr_info;
47 eth_ip4_hdr_info ip4hdr_info;
48 eth_l4_hdr_info l4hdr_info;
51 void net_rx_pkt_init(struct NetRxPkt **pkt, bool has_virt_hdr)
53 struct NetRxPkt *p = g_malloc0(sizeof *p);
54 p->has_virt_hdr = has_virt_hdr;
60 void net_rx_pkt_uninit(struct NetRxPkt *pkt)
62 if (pkt->vec_len_total != 0) {
69 struct virtio_net_hdr *net_rx_pkt_get_vhdr(struct NetRxPkt *pkt)
72 return &pkt->virt_hdr;
76 net_rx_pkt_iovec_realloc(struct NetRxPkt *pkt,
79 if (pkt->vec_len_total < new_iov_len) {
81 pkt->vec = g_malloc(sizeof(*pkt->vec) * new_iov_len);
82 pkt->vec_len_total = new_iov_len;
87 net_rx_pkt_pull_data(struct NetRxPkt *pkt,
88 const struct iovec *iov, int iovcnt,
91 if (pkt->vlan_stripped) {
92 net_rx_pkt_iovec_realloc(pkt, iovcnt + 1);
94 pkt->vec[0].iov_base = pkt->ehdr_buf;
95 pkt->vec[0].iov_len = sizeof(pkt->ehdr_buf);
98 iov_size(iov, iovcnt) - ploff + sizeof(struct eth_header);
100 pkt->vec_len = iov_copy(pkt->vec + 1, pkt->vec_len_total - 1,
101 iov, iovcnt, ploff, pkt->tot_len);
103 net_rx_pkt_iovec_realloc(pkt, iovcnt);
105 pkt->tot_len = iov_size(iov, iovcnt) - ploff;
106 pkt->vec_len = iov_copy(pkt->vec, pkt->vec_len_total,
107 iov, iovcnt, ploff, pkt->tot_len);
110 eth_get_protocols(pkt->vec, pkt->vec_len, &pkt->isip4, &pkt->isip6,
111 &pkt->isudp, &pkt->istcp,
112 &pkt->l3hdr_off, &pkt->l4hdr_off, &pkt->l5hdr_off,
113 &pkt->ip6hdr_info, &pkt->ip4hdr_info, &pkt->l4hdr_info);
115 trace_net_rx_pkt_parsed(pkt->isip4, pkt->isip6, pkt->isudp, pkt->istcp,
116 pkt->l3hdr_off, pkt->l4hdr_off, pkt->l5hdr_off);
119 void net_rx_pkt_attach_iovec(struct NetRxPkt *pkt,
120 const struct iovec *iov, int iovcnt,
121 size_t iovoff, bool strip_vlan)
124 uint16_t ploff = iovoff;
126 pkt->vlan_stripped = false;
129 pkt->vlan_stripped = eth_strip_vlan(iov, iovcnt, iovoff, pkt->ehdr_buf,
135 net_rx_pkt_pull_data(pkt, iov, iovcnt, ploff);
138 void net_rx_pkt_attach_iovec_ex(struct NetRxPkt *pkt,
139 const struct iovec *iov, int iovcnt,
140 size_t iovoff, bool strip_vlan,
144 uint16_t ploff = iovoff;
146 pkt->vlan_stripped = false;
149 pkt->vlan_stripped = eth_strip_vlan_ex(iov, iovcnt, iovoff, vet,
156 net_rx_pkt_pull_data(pkt, iov, iovcnt, ploff);
159 void net_rx_pkt_dump(struct NetRxPkt *pkt)
161 #ifdef NET_RX_PKT_DEBUG
164 printf("RX PKT: tot_len: %d, vlan_stripped: %d, vlan_tag: %d\n",
165 pkt->tot_len, pkt->vlan_stripped, pkt->tci);
169 void net_rx_pkt_set_packet_type(struct NetRxPkt *pkt,
170 eth_pkt_types_e packet_type)
174 pkt->packet_type = packet_type;
178 eth_pkt_types_e net_rx_pkt_get_packet_type(struct NetRxPkt *pkt)
182 return pkt->packet_type;
185 size_t net_rx_pkt_get_total_len(struct NetRxPkt *pkt)
192 void net_rx_pkt_set_protocols(struct NetRxPkt *pkt, const void *data,
195 const struct iovec iov = {
196 .iov_base = (void *)data,
202 eth_get_protocols(&iov, 1, &pkt->isip4, &pkt->isip6,
203 &pkt->isudp, &pkt->istcp,
204 &pkt->l3hdr_off, &pkt->l4hdr_off, &pkt->l5hdr_off,
205 &pkt->ip6hdr_info, &pkt->ip4hdr_info, &pkt->l4hdr_info);
208 void net_rx_pkt_get_protocols(struct NetRxPkt *pkt,
209 bool *isip4, bool *isip6,
210 bool *isudp, bool *istcp)
220 size_t net_rx_pkt_get_l3_hdr_offset(struct NetRxPkt *pkt)
223 return pkt->l3hdr_off;
226 size_t net_rx_pkt_get_l4_hdr_offset(struct NetRxPkt *pkt)
229 return pkt->l4hdr_off;
232 size_t net_rx_pkt_get_l5_hdr_offset(struct NetRxPkt *pkt)
235 return pkt->l5hdr_off;
238 eth_ip6_hdr_info *net_rx_pkt_get_ip6_info(struct NetRxPkt *pkt)
240 return &pkt->ip6hdr_info;
243 eth_ip4_hdr_info *net_rx_pkt_get_ip4_info(struct NetRxPkt *pkt)
245 return &pkt->ip4hdr_info;
248 eth_l4_hdr_info *net_rx_pkt_get_l4_info(struct NetRxPkt *pkt)
250 return &pkt->l4hdr_info;
254 _net_rx_rss_add_chunk(uint8_t *rss_input, size_t *bytes_written,
255 void *ptr, size_t size)
257 memcpy(&rss_input[*bytes_written], ptr, size);
258 trace_net_rx_pkt_rss_add_chunk(ptr, size, *bytes_written);
259 *bytes_written += size;
263 _net_rx_rss_prepare_ip4(uint8_t *rss_input,
264 struct NetRxPkt *pkt,
265 size_t *bytes_written)
267 struct ip_header *ip4_hdr = &pkt->ip4hdr_info.ip4_hdr;
269 _net_rx_rss_add_chunk(rss_input, bytes_written,
270 &ip4_hdr->ip_src, sizeof(uint32_t));
272 _net_rx_rss_add_chunk(rss_input, bytes_written,
273 &ip4_hdr->ip_dst, sizeof(uint32_t));
277 _net_rx_rss_prepare_ip6(uint8_t *rss_input,
278 struct NetRxPkt *pkt,
279 bool ipv6ex, size_t *bytes_written)
281 eth_ip6_hdr_info *ip6info = &pkt->ip6hdr_info;
283 _net_rx_rss_add_chunk(rss_input, bytes_written,
284 (ipv6ex && ip6info->rss_ex_src_valid) ? &ip6info->rss_ex_src
285 : &ip6info->ip6_hdr.ip6_src,
286 sizeof(struct in6_address));
288 _net_rx_rss_add_chunk(rss_input, bytes_written,
289 (ipv6ex && ip6info->rss_ex_dst_valid) ? &ip6info->rss_ex_dst
290 : &ip6info->ip6_hdr.ip6_dst,
291 sizeof(struct in6_address));
295 _net_rx_rss_prepare_tcp(uint8_t *rss_input,
296 struct NetRxPkt *pkt,
297 size_t *bytes_written)
299 struct tcp_header *tcphdr = &pkt->l4hdr_info.hdr.tcp;
301 _net_rx_rss_add_chunk(rss_input, bytes_written,
302 &tcphdr->th_sport, sizeof(uint16_t));
304 _net_rx_rss_add_chunk(rss_input, bytes_written,
305 &tcphdr->th_dport, sizeof(uint16_t));
309 net_rx_pkt_calc_rss_hash(struct NetRxPkt *pkt,
310 NetRxPktRssType type,
313 uint8_t rss_input[36];
314 size_t rss_length = 0;
315 uint32_t rss_hash = 0;
316 net_toeplitz_key key_data;
321 trace_net_rx_pkt_rss_ip4();
322 _net_rx_rss_prepare_ip4(&rss_input[0], pkt, &rss_length);
324 case NetPktRssIpV4Tcp:
327 trace_net_rx_pkt_rss_ip4_tcp();
328 _net_rx_rss_prepare_ip4(&rss_input[0], pkt, &rss_length);
329 _net_rx_rss_prepare_tcp(&rss_input[0], pkt, &rss_length);
331 case NetPktRssIpV6Tcp:
334 trace_net_rx_pkt_rss_ip6_tcp();
335 _net_rx_rss_prepare_ip6(&rss_input[0], pkt, true, &rss_length);
336 _net_rx_rss_prepare_tcp(&rss_input[0], pkt, &rss_length);
340 trace_net_rx_pkt_rss_ip6();
341 _net_rx_rss_prepare_ip6(&rss_input[0], pkt, false, &rss_length);
343 case NetPktRssIpV6Ex:
345 trace_net_rx_pkt_rss_ip6_ex();
346 _net_rx_rss_prepare_ip6(&rss_input[0], pkt, true, &rss_length);
353 net_toeplitz_key_init(&key_data, key);
354 net_toeplitz_add(&rss_hash, rss_input, rss_length, &key_data);
356 trace_net_rx_pkt_rss_hash(rss_length, rss_hash);
361 uint16_t net_rx_pkt_get_ip_id(struct NetRxPkt *pkt)
366 return be16_to_cpu(pkt->ip4hdr_info.ip4_hdr.ip_id);
372 bool net_rx_pkt_is_tcp_ack(struct NetRxPkt *pkt)
377 return TCP_HEADER_FLAGS(&pkt->l4hdr_info.hdr.tcp) & TCP_FLAG_ACK;
383 bool net_rx_pkt_has_tcp_data(struct NetRxPkt *pkt)
388 return pkt->l4hdr_info.has_tcp_data;
394 struct iovec *net_rx_pkt_get_iovec(struct NetRxPkt *pkt)
401 uint16_t net_rx_pkt_get_iovec_len(struct NetRxPkt *pkt)
408 void net_rx_pkt_set_vhdr(struct NetRxPkt *pkt,
409 struct virtio_net_hdr *vhdr)
413 memcpy(&pkt->virt_hdr, vhdr, sizeof pkt->virt_hdr);
416 void net_rx_pkt_set_vhdr_iovec(struct NetRxPkt *pkt,
417 const struct iovec *iov, int iovcnt)
421 iov_to_buf(iov, iovcnt, 0, &pkt->virt_hdr, sizeof pkt->virt_hdr);
424 bool net_rx_pkt_is_vlan_stripped(struct NetRxPkt *pkt)
428 return pkt->vlan_stripped;
431 bool net_rx_pkt_has_virt_hdr(struct NetRxPkt *pkt)
435 return pkt->has_virt_hdr;
438 uint16_t net_rx_pkt_get_vlan_tag(struct NetRxPkt *pkt)
445 bool net_rx_pkt_validate_l3_csum(struct NetRxPkt *pkt, bool *csum_valid)
451 trace_net_rx_pkt_l3_csum_validate_entry();
454 trace_net_rx_pkt_l3_csum_validate_not_ip4();
458 csl = pkt->l4hdr_off - pkt->l3hdr_off;
460 cntr = net_checksum_add_iov(pkt->vec, pkt->vec_len,
464 csum = net_checksum_finish(cntr);
466 *csum_valid = (csum == 0);
468 trace_net_rx_pkt_l3_csum_validate_csum(pkt->l3hdr_off, csl,
469 cntr, csum, *csum_valid);
475 _net_rx_pkt_calc_l4_csum(struct NetRxPkt *pkt)
482 trace_net_rx_pkt_l4_csum_calc_entry();
486 csl = be16_to_cpu(pkt->l4hdr_info.hdr.udp.uh_ulen);
487 trace_net_rx_pkt_l4_csum_calc_ip4_udp();
489 csl = be16_to_cpu(pkt->ip4hdr_info.ip4_hdr.ip_len) -
490 IP_HDR_GET_LEN(&pkt->ip4hdr_info.ip4_hdr);
491 trace_net_rx_pkt_l4_csum_calc_ip4_tcp();
494 cntr = eth_calc_ip4_pseudo_hdr_csum(&pkt->ip4hdr_info.ip4_hdr,
496 trace_net_rx_pkt_l4_csum_calc_ph_csum(cntr, csl);
499 csl = be16_to_cpu(pkt->l4hdr_info.hdr.udp.uh_ulen);
500 trace_net_rx_pkt_l4_csum_calc_ip6_udp();
502 struct ip6_header *ip6hdr = &pkt->ip6hdr_info.ip6_hdr;
503 size_t full_ip6hdr_len = pkt->l4hdr_off - pkt->l3hdr_off;
504 size_t ip6opts_len = full_ip6hdr_len - sizeof(struct ip6_header);
506 csl = be16_to_cpu(ip6hdr->ip6_ctlun.ip6_un1.ip6_un1_plen) -
508 trace_net_rx_pkt_l4_csum_calc_ip6_tcp();
511 cntr = eth_calc_ip6_pseudo_hdr_csum(&pkt->ip6hdr_info.ip6_hdr, csl,
512 pkt->ip6hdr_info.l4proto, &cso);
513 trace_net_rx_pkt_l4_csum_calc_ph_csum(cntr, csl);
516 cntr += net_checksum_add_iov(pkt->vec, pkt->vec_len,
517 pkt->l4hdr_off, csl, cso);
519 csum = net_checksum_finish(cntr);
521 trace_net_rx_pkt_l4_csum_calc_csum(pkt->l4hdr_off, csl, cntr, csum);
526 bool net_rx_pkt_validate_l4_csum(struct NetRxPkt *pkt, bool *csum_valid)
530 trace_net_rx_pkt_l4_csum_validate_entry();
532 if (!pkt->istcp && !pkt->isudp) {
533 trace_net_rx_pkt_l4_csum_validate_not_xxp();
537 if (pkt->isudp && (pkt->l4hdr_info.hdr.udp.uh_sum == 0)) {
538 trace_net_rx_pkt_l4_csum_validate_udp_with_no_checksum();
542 if (pkt->isip4 && pkt->ip4hdr_info.fragment) {
543 trace_net_rx_pkt_l4_csum_validate_ip4_fragment();
547 csum = _net_rx_pkt_calc_l4_csum(pkt);
549 *csum_valid = ((csum == 0) || (csum == 0xFFFF));
551 trace_net_rx_pkt_l4_csum_validate_csum(*csum_valid);
556 bool net_rx_pkt_fix_l4_csum(struct NetRxPkt *pkt)
561 trace_net_rx_pkt_l4_csum_fix_entry();
564 l4_cso = offsetof(struct tcp_header, th_sum);
565 trace_net_rx_pkt_l4_csum_fix_tcp(l4_cso);
566 } else if (pkt->isudp) {
567 if (pkt->l4hdr_info.hdr.udp.uh_sum == 0) {
568 trace_net_rx_pkt_l4_csum_fix_udp_with_no_checksum();
571 l4_cso = offsetof(struct udp_header, uh_sum);
572 trace_net_rx_pkt_l4_csum_fix_udp(l4_cso);
574 trace_net_rx_pkt_l4_csum_fix_not_xxp();
578 if (pkt->isip4 && pkt->ip4hdr_info.fragment) {
579 trace_net_rx_pkt_l4_csum_fix_ip4_fragment();
583 /* Set zero to checksum word */
584 iov_from_buf(pkt->vec, pkt->vec_len,
585 pkt->l4hdr_off + l4_cso,
586 &csum, sizeof(csum));
588 /* Calculate L4 checksum */
589 csum = cpu_to_be16(_net_rx_pkt_calc_l4_csum(pkt));
591 /* Set calculated checksum to checksum word */
592 iov_from_buf(pkt->vec, pkt->vec_len,
593 pkt->l4hdr_off + l4_cso,
594 &csum, sizeof(csum));
596 trace_net_rx_pkt_l4_csum_fix_csum(pkt->l4hdr_off + l4_cso, csum);