Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: Allow raw buffers to be passed into the flow dissector.

Drivers, and perhaps other entities we have not yet considered,
sometimes want to know how deep the protocol headers go before
deciding how large of an SKB to allocate and how much of the packet to
place into the linear SKB area.

For example, consider a driver which has a device which DMAs into
pools of pages and then tells the driver where the data went in the
DMA descriptor(s). The driver can then build an SKB and reference
most of the data via SKB fragments (which are page/offset/length
triplets).

However at least some of the front of the packet should be placed into
the linear SKB area, which comes before the fragments, so that packet
processing can get at the headers efficiently. The first thing each
protocol layer is going to do is a "pskb_may_pull()" so we might as
well aggregate as much of this as possible while we're building the
SKB in the driver.

Part of supporting this is that we don't have an SKB yet, so we want
to be able to let the flow dissector operate on a raw buffer in order
to compute the offset of the end of the headers.

So now we have a __skb_flow_dissect() which takes an explicit data
pointer and length.

Signed-off-by: David S. Miller <davem@davemloft.net>

+50 -22
+12 -6
include/linux/skbuff.h
··· 2567 2567 __wsum skb_checksum(const struct sk_buff *skb, int offset, int len, 2568 2568 __wsum csum); 2569 2569 2570 - static inline void *skb_header_pointer(const struct sk_buff *skb, int offset, 2571 - int len, void *buffer) 2570 + static inline void *__skb_header_pointer(const struct sk_buff *skb, int offset, 2571 + int len, void *data, int hlen, void *buffer) 2572 2572 { 2573 - int hlen = skb_headlen(skb); 2574 - 2575 2573 if (hlen - offset >= len) 2576 - return skb->data + offset; 2574 + return data + offset; 2577 2575 2578 - if (skb_copy_bits(skb, offset, buffer, len) < 0) 2576 + if (!skb || 2577 + skb_copy_bits(skb, offset, buffer, len) < 0) 2579 2578 return NULL; 2580 2579 2581 2580 return buffer; 2581 + } 2582 + 2583 + static inline void *skb_header_pointer(const struct sk_buff *skb, int offset, 2584 + int len, void *buffer) 2585 + { 2586 + return __skb_header_pointer(skb, offset, len, skb->data, 2587 + skb_headlen(skb), buffer); 2582 2588 } 2583 2589 2584 2590 /**
+12 -2
include/net/flow_keys.h
··· 27 27 u8 ip_proto; 28 28 }; 29 29 30 - bool skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow); 31 - __be32 skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto); 30 + bool __skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow, 31 + void *data, int hlen); 32 + static inline bool skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow) 33 + { 34 + return __skb_flow_dissect(skb, flow, NULL, 0); 35 + } 36 + __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto, 37 + void *data, int hlen_proto); 38 + static inline __be32 skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto) 39 + { 40 + return __skb_flow_get_ports(skb, thoff, ip_proto, NULL, 0); 41 + } 32 42 u32 flow_hash_from_keys(struct flow_keys *keys); 33 43 #endif
+26 -14
net/core/flow_dissector.c
··· 34 34 * The function will try to retrieve the ports at offset thoff + poff where poff 35 35 * is the protocol port offset returned from proto_ports_offset 36 36 */ 37 - __be32 skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto) 37 + __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto, 38 + void *data, int hlen) 38 39 { 39 40 int poff = proto_ports_offset(ip_proto); 41 + 42 + if (!data) { 43 + data = skb->data; 44 + hlen = skb_headlen(skb); 45 + } 40 46 41 47 if (poff >= 0) { 42 48 __be32 *ports, _ports; 43 49 44 - ports = skb_header_pointer(skb, thoff + poff, 45 - sizeof(_ports), &_ports); 50 + ports = __skb_header_pointer(skb, thoff + poff, 51 + sizeof(_ports), data, hlen, &_ports); 46 52 if (ports) 47 53 return *ports; 48 54 } 49 55 50 56 return 0; 51 57 } 52 - EXPORT_SYMBOL(skb_flow_get_ports); 58 + EXPORT_SYMBOL(__skb_flow_get_ports); 53 59 54 - bool skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow) 60 + bool __skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow, void *data, int hlen) 55 61 { 56 62 int nhoff = skb_network_offset(skb); 57 63 u8 ip_proto; 58 64 __be16 proto = skb->protocol; 65 + 66 + if (!data) { 67 + data = skb->data; 68 + hlen = skb_headlen(skb); 69 + } 59 70 60 71 memset(flow, 0, sizeof(*flow)); 61 72 ··· 76 65 const struct iphdr *iph; 77 66 struct iphdr _iph; 78 67 ip: 79 - iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph); 68 + iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph); 80 69 if (!iph || iph->ihl < 5) 81 70 return false; 82 71 nhoff += iph->ihl * 4; ··· 94 83 __be32 flow_label; 95 84 96 85 ipv6: 97 - iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph); 86 + iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph); 98 87 if (!iph) 99 88 return false; 100 89 ··· 124 113 const struct vlan_hdr *vlan; 125 114 struct vlan_hdr _vlan; 126 115 127 - vlan = skb_header_pointer(skb, nhoff, sizeof(_vlan), &_vlan); 116 + vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), data, hlen, &_vlan); 128 117 if (!vlan) 129 118 return false; 130 119 ··· 137 126 struct pppoe_hdr hdr; 138 127 __be16 proto; 139 128 } *hdr, _hdr; 140 - hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr); 129 + hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); 141 130 if (!hdr) 142 131 return false; 143 132 proto = hdr->proto; ··· 162 151 __be16 proto; 163 152 } *hdr, _hdr; 164 153 165 - hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr); 154 + hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); 166 155 if (!hdr) 167 156 return false; 168 157 /* ··· 182 171 const struct ethhdr *eth; 183 172 struct ethhdr _eth; 184 173 185 - eth = skb_header_pointer(skb, nhoff, 186 - sizeof(_eth), &_eth); 174 + eth = __skb_header_pointer(skb, nhoff, 175 + sizeof(_eth), 176 + data, hlen, &_eth); 187 177 if (!eth) 188 178 return false; 189 179 proto = eth->h_proto; ··· 206 194 207 195 flow->n_proto = proto; 208 196 flow->ip_proto = ip_proto; 209 - flow->ports = skb_flow_get_ports(skb, nhoff, ip_proto); 197 + flow->ports = __skb_flow_get_ports(skb, nhoff, ip_proto, data, hlen); 210 198 flow->thoff = (u16) nhoff; 211 199 212 200 return true; 213 201 } 214 - EXPORT_SYMBOL(skb_flow_dissect); 202 + EXPORT_SYMBOL(__skb_flow_dissect); 215 203 216 204 static u32 hashrnd __read_mostly; 217 205 static __always_inline void __flow_hash_secret_init(void)