Skip to content

Commit

Permalink
etherdev: Fix sparse error, make test usable by other functions
Browse files Browse the repository at this point in the history
This change does two things.  First it fixes a sparse error for the fact
that the __be16 degrades to an integer.  Since that is actually what I am
kind of doing I am simply working around that by forcing both sides of the
comparison to u16.

Also I realized on some compilers I was generating another instruction for
big endian systems such as PowerPC since it was masking the value before
doing the comparison.  So to resolve that I have simply pulled the mask out
and wrapped it in an #ifndef __BIG_ENDIAN.

Lastly I pulled this all out into its own function.  I notices there are
similar checks in a number of other places so this function can be reused
there to help reduce overhead in these paths as well.

Signed-off-by: Alexander Duyck <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
Alexander Duyck authored and davem330 committed May 5, 2015
1 parent 784b58a commit 2c7a88c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 18 additions & 0 deletions include/linux/etherdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,24 @@ static inline bool is_valid_ether_addr(const u8 *addr)
return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
}

/**
* eth_proto_is_802_3 - Determine if a given Ethertype/length is a protocol
* @proto: Ethertype/length value to be tested
*
* Check that the value from the Ethertype/length field is a valid Ethertype.
*
* Return true if the valid is an 802.3 supported Ethertype.
*/
static inline bool eth_proto_is_802_3(__be16 proto)
{
#ifndef __BIG_ENDIAN
/* if CPU is little endian mask off bits representing LSB */
proto &= htons(0xFF00);
#endif
/* cast both to u16 and compare since LSB can be ignored */
return (__force u16)proto >= (__force u16)htons(ETH_P_802_3_MIN);
}

/**
* eth_random_addr - Generate software assigned random Ethernet address
* @addr: Pointer to a six-byte array containing the Ethernet address
Expand Down
2 changes: 1 addition & 1 deletion net/ethernet/eth.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev)
if (unlikely(netdev_uses_dsa(dev)))
return htons(ETH_P_XDSA);

if (likely((eth->h_proto & htons(0xFF00)) >= htons(ETH_P_802_3_MIN)))
if (likely(eth_proto_is_802_3(eth->h_proto)))
return eth->h_proto;

/*
Expand Down

0 comments on commit 2c7a88c

Please sign in to comment.