lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250512012748.79749-7-damien.riegel@silabs.com>
Date: Sun, 11 May 2025 21:27:39 -0400
From: Damien Riégel <damien.riegel@...abs.com>
To: Andrew Lunn <andrew+netdev@...n.ch>,
        "David S . Miller" <davem@...emloft.net>,
        Eric Dumazet <edumazet@...gle.com>, Jakub Kicinski <kuba@...nel.org>,
        Paolo Abeni <pabeni@...hat.com>, Rob Herring <robh@...nel.org>,
        Krzysztof Kozlowski <krzk+dt@...nel.org>,
        Conor Dooley <conor+dt@...nel.org>,
        Silicon Labs Kernel Team <linux-devel@...abs.com>,
        netdev@...r.kernel.org, devicetree@...r.kernel.org,
        linux-kernel@...r.kernel.org
Subject: [RFC net-next 06/15] net: cpc: implement basic receive path

Implement a very basic receive path. When a new frame is available,
interfaces are expected to call cpc_interface_receive_frame(). This
frame will be handled in a high-priority workqueue and dispatched to the
endpoint it targets, if available.

Endpoints should set an RX callback with cpc_endpoint_set_ops() in order
to be notified when a new frame arrives. This callback should be short,
long operations should be dispatched or the main reception task will be
blocked until that processing finishes.

Signed-off-by: Damien Riégel <damien.riegel@...abs.com>
---
 drivers/net/cpc/cpc.h       |  9 ++++++
 drivers/net/cpc/endpoint.c  | 11 +++++++
 drivers/net/cpc/interface.c | 59 +++++++++++++++++++++++++++++++++++++
 drivers/net/cpc/interface.h |  8 +++++
 drivers/net/cpc/protocol.c  | 15 ++++++++++
 drivers/net/cpc/protocol.h  |  2 ++
 6 files changed, 104 insertions(+)

diff --git a/drivers/net/cpc/cpc.h b/drivers/net/cpc/cpc.h
index 2f54e5b660e..dc05b36b6e6 100644
--- a/drivers/net/cpc/cpc.h
+++ b/drivers/net/cpc/cpc.h
@@ -18,6 +18,13 @@ struct cpc_endpoint;
 
 extern const struct bus_type cpc_bus;
 
+/** struct cpc_endpoint_ops - Endpoint's callbacks.
+ * @rx: Data availability is provided with a skb owned by the driver.
+ */
+struct cpc_endpoint_ops {
+	void (*rx)(struct cpc_endpoint *ep, struct sk_buff *skb);
+};
+
 /**
  * struct cpc_endpoint - Representation of CPC endpointl
  * @dev: Driver model representation of the device.
@@ -39,6 +46,7 @@ struct cpc_endpoint {
 
 	struct cpc_interface *intf;
 	struct list_head list_node;
+	struct cpc_endpoint_ops *ops;
 
 	struct sk_buff_head holding_queue;
 };
@@ -50,6 +58,7 @@ struct cpc_endpoint *cpc_endpoint_new(struct cpc_interface *intf, u8 id, const c
 void cpc_endpoint_unregister(struct cpc_endpoint *ep);
 
 int cpc_endpoint_write(struct cpc_endpoint *ep, struct sk_buff *skb);
+void cpc_endpoint_set_ops(struct cpc_endpoint *ep, struct cpc_endpoint_ops *ops);
 
 /**
  * cpc_endpoint_from_dev() - Upcast from a device pointer.
diff --git a/drivers/net/cpc/endpoint.c b/drivers/net/cpc/endpoint.c
index 4e98955be30..51007ba5bcc 100644
--- a/drivers/net/cpc/endpoint.c
+++ b/drivers/net/cpc/endpoint.c
@@ -172,6 +172,17 @@ void cpc_endpoint_unregister(struct cpc_endpoint *ep)
 	put_device(&ep->dev);
 }
 
+/**
+ * cpc_endpoint_set_ops() - Set callbacks for this endpoint.
+ * @ep: Endpoint
+ * @ops: New callbacks to set. If already set, override pre-existing value.
+ */
+void cpc_endpoint_set_ops(struct cpc_endpoint *ep, struct cpc_endpoint_ops *ops)
+{
+	if (ep)
+		ep->ops = ops;
+}
+
 /**
  * cpc_endpoint_write - Write a DATA frame.
  * @ep: Endpoint handle.
diff --git a/drivers/net/cpc/interface.c b/drivers/net/cpc/interface.c
index 1dd87deed59..edc6b387e50 100644
--- a/drivers/net/cpc/interface.c
+++ b/drivers/net/cpc/interface.c
@@ -6,12 +6,44 @@
 #include <linux/module.h>
 
 #include "cpc.h"
+#include "header.h"
 #include "interface.h"
+#include "protocol.h"
 
 #define to_cpc_interface(d) container_of(d, struct cpc_interface, dev)
 
 static DEFINE_IDA(cpc_ida);
 
+static void cpc_interface_rx_work(struct work_struct *work)
+{
+	struct cpc_interface *intf = container_of(work, struct cpc_interface, rx_work);
+	enum cpc_frame_type type;
+	struct cpc_endpoint *ep;
+	struct sk_buff *skb;
+	u8 ep_id;
+
+	while ((skb = skb_dequeue(&intf->rx_queue))) {
+		cpc_header_get_type(skb->data, &type);
+		ep_id = cpc_header_get_ep_id(skb->data);
+
+		ep = cpc_interface_get_endpoint(intf, ep_id);
+		if (!ep) {
+			kfree_skb(skb);
+			continue;
+		}
+
+		switch (type) {
+		case CPC_FRAME_TYPE_DATA:
+			cpc_protocol_on_data(ep, skb);
+			break;
+		default:
+			kfree_skb(skb);
+		}
+
+		cpc_endpoint_put(ep);
+	}
+}
+
 /**
  * cpc_intf_release() - Actual release of interface.
  * @dev: Device embedded in struct cpc_interface
@@ -23,6 +55,10 @@ static void cpc_intf_release(struct device *dev)
 {
 	struct cpc_interface *intf = to_cpc_interface(dev);
 
+	flush_work(&intf->rx_work);
+
+	destroy_workqueue(intf->workq);
+
 	ida_free(&cpc_ida, intf->index);
 	kfree(intf);
 }
@@ -54,10 +90,20 @@ struct cpc_interface *cpc_interface_alloc(struct device *parent,
 		return NULL;
 	}
 
+	intf->workq = alloc_workqueue(KBUILD_MODNAME "_wq", WQ_HIGHPRI, 0);
+	if (!intf->workq) {
+		ida_free(&cpc_ida, intf->index);
+		kfree(intf);
+
+		return ERR_PTR(-ENOMEM);
+	}
+
 	mutex_init(&intf->add_lock);
 	mutex_init(&intf->lock);
 	INIT_LIST_HEAD(&intf->eps);
 
+	INIT_WORK(&intf->rx_work, cpc_interface_rx_work);
+	skb_queue_head_init(&intf->rx_queue);
 	skb_queue_head_init(&intf->tx_queue);
 
 	intf->ops = ops;
@@ -157,6 +203,19 @@ struct cpc_endpoint *cpc_interface_get_endpoint(struct cpc_interface *intf, u8 e
 	return ep;
 }
 
+/**
+ * cpc_interface_receive_frame - queue a received frame for processing
+ * @intf: pointer to the CPC device
+ * @skb: received frame
+ *
+ * Context: This queues the sk_buff in a list and schedule the work task to process the list.
+ */
+void cpc_interface_receive_frame(struct cpc_interface *intf, struct sk_buff *skb)
+{
+	skb_queue_tail(&intf->rx_queue, skb);
+	queue_work(intf->workq, &intf->rx_work);
+}
+
 /**
  * cpc_interface_send_frame() - Queue a socket buffer for transmission.
  * @intf: Interface to send SKB over.
diff --git a/drivers/net/cpc/interface.h b/drivers/net/cpc/interface.h
index 1b501b1f6dc..a45227a50a7 100644
--- a/drivers/net/cpc/interface.h
+++ b/drivers/net/cpc/interface.h
@@ -22,6 +22,9 @@ struct cpc_interface_ops;
  * @index: Device index.
  * @lock: Protect access to endpoint list.
  * @eps: List of endpoints managed by this device.
+ * @workq: Interface-specific work queue.
+ * @rx_work: work struct for processing received frames
+ * @rx_queue: list of sk_buff that were received
  * @tx_queue: Transmit queue to be consumed by the interface.
  */
 struct cpc_interface {
@@ -37,6 +40,10 @@ struct cpc_interface {
 	struct mutex lock;	/* Protect eps from concurrent access. */
 	struct list_head eps;
 
+	struct workqueue_struct *workq;
+	struct work_struct rx_work;
+	struct sk_buff_head rx_queue;
+
 	struct sk_buff_head tx_queue;
 };
 
@@ -61,6 +68,7 @@ void cpc_interface_unregister(struct cpc_interface *intf);
 
 struct cpc_endpoint *cpc_interface_get_endpoint(struct cpc_interface *intf, u8 ep_id);
 
+void cpc_interface_receive_frame(struct cpc_interface *intf, struct sk_buff *skb);
 void cpc_interface_send_frame(struct cpc_interface *intf, struct sk_buff *skb);
 struct sk_buff *cpc_interface_dequeue(struct cpc_interface *intf);
 bool cpc_interface_tx_queue_empty(struct cpc_interface *intf);
diff --git a/drivers/net/cpc/protocol.c b/drivers/net/cpc/protocol.c
index 692d3e07939..91335160981 100644
--- a/drivers/net/cpc/protocol.c
+++ b/drivers/net/cpc/protocol.c
@@ -39,6 +39,21 @@ static void __cpc_protocol_process_pending_tx_frames(struct cpc_endpoint *ep)
 	}
 }
 
+void cpc_protocol_on_data(struct cpc_endpoint *ep, struct sk_buff *skb)
+{
+	if (skb->len > CPC_HEADER_SIZE) {
+		/* Strip header. */
+		skb_pull(skb, CPC_HEADER_SIZE);
+
+		if (ep->ops && ep->ops->rx)
+			ep->ops->rx(ep, skb);
+		else
+			kfree_skb(skb);
+	} else {
+		kfree_skb(skb);
+	}
+}
+
 /**
  * __cpc_protocol_write() - Write a frame.
  * @ep: Endpoint handle.
diff --git a/drivers/net/cpc/protocol.h b/drivers/net/cpc/protocol.h
index b51f0191be4..9a028e0e94b 100644
--- a/drivers/net/cpc/protocol.h
+++ b/drivers/net/cpc/protocol.h
@@ -16,4 +16,6 @@ struct cpc_header;
 
 int __cpc_protocol_write(struct cpc_endpoint *ep, struct cpc_header *hdr, struct sk_buff *skb);
 
+void cpc_protocol_on_data(struct cpc_endpoint *ep, struct sk_buff *skb);
+
 #endif
-- 
2.49.0


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ