Skip to content

Commit

Permalink
[NET]: Clean up sk_buff walkers.
Browse files Browse the repository at this point in the history
I noticed recently that, in skb_checksum(), "offset" and "start" are
essentially the same thing and have the same value throughout the
function, despite being computed differently. Using a single variable
allows some cleanups and makes the skb_checksum() function smaller,
more readable, and presumably marginally faster.

We appear to have many other "sk_buff walker" functions built on the
exact same model, so the cleanup applies to them, too. Here is a list
of the functions I found to be affected:

net/appletalk/ddp.c:atalk_sum_skb()
net/core/datagram.c:skb_copy_datagram_iovec()
net/core/datagram.c:skb_copy_and_csum_datagram()
net/core/skbuff.c:skb_copy_bits()
net/core/skbuff.c:skb_store_bits()
net/core/skbuff.c:skb_checksum()
net/core/skbuff.c:skb_copy_and_csum_bit()
net/core/user_dma.c:dma_skb_copy_datagram_iovec()
net/xfrm/xfrm_algo.c:skb_icv_walk()
net/xfrm/xfrm_algo.c:skb_to_sgvec()

OTOH, I admit I'm a bit surprised, the cleanup is rather obvious so I'm
really wondering if I am missing something. Can anyone please comment
on this?

Signed-off-by: Jean Delvare <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
jdelvare authored and davem330 committed Apr 26, 2007
1 parent 28d8909 commit eefa390
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 158 deletions.
25 changes: 9 additions & 16 deletions net/appletalk/ddp.c
Original file line number Diff line number Diff line change
Expand Up @@ -937,11 +937,11 @@ static unsigned long atalk_sum_partial(const unsigned char *data,
static unsigned long atalk_sum_skb(const struct sk_buff *skb, int offset,
int len, unsigned long sum)
{
int start = skb_headlen(skb);
int end = skb_headlen(skb);
int i, copy;

/* checksum stuff in header space */
if ( (copy = start - offset) > 0) {
if ((copy = end - offset) > 0) {
if (copy > len)
copy = len;
sum = atalk_sum_partial(skb->data + offset, copy, sum);
Expand All @@ -953,48 +953,41 @@ static unsigned long atalk_sum_skb(const struct sk_buff *skb, int offset,

/* checksum stuff in frags */
for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
int end;
BUG_TRAP(len >= 0);

BUG_TRAP(start <= offset + len);

end = start + skb_shinfo(skb)->frags[i].size;
end = offset + skb_shinfo(skb)->frags[i].size;
if ((copy = end - offset) > 0) {
u8 *vaddr;
skb_frag_t *frag = &skb_shinfo(skb)->frags[i];

if (copy > len)
copy = len;
vaddr = kmap_skb_frag(frag);
sum = atalk_sum_partial(vaddr + frag->page_offset +
offset - start, copy, sum);
sum = atalk_sum_partial(vaddr + frag->page_offset,
copy, sum);
kunmap_skb_frag(vaddr);

if (!(len -= copy))
return sum;
offset += copy;
}
start = end;
}

if (skb_shinfo(skb)->frag_list) {
struct sk_buff *list = skb_shinfo(skb)->frag_list;

for (; list; list = list->next) {
int end;

BUG_TRAP(start <= offset + len);
BUG_TRAP(len >= 0);

end = start + list->len;
end = offset + list->len;
if ((copy = end - offset) > 0) {
if (copy > len)
copy = len;
sum = atalk_sum_skb(list, offset - start,
copy, sum);
sum = atalk_sum_skb(list, 0, copy, sum);
if ((len -= copy) == 0)
return sum;
offset += copy;
}
start = end;
}
}

Expand Down
50 changes: 17 additions & 33 deletions net/core/datagram.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ EXPORT_SYMBOL(skb_kill_datagram);
int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
struct iovec *to, int len)
{
int start = skb_headlen(skb);
int i, copy = start - offset;
int end = skb_headlen(skb);
int i, copy = end - offset;

/* Copy header. */
if (copy > 0) {
Expand All @@ -263,11 +263,9 @@ int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,

/* Copy paged appendix. Hmm... why does this look so complicated? */
for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
int end;
BUG_TRAP(len >= 0);

BUG_TRAP(start <= offset + len);

end = start + skb_shinfo(skb)->frags[i].size;
end = offset + skb_shinfo(skb)->frags[i].size;
if ((copy = end - offset) > 0) {
int err;
u8 *vaddr;
Expand All @@ -277,39 +275,33 @@ int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
if (copy > len)
copy = len;
vaddr = kmap(page);
err = memcpy_toiovec(to, vaddr + frag->page_offset +
offset - start, copy);
err = memcpy_toiovec(to, vaddr + frag->page_offset,
copy);
kunmap(page);
if (err)
goto fault;
if (!(len -= copy))
return 0;
offset += copy;
}
start = end;
}

if (skb_shinfo(skb)->frag_list) {
struct sk_buff *list = skb_shinfo(skb)->frag_list;

for (; list; list = list->next) {
int end;

BUG_TRAP(start <= offset + len);
BUG_TRAP(len >= 0);

end = start + list->len;
end = offset + list->len;
if ((copy = end - offset) > 0) {
if (copy > len)
copy = len;
if (skb_copy_datagram_iovec(list,
offset - start,
to, copy))
if (skb_copy_datagram_iovec(list, 0, to, copy))
goto fault;
if ((len -= copy) == 0)
return 0;
offset += copy;
}
start = end;
}
}
if (!len)
Expand All @@ -323,9 +315,9 @@ static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
u8 __user *to, int len,
__wsum *csump)
{
int start = skb_headlen(skb);
int end = skb_headlen(skb);
int pos = 0;
int i, copy = start - offset;
int i, copy = end - offset;

/* Copy header. */
if (copy > 0) {
Expand All @@ -344,11 +336,9 @@ static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
}

for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
int end;
BUG_TRAP(len >= 0);

BUG_TRAP(start <= offset + len);

end = start + skb_shinfo(skb)->frags[i].size;
end = offset + skb_shinfo(skb)->frags[i].size;
if ((copy = end - offset) > 0) {
__wsum csum2;
int err = 0;
Expand All @@ -360,8 +350,7 @@ static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
copy = len;
vaddr = kmap(page);
csum2 = csum_and_copy_to_user(vaddr +
frag->page_offset +
offset - start,
frag->page_offset,
to, copy, 0, &err);
kunmap(page);
if (err)
Expand All @@ -373,24 +362,20 @@ static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
to += copy;
pos += copy;
}
start = end;
}

if (skb_shinfo(skb)->frag_list) {
struct sk_buff *list = skb_shinfo(skb)->frag_list;

for (; list; list=list->next) {
int end;

BUG_TRAP(start <= offset + len);
BUG_TRAP(len >= 0);

end = start + list->len;
end = offset + list->len;
if ((copy = end - offset) > 0) {
__wsum csum2 = 0;
if (copy > len)
copy = len;
if (skb_copy_and_csum_datagram(list,
offset - start,
if (skb_copy_and_csum_datagram(list, 0,
to, copy,
&csum2))
goto fault;
Expand All @@ -401,7 +386,6 @@ static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
to += copy;
pos += copy;
}
start = end;
}
}
if (!len)
Expand Down
Loading

0 comments on commit eefa390

Please sign in to comment.