net: helper function skb_len_add
authorRichard Gobert <richardbgobert@gmail.com>
Wed, 22 Jun 2022 16:09:03 +0000 (18:09 +0200)
committerJakub Kicinski <kuba@kernel.org>
Fri, 24 Jun 2022 23:24:38 +0000 (16:24 -0700)
Move the len fields manipulation in the skbs to a helper function.
There is a comment specifically requesting this and there are several
other areas in the code displaying the same pattern which can be
refactored.
This improves code readability.

Signed-off-by: Richard Gobert <richardbgobert@gmail.com>
Link: https://lore.kernel.org/r/20220622160853.GA6478@debian
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/linux/skbuff.h
include/net/sock.h
net/core/skbuff.c
net/ipv4/esp4.c
net/ipv4/ip_output.c

index cd4a826..f6a27ab 100644 (file)
@@ -2352,6 +2352,18 @@ static inline unsigned int skb_pagelen(const struct sk_buff *skb)
 }
 
 /**
+ * skb_len_add - adds a number to len fields of skb
+ * @skb: buffer to add len to
+ * @delta: number of bytes to add
+ */
+static inline void skb_len_add(struct sk_buff *skb, int delta)
+{
+       skb->len += delta;
+       skb->data_len += delta;
+       skb->truesize += delta;
+}
+
+/**
  * __skb_fill_page_desc - initialise a paged fragment in an skb
  * @skb: buffer containing fragment to be initialised
  * @i: paged fragment index to initialise
index 5bed1ea..40bbd0e 100644 (file)
@@ -2219,9 +2219,7 @@ static inline int skb_copy_to_page_nocache(struct sock *sk, struct iov_iter *fro
        if (err)
                return err;
 
-       skb->len             += copy;
-       skb->data_len        += copy;
-       skb->truesize        += copy;
+       skb_len_add(skb, copy);
        sk_wmem_queued_add(sk, copy);
        sk_mem_charge(sk, copy);
        return 0;
index 00bf35e..c62e42d 100644 (file)
@@ -3195,9 +3195,7 @@ skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
                }
        }
 
-       to->truesize += len + plen;
-       to->len += len + plen;
-       to->data_len += len + plen;
+       skb_len_add(to, len + plen);
 
        if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
                skb_tx_error(from);
@@ -3634,13 +3632,8 @@ onlymerged:
        tgt->ip_summed = CHECKSUM_PARTIAL;
        skb->ip_summed = CHECKSUM_PARTIAL;
 
-       /* Yak, is it really working this way? Some helper please? */
-       skb->len -= shiftlen;
-       skb->data_len -= shiftlen;
-       skb->truesize -= shiftlen;
-       tgt->len += shiftlen;
-       tgt->data_len += shiftlen;
-       tgt->truesize += shiftlen;
+       skb_len_add(skb, -shiftlen);
+       skb_len_add(tgt, shiftlen);
 
        return shiftlen;
 }
index b21238d..7eae8d6 100644 (file)
@@ -502,9 +502,7 @@ int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *
 
                        nfrags++;
 
-                       skb->len += tailen;
-                       skb->data_len += tailen;
-                       skb->truesize += tailen;
+                       skb_len_add(skb, tailen);
                        if (sk && sk_fullsock(sk))
                                refcount_add(tailen, &sk->sk_wmem_alloc);
 
index 00b4bf2..5e32a2f 100644 (file)
@@ -1214,9 +1214,7 @@ alloc_new_skb:
 
                        pfrag->offset += copy;
                        skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
-                       skb->len += copy;
-                       skb->data_len += copy;
-                       skb->truesize += copy;
+                       skb_len_add(skb, copy);
                        wmem_alloc_delta += copy;
                } else {
                        err = skb_zerocopy_iter_dgram(skb, from, copy);
@@ -1443,9 +1441,7 @@ ssize_t   ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
                        skb->csum = csum_block_add(skb->csum, csum, skb->len);
                }
 
-               skb->len += len;
-               skb->data_len += len;
-               skb->truesize += len;
+               skb_len_add(skb, len);
                refcount_add(len, &sk->sk_wmem_alloc);
                offset += len;
                size -= len;