[PATCH RFC net-next 12/19] net: dsa: tag_lan9303: split out common tag accessors

From: Alexander Lobakin
Date: Mon Dec 30 2019 - 09:32:20 EST


...as they will be needed in the upcoming GRO callbacks.

Signed-off-by: Alexander Lobakin <alobakin@xxxxxxxx>
---
net/dsa/tag_lan9303.c | 46 +++++++++++++++++++++++++++++--------------
1 file changed, 31 insertions(+), 15 deletions(-)

diff --git a/net/dsa/tag_lan9303.c b/net/dsa/tag_lan9303.c
index d328a44381a9..ba03502986a4 100644
--- a/net/dsa/tag_lan9303.c
+++ b/net/dsa/tag_lan9303.c
@@ -38,6 +38,32 @@
# define LAN9303_TAG_RX_TRAPPED_TO_CPU (LAN9303_TAG_RX_IGMP | \
LAN9303_TAG_RX_STP)

+static inline bool lan9303_sanity_check(const u8 *data)
+{
+ /* '->data' points into the middle of our special VLAN tag information:
+ *
+ * ~ MAC src | 0x81 | 0x00 | 0xyy | 0xzz | ether type
+ * ^
+ * ->data
+ */
+ return *(__be16 *)(data - 2) == htons(ETH_P_8021Q);
+}
+
+static inline bool lan9303_trapped_to_cpu(const u8 *data)
+{
+ return *(data + 1) & LAN9303_TAG_RX_TRAPPED_TO_CPU;
+}
+
+static inline int lan9303_source_port(const u8 *data)
+{
+ return *(data + 1) & GENMASK(1, 0);
+}
+
+static inline __be16 lan9303_encap_proto(const u8 *data)
+{
+ return *(__be16 *)(data + 2);
+}
+
/* Decide whether to transmit using ALR lookup, or transmit directly to
* port using tag. ALR learning is performed only when using ALR lookup.
* If the two external ports are bridged and the frame is unicast,
@@ -85,8 +111,6 @@ static struct sk_buff *lan9303_xmit(struct sk_buff *skb, struct net_device *dev)
static struct sk_buff *lan9303_rcv(struct sk_buff *skb, struct net_device *dev,
struct packet_type *pt)
{
- u16 *lan9303_tag;
- u16 lan9303_tag1;
unsigned int source_port;

if (unlikely(!pskb_may_pull(skb, LAN9303_TAG_LEN))) {
@@ -95,21 +119,12 @@ static struct sk_buff *lan9303_rcv(struct sk_buff *skb, struct net_device *dev,
return NULL;
}

- /* '->data' points into the middle of our special VLAN tag information:
- *
- * ~ MAC src | 0x81 | 0x00 | 0xyy | 0xzz | ether type
- * ^
- * ->data
- */
- lan9303_tag = (u16 *)(skb->data - 2);
-
- if (lan9303_tag[0] != htons(ETH_P_8021Q)) {
+ if (!lan9303_sanity_check(skb->data)) {
dev_warn_ratelimited(&dev->dev, "Dropping packet due to invalid VLAN marker\n");
return NULL;
}

- lan9303_tag1 = ntohs(lan9303_tag[1]);
- source_port = lan9303_tag1 & 0x3;
+ source_port = lan9303_source_port(skb->data);

skb->dev = dsa_master_find_slave(dev, 0, source_port);
if (!skb->dev) {
@@ -117,13 +132,14 @@ static struct sk_buff *lan9303_rcv(struct sk_buff *skb, struct net_device *dev,
return NULL;
}

+ skb->offload_fwd_mark = !lan9303_trapped_to_cpu(skb->data);
+
/* remove the special VLAN tag between the MAC addresses
* and the current ethertype field.
*/
skb_pull_rcsum(skb, 2 + 2);
memmove(skb->data - ETH_HLEN, skb->data - (ETH_HLEN + LAN9303_TAG_LEN),
2 * ETH_ALEN);
- skb->offload_fwd_mark = !(lan9303_tag1 & LAN9303_TAG_RX_TRAPPED_TO_CPU);

return skb;
}
@@ -132,7 +148,7 @@ static void lan9303_flow_dissect(const struct sk_buff *skb, __be16 *proto,
int *offset)
{
*offset = LAN9303_TAG_LEN;
- *proto = *(__be16 *)(skb->data + 2);
+ *proto = lan9303_encap_proto(skb->data);
}

static const struct dsa_device_ops lan9303_netdev_ops = {
--
2.24.1