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

From: Alexander Lobakin
Date: Mon Dec 30 2019 - 09:33:25 EST


...to make them available for the upcoming GRO callbacks.

Signed-off-by: Alexander Lobakin <alobakin@xxxxxxxx>
---
net/dsa/tag_qca.c | 45 +++++++++++++++++++++++++++++----------------
1 file changed, 29 insertions(+), 16 deletions(-)

diff --git a/net/dsa/tag_qca.c b/net/dsa/tag_qca.c
index 8939abce36d7..bee2788e034d 100644
--- a/net/dsa/tag_qca.c
+++ b/net/dsa/tag_qca.c
@@ -28,6 +28,27 @@
#define QCA_HDR_XMIT_FROM_CPU BIT(7)
#define QCA_HDR_XMIT_DP(port) FIELD_PREP(GENMASK(6, 0), BIT(port))

+static inline bool qca_tag_sanity_check(const u8 *data)
+{
+ /* The QCA header is added by the switch between src addr and Ethertype
+ * At this point, skb->data points to ethertype so header should be
+ * right before
+ */
+ u16 hdr = ntohs(*(__be16 *)(data - 2));
+
+ return QCA_HDR_RECV_VERSION(hdr) == QCA_HDR_VERSION;
+}
+
+static inline int qca_tag_source_port(const u8 *data)
+{
+ return *(data - 1) & QCA_HDR_RECV_SOURCE_PORT_MASK;
+}
+
+static inline __be16 qca_tag_encap_proto(const u8 *data)
+{
+ return *(__be16 *)data;
+}
+
static struct sk_buff *qca_tag_xmit(struct sk_buff *skb, struct net_device *dev)
{
const struct dsa_port *dp = dsa_slave_to_port(dev);
@@ -55,34 +76,26 @@ static struct sk_buff *qca_tag_rcv(struct sk_buff *skb, struct net_device *dev,
struct packet_type *pt)
{
int port;
- __be16 *phdr, hdr;

if (unlikely(!pskb_may_pull(skb, QCA_HDR_LEN)))
return NULL;

- /* The QCA header is added by the switch between src addr and Ethertype
- * At this point, skb->data points to ethertype so header should be
- * right before
- */
- phdr = (__be16 *)(skb->data - 2);
- hdr = ntohs(*phdr);
-
/* Make sure the version is correct */
- if (unlikely(QCA_HDR_RECV_VERSION(hdr) != QCA_HDR_VERSION))
+ if (unlikely(!qca_tag_sanity_check(skb->data)))
return NULL;

- /* Remove QCA tag and recalculate checksum */
- skb_pull_rcsum(skb, QCA_HDR_LEN);
- memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - QCA_HDR_LEN,
- ETH_HLEN - QCA_HDR_LEN);
-
/* Get source port information */
- port = (hdr & QCA_HDR_RECV_SOURCE_PORT_MASK);
+ port = qca_tag_source_port(skb->data);

skb->dev = dsa_master_find_slave(dev, 0, port);
if (!skb->dev)
return NULL;

+ /* Remove QCA tag and recalculate checksum */
+ skb_pull_rcsum(skb, QCA_HDR_LEN);
+ memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - QCA_HDR_LEN,
+ ETH_HLEN - QCA_HDR_LEN);
+
return skb;
}

@@ -90,7 +103,7 @@ static void qca_tag_flow_dissect(const struct sk_buff *skb, __be16 *proto,
int *offset)
{
*offset = QCA_HDR_LEN;
- *proto = ((__be16 *)skb->data)[0];
+ *proto = qca_tag_encap_proto(skb->data);
}

static const struct dsa_device_ops qca_netdev_ops = {
--
2.24.1