Skip to contentSkip to conversation

mctp: Add MCTP-over-serial transport binding

PATCH Superseded Jeremy Kerr 2 participants 3 messages Nov 23, 2021

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jeremy Kerr

author maintainer
#0 lore raw

PATCH v2 1/1 mctp: Add MCTP-over-serial transport binding

This change adds a MCTP Serial transport binding, as defined by DMTF specificiation DSP0253 - "MCTP Serial Transport Binding". This is implemented as a new serial line discipline, and can be attached to arbitrary tty devices.

From the Kconfig description:

This driver provides an MCTP-over-serial interface, through a serial line-discipline, as defined by DMTF specification "DSP0253 - MCTP Serial Transport Binding". By attaching the ldisc to a serial device, we get a new net device to transport MCTP packets.

This allows communication with external MCTP endpoints which use serial as their transport. It can also be used as an easy way to provide MCTP connectivity between virtual machines, by forwarding data between simple virtual serial devices.

Say y here if you need to connect to MCTP endpoints over serial. To compile as a module, use m; the module will be called mctp-serial.

Once the N_MCTP line discipline is set [using ioctl(TCIOSETD)], we get a new netdev suitable for MCTP communication.

The 'mctp' utility[1] provides a simple wrapper for this ioctl, using 'link serial <device>':

  # mctp link serial /dev/ttyS0 &
  # mctp link
  dev lo index 1 address 0x00:00:00:00:00:00 net 1 mtu 65536 up
  dev mctpserial0 index 5 address 0x(no-addr) net 1 mtu 68 down

[1]: https://github.com/CodeConstruct/mctp

---
v2:
- improve tx tty write() error handling
- remove unnecessary RCU deref of disc_data
- expand Kconfig help: elaborate on MCTP serial spec, list module name
- add references to mctp util

---
 drivers/net/mctp/Kconfig       |  18 ++
 drivers/net/mctp/Makefile      |   1 +
 drivers/net/mctp/mctp-serial.c | 510 +++++++++++++++++++++++++++++++++
 include/uapi/linux/tty.h       |   1 +
 4 files changed, 530 insertions(+)
 create mode 100644 drivers/net/mctp/mctp-serial.c
drivers/net/mctp/Kconfig +18 -0
index d8f966cedc89..2929471395ae 100644
@@ -3,6 +3,24 @@ if MCTP
 
 menu "MCTP Device Drivers"
 
+config MCTP_SERIAL
+	tristate "MCTP serial transport"
+	depends on TTY
+	select CRC_CCITT
+	help
+	  This driver provides an MCTP-over-serial interface, through a
+	  serial line-discipline, as defined by DMTF specification "DSP0253 -
+	  MCTP Serial Transport Binding". By attaching the ldisc to a serial
+	  device, we get a new net device to transport MCTP packets.
+
+	  This allows communication with external MCTP endpoints which use
+	  serial as their transport. It can also be used as an easy way to
+	  provide MCTP connectivity between virtual machines, by forwarding
+	  data between simple virtual serial devices.
+
+	  Say y here if you need to connect to MCTP endpoints over serial. To
+	  compile as a module, use m; the module will be called mctp-serial.
+
 endmenu
 
 endif
drivers/net/mctp/Makefile +1 -0
index e69de29bb2d1..d32622613ce4 100644
@@ -0,0 +1 @@
+obj-$(CONFIG_MCTP_SERIAL) += mctp-serial.o
drivers/net/mctp/mctp-serial.c new file +510 -0

510 lines not on this page · raw message

include/uapi/linux/tty.h +1 -0
index 376cccf397be..a58deb3061eb 100644
@@ -38,5 +38,6 @@
 #define N_NCI		25	/* NFC NCI UART */
 #define N_SPEAKUP	26	/* Speakup communication with synths */
 #define N_NULL		27	/* Null ldisc used for error handling */
+#define N_MCTP		28	/* MCTP-over-serial */
 
 #endif /* _UAPI_LINUX_TTY_H */
Signature · 1 line
2.30.2
maintainer
#1 lore raw
Quoted 4 lines · Jeremy Kerr

This change adds a MCTP Serial transport binding, as defined by DMTF specificiation DSP0253 - "MCTP Serial Transport Binding". This is implemented as a new serial line discipline, and can be attached to arbitrary tty devices.

…

Quoted 3 lines
drivers/net/mctp/mctp-serial.c new file +0 -0
@@ -0,0 +1,510 @@

…

drivers/net/mctp/mctp-serial.c line 83

Quoted 10 lines
drivers/net/mctp/mctp-serial.c new file +10 -0
+static int next_chunk_len(struct mctp_serial *dev)
+{
+	int i;
+
+	/* either we have no bytes to send ... */
+	if (dev->txpos == dev->txlen)
+		return 0;
+
+	/* ... or the next byte to send is an escaped byte; requiring a
+	 * single-byte chunk...

This is not a correct multi-line comment. Or does net/ differ in this?

Quoted 20 lines
drivers/net/mctp/mctp-serial.c new file +20 -0
+	 */
+	if (needs_escape(dev->txbuf[dev->txpos]))
+		return 1;
+
+	/* ... or we have one or more bytes up to the next escape - this chunk
+	 * will be those non-escaped bytes, and does not include the escaped
+	 * byte.
+	 */
+	for (i = 1; i + dev->txpos + 1 < dev->txlen; i++) {
+		if (needs_escape(dev->txbuf[dev->txpos + i + 1]))
+			break;
+	}
+
+	return i;
+}
+
+static int write_chunk(struct mctp_serial *dev, unsigned char *buf, int len)
+{
+	return dev->tty->ops->write(dev->tty, buf, len);
+}

drivers/net/mctp/mctp-serial.c

Quoted 10 lines
drivers/net/mctp/mctp-serial.c new file +8 -0
 
 
+static netdev_tx_t mctp_serial_tx(struct sk_buff *skb, struct net_device *ndev)
+{
+	struct mctp_serial *dev = netdev_priv(ndev);
+	unsigned long flags;
+
+	WARN_ON(dev->txstate != STATE_IDLE);
+
+	if (skb->len > MCTP_SERIAL_MTU) {

Shouldn't you implement ndo_change_mtu to forbid larger frames instead?

Quoted 19 lines
drivers/net/mctp/mctp-serial.c new file +19 -0
+		dev->netdev->stats.tx_dropped++;
+		goto out;
+	}
+
+	spin_lock_irqsave(&dev->lock, flags);
+	netif_stop_queue(dev->netdev);
+	skb_copy_bits(skb, 0, dev->txbuf, skb->len);
+	dev->txpos = 0;
+	dev->txlen = skb->len;
+	dev->txstate = STATE_START;
+	spin_unlock_irqrestore(&dev->lock, flags);
+
+	set_bit(TTY_DO_WRITE_WAKEUP, &dev->tty->flags);
+	schedule_work(&dev->tx_work);
+
+out:
+	kfree_skb(skb);
+	return NETDEV_TX_OK;
+}

…

drivers/net/mctp/mctp-serial.c line 276

Quoted 12 lines
drivers/net/mctp/mctp-serial.c new file +12 -0
+static void mctp_serial_rx(struct mctp_serial *dev)
+{
+	struct mctp_skb_cb *cb;
+	struct sk_buff *skb;
+
+	if (dev->rxfcs != dev->rxfcs_rcvd) {
+		dev->netdev->stats.rx_dropped++;
+		dev->netdev->stats.rx_crc_errors++;
+		return;
+	}
+
+	skb = netdev_alloc_skb(dev->netdev, dev->rxlen);

Just thinking… Wouldn't it be easier to have dev->skb instead of dev->rxbuf and push data to it directly in all those mctp_serial_push*()?

Quoted 23 lines
drivers/net/mctp/mctp-serial.c new file +21 -0
+	if (!skb) {
+		dev->netdev->stats.rx_dropped++;
+		return;
+	}
+
+	skb->protocol = htons(ETH_P_MCTP);
+	skb_put_data(skb, dev->rxbuf, dev->rxlen);
+	skb_reset_network_header(skb);
+
+	cb = __mctp_cb(skb);
+	cb->halen = 0;
+
+	netif_rx_ni(skb);
+	dev->netdev->stats.rx_packets++;
+	dev->netdev->stats.rx_bytes += dev->rxlen;
+}
 
 
+static int mctp_serial_open(struct tty_struct *tty)
+{
+	struct mctp_serial *dev;
+	struct net_device *ndev;
+	char name[32];

drivers/net/mctp/mctp-serial.c

Quoted 10 lines
drivers/net/mctp/mctp-serial.c new file +10 -0
+	int idx, rc;
+
+	if (!capable(CAP_NET_ADMIN))
+		return -EPERM;
+
+	if (!tty->ops->write)
+		return -EOPNOTSUPP;
+
+	if (tty->disc_data)
+		return -EEXIST;

This should never happen™.

Quoted 36 lines
drivers/net/mctp/mctp-serial.c new file +36 -0
+
+	idx = ida_alloc(&mctp_serial_ida, GFP_KERNEL);
+	if (idx < 0)
+		return idx;
+
+	snprintf(name, sizeof(name), "mctpserial%d", idx);
+	ndev = alloc_netdev(sizeof(*dev), name, NET_NAME_ENUM,
+			    mctp_serial_setup);
+	if (!ndev) {
+		rc = -ENOMEM;
+		goto free_ida;
+	}
+
+	dev = netdev_priv(ndev);
+	dev->idx = idx;
+	dev->tty = tty;
+	dev->netdev = ndev;
+	dev->txstate = STATE_IDLE;
+	dev->rxstate = STATE_IDLE;
+	spin_lock_init(&dev->lock);
+	INIT_WORK(&dev->tx_work, mctp_serial_tx_work);
+
+	rc = register_netdev(ndev);
+	if (rc)
+		goto free_netdev;
+
+	tty->receive_room = 64 * 1024;
+	tty->disc_data = dev;
+
+	return 0;
+
+free_netdev:
+	free_netdev(ndev);
+
+free_ida:
+	ida_free(&mctp_serial_ida, idx);

drivers/net/mctp/mctp-serial.c line 482

Quoted 10 lines
drivers/net/mctp/mctp-serial.c new file +10 -0
+	return rc;
+}
+
+static void mctp_serial_close(struct tty_struct *tty)
+{
+	struct mctp_serial *dev = tty->disc_data;
+	int idx = dev->idx;
+
+	unregister_netdev(dev->netdev);
+	ida_free(&mctp_serial_ida, idx);

No tx_work flushing/cancelling?

Quoted 1 line

+}

regards,

Signature · 2 lines
js
suse labs
author maintainer
#2 lore raw

Hi Jiri,

Thanks for taking a look at the patch, some comments inline.

Quoted 5 lines
Quoted 2 lines

+       /* ... or the next byte to send is an escaped byte; requiring a +        * single-byte chunk...

This is not a correct multi-line comment. Or does net/ differ in this?

Yep, different for net/:

https://www.kernel.org/doc/html/latest/networking/netdev-FAQ.html#is-the-comment-style-convention-different-for-the-networking-content

(even though I guess we're technically drivers/net/ here...)

Quoted 11 lines

drivers/net/mctp/mctp-serial.c line 237

Quoted 8 lines
drivers/net/mctp/mctp-serial.c new file +8 -0
+static netdev_tx_t mctp_serial_tx(struct sk_buff *skb, struct net_device *ndev)
+{
+       struct mctp_serial *dev = netdev_priv(ndev);
+       unsigned long flags;
+
+       WARN_ON(dev->txstate != STATE_IDLE);
+
+       if (skb->len > MCTP_SERIAL_MTU) {

Shouldn't you implement ndo_change_mtu to forbid larger frames instead?

Even better, I can set the dev min and max MTUs in _setup, so we'll get the netlink extacks reported too. However, I'd still like to leave this is in to ensure there's no way to overrun the tx buffer.

Quoted 8 lines
Quoted 8 lines
drivers/net/mctp/mctp-serial.c new file +8 -0
+static void mctp_serial_rx(struct mctp_serial *dev)
+{
+       struct mctp_skb_cb *cb;
+       struct sk_buff *skb;
+
+       if (dev->rxfcs != dev->rxfcs_rcvd) {
+               dev->netdev->stats.rx_dropped++;
+               dev->netdev->stats.rx_crc_errors++;
Quoted 8 lines
Quoted 4 lines

+               return;
+       }
+
+       skb = netdev_alloc_skb(dev->netdev, dev->rxlen);

Just thinking… Wouldn't it be easier to have dev->skb instead of dev->rxbuf and push data to it directly in all those mctp_serial_push*()?

There's not a huge benefit to doing so - we still need the RX state machine, so will still need the mctp_serial_push_* code. Having the rxbuf means we will always have a buffer available for pushed data, rather than extra conditions in the rx state machine for if we've failed to allocate the next skb.

Doing this as a final stage in the rx path means we can keep the state machine a little simpler, and perform the drop in a single location.

The downside to the current approach is the extra copy in skb_put_data() though, but we're not really dealing with high-bandwidth links here.

I'm happy to change if there's particular preference either way though!

Quoted 9 lines
Quoted 9 lines
drivers/net/mctp/mctp-serial.c new file +9 -0
+static int mctp_serial_open(struct tty_struct *tty)
+{
+       struct mctp_serial *dev;
+       struct net_device *ndev;
+       char name[32];
+       int idx, rc;
+
+       if (!capable(CAP_NET_ADMIN))
+               return -EPERM;
Quoted 8 lines

drivers/net/mctp/mctp-serial.c line 436

Quoted 6 lines
drivers/net/mctp/mctp-serial.c new file +6 -0
+
+       if (!tty->ops->write)
+               return -EOPNOTSUPP;
+
+       if (tty->disc_data)
+               return -EEXIST;

This should never happen™.

OK, excellent, I'll remove.

Quoted 9 lines

drivers/net/mctp/mctp-serial.c line 482

Quoted 7 lines
drivers/net/mctp/mctp-serial.c new file +7 -0
+static void mctp_serial_close(struct tty_struct *tty)
+{
+       struct mctp_serial *dev = tty->disc_data;
+       int idx = dev->idx;
+
+       unregister_netdev(dev->netdev);
+       ida_free(&mctp_serial_ida, idx);

No tx_work flushing/cancelling?

Ooh, yeah. I'll fix in v4.

Cheers,

Jeremy