]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - include/linux/virtio_net.h
56436472ccc774e43a372d3e92fb2da6a4957792
[mirror_ubuntu-bionic-kernel.git] / include / linux / virtio_net.h
1 #ifndef _LINUX_VIRTIO_NET_H
2 #define _LINUX_VIRTIO_NET_H
3
4 #include <linux/if_vlan.h>
5 #include <uapi/linux/virtio_net.h>
6
7 static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
8 const struct virtio_net_hdr *hdr,
9 bool little_endian)
10 {
11 unsigned short gso_type = 0;
12
13 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
14 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
15 case VIRTIO_NET_HDR_GSO_TCPV4:
16 gso_type = SKB_GSO_TCPV4;
17 break;
18 case VIRTIO_NET_HDR_GSO_TCPV6:
19 gso_type = SKB_GSO_TCPV6;
20 break;
21 case VIRTIO_NET_HDR_GSO_UDP:
22 gso_type = SKB_GSO_UDP;
23 break;
24 default:
25 return -EINVAL;
26 }
27
28 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
29 gso_type |= SKB_GSO_TCP_ECN;
30
31 if (hdr->gso_size == 0)
32 return -EINVAL;
33 }
34
35 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
36 u16 start = __virtio16_to_cpu(little_endian, hdr->csum_start);
37 u16 off = __virtio16_to_cpu(little_endian, hdr->csum_offset);
38
39 if (!skb_partial_csum_set(skb, start, off))
40 return -EINVAL;
41 }
42
43 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
44 u16 gso_size = __virtio16_to_cpu(little_endian, hdr->gso_size);
45
46 skb_shinfo(skb)->gso_size = gso_size;
47 skb_shinfo(skb)->gso_type = gso_type;
48
49 /* Header must be checked, and gso_segs computed. */
50 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
51 skb_shinfo(skb)->gso_segs = 0;
52 }
53
54 return 0;
55 }
56
57 static inline int virtio_net_hdr_from_skb(const struct sk_buff *skb,
58 struct virtio_net_hdr *hdr,
59 bool little_endian)
60 {
61 memset(hdr, 0, sizeof(*hdr)); /* no info leak */
62
63 if (skb_is_gso(skb)) {
64 struct skb_shared_info *sinfo = skb_shinfo(skb);
65
66 /* This is a hint as to how much should be linear. */
67 hdr->hdr_len = __cpu_to_virtio16(little_endian,
68 skb_headlen(skb));
69 hdr->gso_size = __cpu_to_virtio16(little_endian,
70 sinfo->gso_size);
71 if (sinfo->gso_type & SKB_GSO_TCPV4)
72 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
73 else if (sinfo->gso_type & SKB_GSO_TCPV6)
74 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
75 else if (sinfo->gso_type & SKB_GSO_UDP)
76 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
77 else
78 return -EINVAL;
79 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
80 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
81 } else
82 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
83
84 if (skb->ip_summed == CHECKSUM_PARTIAL) {
85 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
86 if (skb_vlan_tag_present(skb))
87 hdr->csum_start = __cpu_to_virtio16(little_endian,
88 skb_checksum_start_offset(skb) + VLAN_HLEN);
89 else
90 hdr->csum_start = __cpu_to_virtio16(little_endian,
91 skb_checksum_start_offset(skb));
92 hdr->csum_offset = __cpu_to_virtio16(little_endian,
93 skb->csum_offset);
94 } /* else everything is zero */
95
96 return 0;
97 }
98
99 #endif /* _LINUX_VIRTIO_NET_H */