diff mbox series

[v4,01/12] net: eth: Add a helper to pad a short Ethernet frame

Message ID 20210316120420.19658-2-bmeng.cn@gmail.com (mailing list archive)
State New, archived
Headers show
Series net: Pad short frames for network backends | expand

Commit Message

Bin Meng March 16, 2021, 12:04 p.m. UTC
Add a helper to pad a short Ethernet frame to the minimum required
length, which can be used by backend codes.

Signed-off-by: Bin Meng <bmeng.cn@gmail.com>

---

Changes in v4:
- change 'ethernet' to 'Ethernet'
- do not inline the helper
- check the padded buffer size to avoid buffer overflow

Changes in v3:
- use 'without' instead of 'sans'
- add a helper to pad short frames

 include/net/eth.h | 17 +++++++++++++++++
 net/eth.c         | 17 +++++++++++++++++
 2 files changed, 34 insertions(+)

Comments

Philippe Mathieu-Daudé March 16, 2021, 3:09 p.m. UTC | #1
On 3/16/21 1:04 PM, Bin Meng wrote:
> Add a helper to pad a short Ethernet frame to the minimum required
> length, which can be used by backend codes.

"backend codes" sounds odd but I'm not native English speaker.
I'd have used "backends' code" instead...

> Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
> 
> ---
> 
> Changes in v4:
> - change 'ethernet' to 'Ethernet'
> - do not inline the helper
> - check the padded buffer size to avoid buffer overflow
> 
> Changes in v3:
> - use 'without' instead of 'sans'
> - add a helper to pad short frames
> 
>  include/net/eth.h | 17 +++++++++++++++++
>  net/eth.c         | 17 +++++++++++++++++
>  2 files changed, 34 insertions(+)
> 
> diff --git a/include/net/eth.h b/include/net/eth.h
> index 0671be6916..6aabbdd0d3 100644
> --- a/include/net/eth.h
> +++ b/include/net/eth.h
> @@ -31,6 +31,7 @@
>  
>  #define ETH_ALEN 6
>  #define ETH_HLEN 14
> +#define ETH_ZLEN 60     /* Min. octets in frame without FCS */
>  
>  struct eth_header {
>      uint8_t  h_dest[ETH_ALEN];   /* destination eth addr */
> @@ -422,4 +423,20 @@ bool
>  eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags,
>                     size_t ip6hdr_off, eth_ip6_hdr_info *info);
>  
> +/**
> + * eth_pad_short_frame - pad a short frame to the minimum Ethernet frame length
> + *
> + * If the Ethernet frame size is shorter than 60 bytes, it will be padded to
> + * 60 bytes at the address @padded_pkt.
> + *
> + * @padded_pkt: buffer address to hold the padded frame
> + * @padded_buflen: buffer length of @padded_pkt. If the frame is padded, it is
> + *                 written to ETH_ZLEN, otherwise it remains unchanged.

 @padded_buflen: pointer holding length of @padded_pkt. If the frame is
                 padded, the length will be updated to the padded one.

Otherwise:
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

(since you describe frames, maybe s/pkt/frame/?)

> + * @pkt: address to hold the original Ethernet frame
> + * @pkt_size: size of the original Ethernet frame
> + * @return true if the frame is padded, otherwise false
> + */
> +bool eth_pad_short_frame(uint8_t *padded_pkt, size_t *padded_buflen,
> +                         const void *pkt, size_t pkt_size);
> +
>  #endif
> diff --git a/net/eth.c b/net/eth.c
> index 1e0821c5f8..f913e4396f 100644
> --- a/net/eth.c
> +++ b/net/eth.c
> @@ -548,3 +548,20 @@ bool eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags,
>      info->l4proto = ext_hdr.ip6r_nxt;
>      return true;
>  }
> +
> +bool eth_pad_short_frame(uint8_t *padded_pkt, size_t *padded_buflen,
> +                         const void *pkt, size_t pkt_size)
> +{
> +    assert(padded_buflen && *padded_buflen >= ETH_ZLEN);
> +
> +    if (pkt_size >= ETH_ZLEN) {
> +        return false;
> +    }
> +
> +    /* pad to minimum Ethernet frame length */
> +    memcpy(padded_pkt, pkt, pkt_size);
> +    memset(&padded_pkt[pkt_size], 0, ETH_ZLEN - pkt_size);
> +    *padded_buflen = ETH_ZLEN;
> +
> +    return true;
> +}
>
Bin Meng March 17, 2021, 6:22 a.m. UTC | #2
On Tue, Mar 16, 2021 at 11:09 PM Philippe Mathieu-Daudé
<philmd@redhat.com> wrote:
>
> On 3/16/21 1:04 PM, Bin Meng wrote:
> > Add a helper to pad a short Ethernet frame to the minimum required
> > length, which can be used by backend codes.
>
> "backend codes" sounds odd but I'm not native English speaker.
> I'd have used "backends' code" instead...
>

Will update in v5.

> > Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
> >
> > ---
> >
> > Changes in v4:
> > - change 'ethernet' to 'Ethernet'
> > - do not inline the helper
> > - check the padded buffer size to avoid buffer overflow
> >
> > Changes in v3:
> > - use 'without' instead of 'sans'
> > - add a helper to pad short frames
> >
> >  include/net/eth.h | 17 +++++++++++++++++
> >  net/eth.c         | 17 +++++++++++++++++
> >  2 files changed, 34 insertions(+)
> >
> > diff --git a/include/net/eth.h b/include/net/eth.h
> > index 0671be6916..6aabbdd0d3 100644
> > --- a/include/net/eth.h
> > +++ b/include/net/eth.h
> > @@ -31,6 +31,7 @@
> >
> >  #define ETH_ALEN 6
> >  #define ETH_HLEN 14
> > +#define ETH_ZLEN 60     /* Min. octets in frame without FCS */
> >
> >  struct eth_header {
> >      uint8_t  h_dest[ETH_ALEN];   /* destination eth addr */
> > @@ -422,4 +423,20 @@ bool
> >  eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags,
> >                     size_t ip6hdr_off, eth_ip6_hdr_info *info);
> >
> > +/**
> > + * eth_pad_short_frame - pad a short frame to the minimum Ethernet frame length
> > + *
> > + * If the Ethernet frame size is shorter than 60 bytes, it will be padded to
> > + * 60 bytes at the address @padded_pkt.
> > + *
> > + * @padded_pkt: buffer address to hold the padded frame
> > + * @padded_buflen: buffer length of @padded_pkt. If the frame is padded, it is
> > + *                 written to ETH_ZLEN, otherwise it remains unchanged.
>
>  @padded_buflen: pointer holding length of @padded_pkt. If the frame is
>                  padded, the length will be updated to the padded one.

Will update in v5.

>
> Otherwise:
> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
>
> (since you describe frames, maybe s/pkt/frame/?)
>

By looking at the existing codes, 'pkt' seems to be a common variable
name representing ethernet frames, hence I will keep this as it is.

Regards,
Bin
diff mbox series

Patch

diff --git a/include/net/eth.h b/include/net/eth.h
index 0671be6916..6aabbdd0d3 100644
--- a/include/net/eth.h
+++ b/include/net/eth.h
@@ -31,6 +31,7 @@ 
 
 #define ETH_ALEN 6
 #define ETH_HLEN 14
+#define ETH_ZLEN 60     /* Min. octets in frame without FCS */
 
 struct eth_header {
     uint8_t  h_dest[ETH_ALEN];   /* destination eth addr */
@@ -422,4 +423,20 @@  bool
 eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags,
                    size_t ip6hdr_off, eth_ip6_hdr_info *info);
 
+/**
+ * eth_pad_short_frame - pad a short frame to the minimum Ethernet frame length
+ *
+ * If the Ethernet frame size is shorter than 60 bytes, it will be padded to
+ * 60 bytes at the address @padded_pkt.
+ *
+ * @padded_pkt: buffer address to hold the padded frame
+ * @padded_buflen: buffer length of @padded_pkt. If the frame is padded, it is
+ *                 written to ETH_ZLEN, otherwise it remains unchanged.
+ * @pkt: address to hold the original Ethernet frame
+ * @pkt_size: size of the original Ethernet frame
+ * @return true if the frame is padded, otherwise false
+ */
+bool eth_pad_short_frame(uint8_t *padded_pkt, size_t *padded_buflen,
+                         const void *pkt, size_t pkt_size);
+
 #endif
diff --git a/net/eth.c b/net/eth.c
index 1e0821c5f8..f913e4396f 100644
--- a/net/eth.c
+++ b/net/eth.c
@@ -548,3 +548,20 @@  bool eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags,
     info->l4proto = ext_hdr.ip6r_nxt;
     return true;
 }
+
+bool eth_pad_short_frame(uint8_t *padded_pkt, size_t *padded_buflen,
+                         const void *pkt, size_t pkt_size)
+{
+    assert(padded_buflen && *padded_buflen >= ETH_ZLEN);
+
+    if (pkt_size >= ETH_ZLEN) {
+        return false;
+    }
+
+    /* pad to minimum Ethernet frame length */
+    memcpy(padded_pkt, pkt, pkt_size);
+    memset(&padded_pkt[pkt_size], 0, ETH_ZLEN - pkt_size);
+    *padded_buflen = ETH_ZLEN;
+
+    return true;
+}