2022-07-16 17:02:02

by Dario Binacchi

[permalink] [raw]
Subject: [RFC PATCH 0/5] can: slcan: extend supported features (step 2)

With this series I try to finish the task, started with the series [1],
of completely removing the dependency of the slcan driver from the
userspace slcand/slcan_attach applications.

The series, however, still lacks a patch for sending the bitrate setting
command to the adapter:

slcan_attach -b <btr> <dev>

Without at least this patch the task cannot be considered truly completed.

The idea I got is that this can only happen through the ethtool API.
Among the various operations made available by this interface I would
have used the set_regs (but only the get_regs has been developed), or,
the set_eeprom, even if the setting would not be stored in an eeprom.
IMHO it would take a set_regs operation with a `struct ethtool_wregs'
parameter similar to `struct ethtool_eeprom' without the magic field:

struct ethtool_wregs {
__u32 cmd;
__u32 offset;
__u32 len;
__u8 data[0];
};

But I am not the expert and if there was an alternative solution already
usable, it would be welcome.

The series also contains patches that remove the legacy stuff (slcan_devs,
SLCAN_MAGIC, ...) and do some module cleanup.

The series has been created on top of the patches:

can: slcan: convert comments to network style comments
can: slcan: slcan_init() convert printk(LEVEL ...) to pr_level()
can: slcan: fix whitespace issues
can: slcan: convert comparison to NULL into !val
can: slcan: clean up if/else
can: slcan: use scnprintf() as a hardening measure
can: slcan: do not report txerr and rxerr during bus-off
can: slcan: do not sleep with a spin lock held

applied to linux-next.

[1] https://lore.kernel.org/all/[email protected]/


Dario Binacchi (5):
can: slcan: remove useless header inclusions
can: slcan: remove legacy infrastructure
can: slcan: change every `slc' occurrence in `slcan'
can: slcan: use the generic can_change_mtu()
can: slcan: send the listen-only command to the adapter

drivers/net/can/slcan/slcan-core.c | 465 +++++++++--------------------
1 file changed, 134 insertions(+), 331 deletions(-)

--
2.32.0


2022-07-16 17:02:15

by Dario Binacchi

[permalink] [raw]
Subject: [RFC PATCH 1/5] can: slcan: remove useless header inclusions

Include only the necessary headers.

Signed-off-by: Dario Binacchi <[email protected]>
---

drivers/net/can/slcan/slcan-core.c | 18 ------------------
1 file changed, 18 deletions(-)

diff --git a/drivers/net/can/slcan/slcan-core.c b/drivers/net/can/slcan/slcan-core.c
index dfd1baba4130..3394d059fc29 100644
--- a/drivers/net/can/slcan/slcan-core.c
+++ b/drivers/net/can/slcan/slcan-core.c
@@ -36,26 +36,8 @@
*/

#include <linux/module.h>
-#include <linux/moduleparam.h>
-
-#include <linux/uaccess.h>
-#include <linux/bitops.h>
-#include <linux/string.h>
#include <linux/tty.h>
-#include <linux/errno.h>
-#include <linux/netdevice.h>
-#include <linux/skbuff.h>
-#include <linux/rtnetlink.h>
-#include <linux/if_arp.h>
-#include <linux/if_ether.h>
-#include <linux/sched.h>
-#include <linux/delay.h>
-#include <linux/init.h>
-#include <linux/kernel.h>
-#include <linux/workqueue.h>
-#include <linux/can.h>
#include <linux/can/dev.h>
-#include <linux/can/skb.h>

#include "slcan.h"

--
2.32.0

2022-07-16 17:03:45

by Dario Binacchi

[permalink] [raw]
Subject: [RFC PATCH 4/5] can: slcan: use the generic can_change_mtu()

It is useless to define a custom function that does nothing but always
return the same error code. Better to use the generic can_change_mtu()
function.

Signed-off-by: Dario Binacchi <[email protected]>
---

drivers/net/can/slcan/slcan-core.c | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/drivers/net/can/slcan/slcan-core.c b/drivers/net/can/slcan/slcan-core.c
index 093d232c13dd..7a1540507ecd 100644
--- a/drivers/net/can/slcan/slcan-core.c
+++ b/drivers/net/can/slcan/slcan-core.c
@@ -727,16 +727,11 @@ static int slcan_netdev_open(struct net_device *dev)
return err;
}

-static int slcan_netdev_change_mtu(struct net_device *dev, int new_mtu)
-{
- return -EINVAL;
-}
-
static const struct net_device_ops slcan_netdev_ops = {
.ndo_open = slcan_netdev_open,
.ndo_stop = slcan_netdev_close,
.ndo_start_xmit = slcan_netdev_xmit,
- .ndo_change_mtu = slcan_netdev_change_mtu,
+ .ndo_change_mtu = can_change_mtu,
};

/******************************************
--
2.32.0

2022-07-16 17:14:25

by Dario Binacchi

[permalink] [raw]
Subject: [RFC PATCH 3/5] can: slcan: change every `slc' occurrence in `slcan'

In the driver there are parts of code where the prefix `slc' is used and
others where the prefix `slcan' is used instead. The patch replaces
every occurrence of `slc' with `slcan', except for the netdev functions
where, to avoid compilation conflicts, it was necessary to replace `slc'
with `slcan_netdev'.

The patch does not make any functional changes.

Signed-off-by: Dario Binacchi <[email protected]>
---

drivers/net/can/slcan/slcan-core.c | 109 +++++++++++++++--------------
1 file changed, 56 insertions(+), 53 deletions(-)

diff --git a/drivers/net/can/slcan/slcan-core.c b/drivers/net/can/slcan/slcan-core.c
index 92cab093453d..093d232c13dd 100644
--- a/drivers/net/can/slcan/slcan-core.c
+++ b/drivers/net/can/slcan/slcan-core.c
@@ -50,16 +50,17 @@ MODULE_LICENSE("GPL");
MODULE_AUTHOR("Oliver Hartkopp <[email protected]>");

/* maximum rx buffer len: extended CAN frame with timestamp */
-#define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r") + 1)
-
-#define SLC_CMD_LEN 1
-#define SLC_SFF_ID_LEN 3
-#define SLC_EFF_ID_LEN 8
-#define SLC_STATE_LEN 1
-#define SLC_STATE_BE_RXCNT_LEN 3
-#define SLC_STATE_BE_TXCNT_LEN 3
-#define SLC_STATE_FRAME_LEN (1 + SLC_CMD_LEN + SLC_STATE_BE_RXCNT_LEN + \
- SLC_STATE_BE_TXCNT_LEN)
+#define SLCAN_MTU (sizeof("T1111222281122334455667788EA5F\r") + 1)
+
+#define SLCAN_CMD_LEN 1
+#define SLCAN_SFF_ID_LEN 3
+#define SLCAN_EFF_ID_LEN 8
+#define SLCAN_STATE_LEN 1
+#define SLCAN_STATE_BE_RXCNT_LEN 3
+#define SLCAN_STATE_BE_TXCNT_LEN 3
+#define SLCAN_STATE_FRAME_LEN (1 + SLCAN_CMD_LEN + \
+ SLCAN_STATE_BE_RXCNT_LEN + \
+ SLCAN_STATE_BE_TXCNT_LEN)
struct slcan {
struct can_priv can;

@@ -70,9 +71,9 @@ struct slcan {
struct work_struct tx_work; /* Flushes transmit buffer */

/* These are pointers to the malloc()ed frame buffers. */
- unsigned char rbuff[SLC_MTU]; /* receiver buffer */
+ unsigned char rbuff[SLCAN_MTU]; /* receiver buffer */
int rcount; /* received chars counter */
- unsigned char xbuff[SLC_MTU]; /* transmitter buffer */
+ unsigned char xbuff[SLCAN_MTU]; /* transmitter buffer*/
unsigned char *xhead; /* pointer to next XMIT byte */
int xleft; /* bytes left in XMIT queue */

@@ -151,7 +152,7 @@ int slcan_enable_err_rst_on_open(struct net_device *ndev, bool on)
*************************************************************************/

/* Send one completely decapsulated can_frame to the network layer */
-static void slc_bump_frame(struct slcan *sl)
+static void slcan_bump_frame(struct slcan *sl)
{
struct sk_buff *skb;
struct can_frame *cf;
@@ -171,10 +172,10 @@ static void slc_bump_frame(struct slcan *sl)
fallthrough;
case 't':
/* store dlc ASCII value and terminate SFF CAN ID string */
- cf->len = sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN];
- sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN] = 0;
+ cf->len = sl->rbuff[SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN];
+ sl->rbuff[SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN] = 0;
/* point to payload data behind the dlc */
- cmd += SLC_CMD_LEN + SLC_SFF_ID_LEN + 1;
+ cmd += SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN + 1;
break;
case 'R':
cf->can_id = CAN_RTR_FLAG;
@@ -182,16 +183,16 @@ static void slc_bump_frame(struct slcan *sl)
case 'T':
cf->can_id |= CAN_EFF_FLAG;
/* store dlc ASCII value and terminate EFF CAN ID string */
- cf->len = sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN];
- sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN] = 0;
+ cf->len = sl->rbuff[SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN];
+ sl->rbuff[SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN] = 0;
/* point to payload data behind the dlc */
- cmd += SLC_CMD_LEN + SLC_EFF_ID_LEN + 1;
+ cmd += SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN + 1;
break;
default:
goto decode_failed;
}

- if (kstrtou32(sl->rbuff + SLC_CMD_LEN, 16, &tmpid))
+ if (kstrtou32(sl->rbuff + SLCAN_CMD_LEN, 16, &tmpid))
goto decode_failed;

cf->can_id |= tmpid;
@@ -238,7 +239,7 @@ static void slc_bump_frame(struct slcan *sl)
* sb256256 : state bus-off: rx counter 256, tx counter 256
* sa057033 : state active, rx counter 57, tx counter 33
*/
-static void slc_bump_state(struct slcan *sl)
+static void slcan_bump_state(struct slcan *sl)
{
struct net_device *dev = sl->dev;
struct sk_buff *skb;
@@ -264,16 +265,16 @@ static void slc_bump_state(struct slcan *sl)
return;
}

- if (state == sl->can.state || sl->rcount < SLC_STATE_FRAME_LEN)
+ if (state == sl->can.state || sl->rcount < SLCAN_STATE_FRAME_LEN)
return;

- cmd += SLC_STATE_BE_RXCNT_LEN + SLC_CMD_LEN + 1;
- cmd[SLC_STATE_BE_TXCNT_LEN] = 0;
+ cmd += SLCAN_STATE_BE_RXCNT_LEN + SLCAN_CMD_LEN + 1;
+ cmd[SLCAN_STATE_BE_TXCNT_LEN] = 0;
if (kstrtou32(cmd, 10, &txerr))
return;

*cmd = 0;
- cmd -= SLC_STATE_BE_RXCNT_LEN;
+ cmd -= SLCAN_STATE_BE_RXCNT_LEN;
if (kstrtou32(cmd, 10, &rxerr))
return;

@@ -301,7 +302,7 @@ static void slc_bump_state(struct slcan *sl)
* e1a : len 1, errors: ACK error
* e3bcO: len 3, errors: Bit0 error, CRC error, Tx overrun error
*/
-static void slc_bump_err(struct slcan *sl)
+static void slcan_bump_err(struct slcan *sl)
{
struct net_device *dev = sl->dev;
struct sk_buff *skb;
@@ -317,7 +318,7 @@ static void slc_bump_err(struct slcan *sl)
else
return;

- if ((len + SLC_CMD_LEN + 1) > sl->rcount)
+ if ((len + SLCAN_CMD_LEN + 1) > sl->rcount)
return;

skb = alloc_can_err_skb(dev, &cf);
@@ -325,7 +326,7 @@ static void slc_bump_err(struct slcan *sl)
if (skb)
cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;

- cmd += SLC_CMD_LEN + 1;
+ cmd += SLCAN_CMD_LEN + 1;
for (i = 0; i < len; i++, cmd++) {
switch (*cmd) {
case 'a':
@@ -414,7 +415,7 @@ static void slc_bump_err(struct slcan *sl)
netif_rx(skb);
}

-static void slc_bump(struct slcan *sl)
+static void slcan_bump(struct slcan *sl)
{
switch (sl->rbuff[0]) {
case 'r':
@@ -424,11 +425,11 @@ static void slc_bump(struct slcan *sl)
case 'R':
fallthrough;
case 'T':
- return slc_bump_frame(sl);
+ return slcan_bump_frame(sl);
case 'e':
- return slc_bump_err(sl);
+ return slcan_bump_err(sl);
case 's':
- return slc_bump_state(sl);
+ return slcan_bump_state(sl);
default:
return;
}
@@ -440,12 +441,12 @@ static void slcan_unesc(struct slcan *sl, unsigned char s)
if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
sl->rcount > 4)
- slc_bump(sl);
+ slcan_bump(sl);

sl->rcount = 0;
} else {
if (!test_bit(SLF_ERROR, &sl->flags)) {
- if (sl->rcount < SLC_MTU) {
+ if (sl->rcount < SLCAN_MTU) {
sl->rbuff[sl->rcount++] = s;
return;
}
@@ -461,7 +462,7 @@ static void slcan_unesc(struct slcan *sl, unsigned char s)
*************************************************************************/

/* Encapsulate one can_frame and stuff into a TTY queue. */
-static void slc_encaps(struct slcan *sl, struct can_frame *cf)
+static void slcan_encaps(struct slcan *sl, struct can_frame *cf)
{
int actual, i;
unsigned char *pos;
@@ -478,11 +479,11 @@ static void slc_encaps(struct slcan *sl, struct can_frame *cf)
/* determine number of chars for the CAN-identifier */
if (cf->can_id & CAN_EFF_FLAG) {
id &= CAN_EFF_MASK;
- endpos = pos + SLC_EFF_ID_LEN;
+ endpos = pos + SLCAN_EFF_ID_LEN;
} else {
*pos |= 0x20; /* convert R/T to lower case for SFF */
id &= CAN_SFF_MASK;
- endpos = pos + SLC_SFF_ID_LEN;
+ endpos = pos + SLCAN_SFF_ID_LEN;
}

/* build 3 (SFF) or 8 (EFF) digit CAN identifier */
@@ -492,7 +493,8 @@ static void slc_encaps(struct slcan *sl, struct can_frame *cf)
id >>= 4;
}

- pos += (cf->can_id & CAN_EFF_FLAG) ? SLC_EFF_ID_LEN : SLC_SFF_ID_LEN;
+ pos += (cf->can_id & CAN_EFF_FLAG) ?
+ SLCAN_EFF_ID_LEN : SLCAN_SFF_ID_LEN;

*pos++ = cf->len + '0';

@@ -570,7 +572,8 @@ static void slcan_write_wakeup(struct tty_struct *tty)
}

/* Send a can_frame to a TTY queue. */
-static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev)
+static netdev_tx_t slcan_netdev_xmit(struct sk_buff *skb,
+ struct net_device *dev)
{
struct slcan *sl = netdev_priv(dev);

@@ -589,7 +592,7 @@ static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev)
}

netif_stop_queue(sl->dev);
- slc_encaps(sl, (struct can_frame *)skb->data); /* encaps & send */
+ slcan_encaps(sl, (struct can_frame *)skb->data); /* encaps & send */
spin_unlock(&sl->lock);

out:
@@ -632,7 +635,7 @@ static int slcan_transmit_cmd(struct slcan *sl, const unsigned char *cmd)
}

/* Netdevice UP -> DOWN routine */
-static int slc_close(struct net_device *dev)
+static int slcan_netdev_close(struct net_device *dev)
{
struct slcan *sl = netdev_priv(dev);
int err;
@@ -661,10 +664,10 @@ static int slc_close(struct net_device *dev)
}

/* Netdevice DOWN -> UP routine */
-static int slc_open(struct net_device *dev)
+static int slcan_netdev_open(struct net_device *dev)
{
struct slcan *sl = netdev_priv(dev);
- unsigned char cmd[SLC_MTU];
+ unsigned char cmd[SLCAN_MTU];
int err, s;

/* The baud rate is not set with the command
@@ -724,16 +727,16 @@ static int slc_open(struct net_device *dev)
return err;
}

-static int slcan_change_mtu(struct net_device *dev, int new_mtu)
+static int slcan_netdev_change_mtu(struct net_device *dev, int new_mtu)
{
return -EINVAL;
}

-static const struct net_device_ops slc_netdev_ops = {
- .ndo_open = slc_open,
- .ndo_stop = slc_close,
- .ndo_start_xmit = slc_xmit,
- .ndo_change_mtu = slcan_change_mtu,
+static const struct net_device_ops slcan_netdev_ops = {
+ .ndo_open = slcan_netdev_open,
+ .ndo_stop = slcan_netdev_close,
+ .ndo_start_xmit = slcan_netdev_xmit,
+ .ndo_change_mtu = slcan_netdev_change_mtu,
};

/******************************************
@@ -807,7 +810,7 @@ static int slcan_open(struct tty_struct *tty)
/* Configure netdev interface */
sl->dev = dev;
strscpy(dev->name, "slcan%d", sizeof(dev->name));
- dev->netdev_ops = &slc_netdev_ops;
+ dev->netdev_ops = &slcan_netdev_ops;
slcan_set_ethtool_ops(dev);

/* Mark ldisc channel as alive */
@@ -875,7 +878,7 @@ static int slcan_ioctl(struct tty_struct *tty, unsigned int cmd,
}
}

-static struct tty_ldisc_ops slc_ldisc = {
+static struct tty_ldisc_ops slcan_ldisc = {
.owner = THIS_MODULE,
.num = N_SLCAN,
.name = "slcan",
@@ -893,7 +896,7 @@ static int __init slcan_init(void)
pr_info("slcan: serial line CAN interface driver\n");

/* Fill in our line protocol discipline, and register it */
- status = tty_register_ldisc(&slc_ldisc);
+ status = tty_register_ldisc(&slcan_ldisc);
if (status)
pr_err("slcan: can't register line discipline\n");

@@ -905,7 +908,7 @@ static void __exit slcan_exit(void)
/* This will only be called when all channels have been closed by
* userspace - tty_ldisc.c takes care of the module's refcount.
*/
- tty_unregister_ldisc(&slc_ldisc);
+ tty_unregister_ldisc(&slcan_ldisc);
}

module_init(slcan_init);
--
2.32.0

2022-07-16 17:37:27

by Dario Binacchi

[permalink] [raw]
Subject: [RFC PATCH 2/5] can: slcan: remove legacy infrastructure

Taking inspiration from the drivers/net/can/can327.c driver and at the
suggestion of its author Max Staudt, I removed legacy stuff like
`SLCAN_MAGIC' and `slcan_devs' resulting in simplification of the code
and its maintainability.

The use of slcan_devs is derived from a very old kernel, since slip.c
is about 30 years old, so today's kernel allows us to remove it.

The .hangup() ldisc function, which only called the ldisc .close(), has
been removed since the ldisc layer calls .close() in a good place
anyway.

The `maxdev' module parameter has also been removed.

CC: Max Staudt <[email protected]>
Signed-off-by: Dario Binacchi <[email protected]>
---

drivers/net/can/slcan/slcan-core.c | 317 ++++++-----------------------
1 file changed, 64 insertions(+), 253 deletions(-)

diff --git a/drivers/net/can/slcan/slcan-core.c b/drivers/net/can/slcan/slcan-core.c
index 3394d059fc29..92cab093453d 100644
--- a/drivers/net/can/slcan/slcan-core.c
+++ b/drivers/net/can/slcan/slcan-core.c
@@ -1,11 +1,14 @@
/*
* slcan.c - serial line CAN interface driver (using tty line discipline)
*
- * This file is derived from linux/drivers/net/slip/slip.c
+ * This file is derived from linux/drivers/net/slip/slip.c and got
+ * inspiration from linux/drivers/net/can/can327.c for the rework made
+ * on the line discipline code.
*
* slip.c Authors : Laurence Culhane <[email protected]>
* Fred N. van Kempen <[email protected]>
* slcan.c Author : Oliver Hartkopp <[email protected]>
+ * can327.c Author : Max Staudt <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
@@ -46,15 +49,6 @@ MODULE_DESCRIPTION("serial line CAN interface");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Oliver Hartkopp <[email protected]>");

-#define SLCAN_MAGIC 0x53CA
-
-static int maxdev = 10; /* MAX number of SLCAN channels;
- * This can be overridden with
- * insmod slcan.ko maxdev=nnn
- */
-module_param(maxdev, int, 0);
-MODULE_PARM_DESC(maxdev, "Maximum number of slcan interfaces");
-
/* maximum rx buffer len: extended CAN frame with timestamp */
#define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r") + 1)

@@ -68,7 +62,6 @@ MODULE_PARM_DESC(maxdev, "Maximum number of slcan interfaces");
SLC_STATE_BE_TXCNT_LEN)
struct slcan {
struct can_priv can;
- int magic;

/* Various fields. */
struct tty_struct *tty; /* ptr to TTY structure */
@@ -84,17 +77,14 @@ struct slcan {
int xleft; /* bytes left in XMIT queue */

unsigned long flags; /* Flag values/ mode etc */
-#define SLF_INUSE 0 /* Channel in use */
-#define SLF_ERROR 1 /* Parity, etc. error */
-#define SLF_XCMD 2 /* Command transmission */
+#define SLF_ERROR 0 /* Parity, etc. error */
+#define SLF_XCMD 1 /* Command transmission */
unsigned long cmd_flags; /* Command flags */
#define CF_ERR_RST 0 /* Reset errors on open */
wait_queue_head_t xcmd_wait; /* Wait queue for commands */
/* transmission */
};

-static struct net_device **slcan_devs;
-
static const u32 slcan_bitrate_const[] = {
10000, 20000, 50000, 100000, 125000,
250000, 500000, 800000, 1000000
@@ -538,9 +528,8 @@ static void slcan_transmit(struct work_struct *work)

spin_lock_bh(&sl->lock);
/* First make sure we're connected. */
- if (!sl->tty || sl->magic != SLCAN_MAGIC ||
- (unlikely(!netif_running(sl->dev)) &&
- likely(!test_bit(SLF_XCMD, &sl->flags)))) {
+ if (unlikely(!netif_running(sl->dev)) &&
+ likely(!test_bit(SLF_XCMD, &sl->flags))) {
spin_unlock_bh(&sl->lock);
return;
}
@@ -575,13 +564,9 @@ static void slcan_transmit(struct work_struct *work)
*/
static void slcan_write_wakeup(struct tty_struct *tty)
{
- struct slcan *sl;
+ struct slcan *sl = (struct slcan *)tty->disc_data;

- rcu_read_lock();
- sl = rcu_dereference(tty->disc_data);
- if (sl)
- schedule_work(&sl->tx_work);
- rcu_read_unlock();
+ schedule_work(&sl->tx_work);
}

/* Send a can_frame to a TTY queue. */
@@ -652,25 +637,21 @@ static int slc_close(struct net_device *dev)
struct slcan *sl = netdev_priv(dev);
int err;

- spin_lock_bh(&sl->lock);
- if (sl->tty) {
- if (sl->can.bittiming.bitrate &&
- sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
- spin_unlock_bh(&sl->lock);
- err = slcan_transmit_cmd(sl, "C\r");
- spin_lock_bh(&sl->lock);
- if (err)
- netdev_warn(dev,
- "failed to send close command 'C\\r'\n");
- }
-
- /* TTY discipline is running. */
- clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
+ if (sl->can.bittiming.bitrate &&
+ sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
+ err = slcan_transmit_cmd(sl, "C\r");
+ if (err)
+ netdev_warn(dev,
+ "failed to send close command 'C\\r'\n");
}
+
+ /* TTY discipline is running. */
+ clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
+ flush_work(&sl->tx_work);
+
netif_stop_queue(dev);
sl->rcount = 0;
sl->xleft = 0;
- spin_unlock_bh(&sl->lock);
close_candev(dev);
sl->can.state = CAN_STATE_STOPPED;
if (sl->can.bittiming.bitrate == CAN_BITRATE_UNKNOWN)
@@ -686,9 +667,6 @@ static int slc_open(struct net_device *dev)
unsigned char cmd[SLC_MTU];
int err, s;

- if (!sl->tty)
- return -ENODEV;
-
/* The baud rate is not set with the command
* `ip link set <iface> type can bitrate <baud>' and therefore
* can.bittiming.bitrate is CAN_BITRATE_UNSET (0), causing
@@ -703,8 +681,6 @@ static int slc_open(struct net_device *dev)
return err;
}

- sl->flags &= BIT(SLF_INUSE);
-
if (sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
for (s = 0; s < ARRAY_SIZE(slcan_bitrate_const); s++) {
if (sl->can.bittiming.bitrate == slcan_bitrate_const[s])
@@ -748,14 +724,6 @@ static int slc_open(struct net_device *dev)
return err;
}

-static void slc_dealloc(struct slcan *sl)
-{
- int i = sl->dev->base_addr;
-
- free_candev(sl->dev);
- slcan_devs[i] = NULL;
-}
-
static int slcan_change_mtu(struct net_device *dev, int new_mtu)
{
return -EINVAL;
@@ -785,7 +753,7 @@ static void slcan_receive_buf(struct tty_struct *tty,
{
struct slcan *sl = (struct slcan *)tty->disc_data;

- if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev))
+ if (!netif_running(sl->dev))
return;

/* Read the characters out of the buffer */
@@ -800,80 +768,15 @@ static void slcan_receive_buf(struct tty_struct *tty,
}
}

-/************************************
- * slcan_open helper routines.
- ************************************/
-
-/* Collect hanged up channels */
-static void slc_sync(void)
-{
- int i;
- struct net_device *dev;
- struct slcan *sl;
-
- for (i = 0; i < maxdev; i++) {
- dev = slcan_devs[i];
- if (!dev)
- break;
-
- sl = netdev_priv(dev);
- if (sl->tty)
- continue;
- if (dev->flags & IFF_UP)
- dev_close(dev);
- }
-}
-
-/* Find a free SLCAN channel, and link in this `tty' line. */
-static struct slcan *slc_alloc(void)
-{
- int i;
- struct net_device *dev = NULL;
- struct slcan *sl;
-
- for (i = 0; i < maxdev; i++) {
- dev = slcan_devs[i];
- if (!dev)
- break;
- }
-
- /* Sorry, too many, all slots in use */
- if (i >= maxdev)
- return NULL;
-
- dev = alloc_candev(sizeof(*sl), 1);
- if (!dev)
- return NULL;
-
- snprintf(dev->name, sizeof(dev->name), "slcan%d", i);
- dev->netdev_ops = &slc_netdev_ops;
- dev->base_addr = i;
- slcan_set_ethtool_ops(dev);
- sl = netdev_priv(dev);
-
- /* Initialize channel control data */
- sl->magic = SLCAN_MAGIC;
- sl->dev = dev;
- sl->can.bitrate_const = slcan_bitrate_const;
- sl->can.bitrate_const_cnt = ARRAY_SIZE(slcan_bitrate_const);
- spin_lock_init(&sl->lock);
- INIT_WORK(&sl->tx_work, slcan_transmit);
- init_waitqueue_head(&sl->xcmd_wait);
- slcan_devs[i] = dev;
-
- return sl;
-}
-
/* Open the high-level part of the SLCAN channel.
* This function is called by the TTY module when the
- * SLCAN line discipline is called for. Because we are
- * sure the tty line exists, we only have to link it to
- * a free SLCAN channel...
+ * SLCAN line discipline is called for.
*
* Called in process context serialized from other ldisc calls.
*/
static int slcan_open(struct tty_struct *tty)
{
+ struct net_device *dev;
struct slcan *sl;
int err;

@@ -883,72 +786,50 @@ static int slcan_open(struct tty_struct *tty)
if (!tty->ops->write)
return -EOPNOTSUPP;

- /* RTnetlink lock is misused here to serialize concurrent
- * opens of slcan channels. There are better ways, but it is
- * the simplest one.
- */
- rtnl_lock();
+ dev = alloc_candev(sizeof(*sl), 1);
+ if (!dev)
+ return -ENFILE;

- /* Collect hanged up channels. */
- slc_sync();
+ sl = netdev_priv(dev);

- sl = tty->disc_data;
+ /* Configure TTY interface */
+ tty->receive_room = 65536; /* We don't flow control */
+ sl->rcount = 0;
+ sl->xleft = 0;
+ spin_lock_init(&sl->lock);
+ INIT_WORK(&sl->tx_work, slcan_transmit);
+ init_waitqueue_head(&sl->xcmd_wait);

- err = -EEXIST;
- /* First make sure we're not already connected. */
- if (sl && sl->magic == SLCAN_MAGIC)
- goto err_exit;
+ /* Configure CAN metadata */
+ sl->can.bitrate_const = slcan_bitrate_const;
+ sl->can.bitrate_const_cnt = ARRAY_SIZE(slcan_bitrate_const);

- /* OK. Find a free SLCAN channel to use. */
- err = -ENFILE;
- sl = slc_alloc();
- if (!sl)
- goto err_exit;
+ /* Configure netdev interface */
+ sl->dev = dev;
+ strscpy(dev->name, "slcan%d", sizeof(dev->name));
+ dev->netdev_ops = &slc_netdev_ops;
+ slcan_set_ethtool_ops(dev);

+ /* Mark ldisc channel as alive */
sl->tty = tty;
tty->disc_data = sl;

- if (!test_bit(SLF_INUSE, &sl->flags)) {
- /* Perform the low-level SLCAN initialization. */
- sl->rcount = 0;
- sl->xleft = 0;
-
- set_bit(SLF_INUSE, &sl->flags);
-
- rtnl_unlock();
- err = register_candev(sl->dev);
- if (err) {
- pr_err("slcan: can't register candev\n");
- goto err_free_chan;
- }
- } else {
- rtnl_unlock();
+ err = register_candev(dev);
+ if (err) {
+ free_candev(dev);
+ pr_err("slcan: can't register candev\n");
+ return err;
}

- tty->receive_room = 65536; /* We don't flow control */
-
+ netdev_info(dev, "slcan on %s.\n", tty->name);
/* TTY layer expects 0 on success */
return 0;
-
-err_free_chan:
- rtnl_lock();
- sl->tty = NULL;
- tty->disc_data = NULL;
- clear_bit(SLF_INUSE, &sl->flags);
- slc_dealloc(sl);
- rtnl_unlock();
- return err;
-
-err_exit:
- rtnl_unlock();
-
- /* Count references from TTY module */
- return err;
}

/* Close down a SLCAN channel.
* This means flushing out any pending queues, and then returning. This
* call is serialized against other ldisc functions.
+ * Once this is called, no other ldisc function of ours is entered.
*
* We also use this method for a hangup event.
*/
@@ -956,28 +837,20 @@ static void slcan_close(struct tty_struct *tty)
{
struct slcan *sl = (struct slcan *)tty->disc_data;

- /* First make sure we're connected. */
- if (!sl || sl->magic != SLCAN_MAGIC || sl->tty != tty)
- return;
+ /* unregister_netdev() calls .ndo_stop() so we don't have to.
+ * Our .ndo_stop() also flushes the TTY write wakeup handler,
+ * so we can safely set sl->tty = NULL after this.
+ */
+ unregister_candev(sl->dev);

+ /* Mark channel as dead */
spin_lock_bh(&sl->lock);
- rcu_assign_pointer(tty->disc_data, NULL);
+ tty->disc_data = NULL;
sl->tty = NULL;
spin_unlock_bh(&sl->lock);

- synchronize_rcu();
- flush_work(&sl->tx_work);
-
- slc_close(sl->dev);
- unregister_candev(sl->dev);
- rtnl_lock();
- slc_dealloc(sl);
- rtnl_unlock();
-}
-
-static void slcan_hangup(struct tty_struct *tty)
-{
- slcan_close(tty);
+ netdev_info(sl->dev, "slcan off %s.\n", tty->name);
+ free_candev(sl->dev);
}

/* Perform I/O control on an active SLCAN channel. */
@@ -987,10 +860,6 @@ static int slcan_ioctl(struct tty_struct *tty, unsigned int cmd,
struct slcan *sl = (struct slcan *)tty->disc_data;
unsigned int tmp;

- /* First make sure we're connected. */
- if (!sl || sl->magic != SLCAN_MAGIC)
- return -EINVAL;
-
switch (cmd) {
case SIOCGIFNAME:
tmp = strlen(sl->dev->name) + 1;
@@ -1012,7 +881,6 @@ static struct tty_ldisc_ops slc_ldisc = {
.name = "slcan",
.open = slcan_open,
.close = slcan_close,
- .hangup = slcan_hangup,
.ioctl = slcan_ioctl,
.receive_buf = slcan_receive_buf,
.write_wakeup = slcan_write_wakeup,
@@ -1022,78 +890,21 @@ static int __init slcan_init(void)
{
int status;

- if (maxdev < 4)
- maxdev = 4; /* Sanity */
-
pr_info("slcan: serial line CAN interface driver\n");
- pr_info("slcan: %d dynamic interface channels.\n", maxdev);
-
- slcan_devs = kcalloc(maxdev, sizeof(struct net_device *), GFP_KERNEL);
- if (!slcan_devs)
- return -ENOMEM;

/* Fill in our line protocol discipline, and register it */
status = tty_register_ldisc(&slc_ldisc);
- if (status) {
+ if (status)
pr_err("slcan: can't register line discipline\n");
- kfree(slcan_devs);
- }
+
return status;
}

static void __exit slcan_exit(void)
{
- int i;
- struct net_device *dev;
- struct slcan *sl;
- unsigned long timeout = jiffies + HZ;
- int busy = 0;
-
- if (!slcan_devs)
- return;
-
- /* First of all: check for active disciplines and hangup them.
- */
- do {
- if (busy)
- msleep_interruptible(100);
-
- busy = 0;
- for (i = 0; i < maxdev; i++) {
- dev = slcan_devs[i];
- if (!dev)
- continue;
- sl = netdev_priv(dev);
- spin_lock_bh(&sl->lock);
- if (sl->tty) {
- busy++;
- tty_hangup(sl->tty);
- }
- spin_unlock_bh(&sl->lock);
- }
- } while (busy && time_before(jiffies, timeout));
-
- /* FIXME: hangup is async so we should wait when doing this second
- * phase
+ /* This will only be called when all channels have been closed by
+ * userspace - tty_ldisc.c takes care of the module's refcount.
*/
-
- for (i = 0; i < maxdev; i++) {
- dev = slcan_devs[i];
- if (!dev)
- continue;
-
- sl = netdev_priv(dev);
- if (sl->tty)
- netdev_err(dev, "tty discipline still running\n");
-
- slc_close(dev);
- unregister_candev(dev);
- slc_dealloc(sl);
- }
-
- kfree(slcan_devs);
- slcan_devs = NULL;
-
tty_unregister_ldisc(&slc_ldisc);
}

--
2.32.0

2022-07-16 17:39:39

by Dario Binacchi

[permalink] [raw]
Subject: [RFC PATCH 5/5] can: slcan: send the listen-only command to the adapter

In case the bitrate has been set via ip tool, this patch changes the
driver to send the listen-only ("L\r") command to the adapter.

Signed-off-by: Dario Binacchi <[email protected]>

---

drivers/net/can/slcan/slcan-core.c | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/drivers/net/can/slcan/slcan-core.c b/drivers/net/can/slcan/slcan-core.c
index 7a1540507ecd..d97dfeccbf9c 100644
--- a/drivers/net/can/slcan/slcan-core.c
+++ b/drivers/net/can/slcan/slcan-core.c
@@ -711,10 +711,21 @@ static int slcan_netdev_open(struct net_device *dev)
}
}

- err = slcan_transmit_cmd(sl, "O\r");
- if (err) {
- netdev_err(dev, "failed to send open command 'O\\r'\n");
- goto cmd_transmit_failed;
+ /* listen-only command overrides open command */
+ if (sl->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
+ err = slcan_transmit_cmd(sl, "L\r");
+ if (err) {
+ netdev_err(dev,
+ "failed to send listen-only command 'L\\r'\n");
+ goto cmd_transmit_failed;
+ }
+ } else {
+ err = slcan_transmit_cmd(sl, "O\r");
+ if (err) {
+ netdev_err(dev,
+ "failed to send open command 'O\\r'\n");
+ goto cmd_transmit_failed;
+ }
}
}

@@ -801,6 +812,7 @@ static int slcan_open(struct tty_struct *tty)
/* Configure CAN metadata */
sl->can.bitrate_const = slcan_bitrate_const;
sl->can.bitrate_const_cnt = ARRAY_SIZE(slcan_bitrate_const);
+ sl->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY;

/* Configure netdev interface */
sl->dev = dev;
--
2.32.0

2022-07-17 21:54:43

by Max Staudt

[permalink] [raw]
Subject: Re: [RFC PATCH 2/5] can: slcan: remove legacy infrastructure

Hi Dario,

This looks good, thank you for continuing to look after slcan!

A few comments below.



On Sat, 16 Jul 2022 19:00:04 +0200
Dario Binacchi <[email protected]> wrote:

[...]


> @@ -68,7 +62,6 @@ MODULE_PARM_DESC(maxdev, "Maximum number of slcan interfaces");
> SLC_STATE_BE_TXCNT_LEN)
> struct slcan {
> struct can_priv can;
> - int magic;
>
> /* Various fields. */
> struct tty_struct *tty; /* ptr to TTY structure */
> @@ -84,17 +77,14 @@ struct slcan {
> int xleft; /* bytes left in XMIT queue */
>
> unsigned long flags; /* Flag values/ mode etc */
> -#define SLF_INUSE 0 /* Channel in use */
> -#define SLF_ERROR 1 /* Parity, etc. error */
> -#define SLF_XCMD 2 /* Command transmission */
> +#define SLF_ERROR 0 /* Parity, etc. error */
> +#define SLF_XCMD 1 /* Command transmission */
> unsigned long cmd_flags; /* Command flags */
> #define CF_ERR_RST 0 /* Reset errors on open */
> wait_queue_head_t xcmd_wait; /* Wait queue for commands */

I assume xcmd_wait() came in as part of the previous patch series?


[...]


> /* Send a can_frame to a TTY queue. */
> @@ -652,25 +637,21 @@ static int slc_close(struct net_device *dev)
> struct slcan *sl = netdev_priv(dev);
> int err;
>
> - spin_lock_bh(&sl->lock);
> - if (sl->tty) {
> - if (sl->can.bittiming.bitrate &&
> - sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
> - spin_unlock_bh(&sl->lock);
> - err = slcan_transmit_cmd(sl, "C\r");
> - spin_lock_bh(&sl->lock);
> - if (err)
> - netdev_warn(dev,
> - "failed to send close command 'C\\r'\n");
> - }
> -
> - /* TTY discipline is running. */
> - clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
> + if (sl->can.bittiming.bitrate &&
> + sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
> + err = slcan_transmit_cmd(sl, "C\r");
> + if (err)
> + netdev_warn(dev,
> + "failed to send close command 'C\\r'\n");
> }
> +
> + /* TTY discipline is running. */
> + clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
> + flush_work(&sl->tx_work);
> +
> netif_stop_queue(dev);
> sl->rcount = 0;
> sl->xleft = 0;

I suggest moving these two assignments to slc_open() - see below.


[...]


> @@ -883,72 +786,50 @@ static int slcan_open(struct tty_struct *tty)
> if (!tty->ops->write)
> return -EOPNOTSUPP;
>
> - /* RTnetlink lock is misused here to serialize concurrent
> - * opens of slcan channels. There are better ways, but it is
> - * the simplest one.
> - */
> - rtnl_lock();
> + dev = alloc_candev(sizeof(*sl), 1);
> + if (!dev)
> + return -ENFILE;
>
> - /* Collect hanged up channels. */
> - slc_sync();
> + sl = netdev_priv(dev);
>
> - sl = tty->disc_data;
> + /* Configure TTY interface */
> + tty->receive_room = 65536; /* We don't flow control */
> + sl->rcount = 0;
> + sl->xleft = 0;

I suggest moving the zeroing to slc_open() - i.e. to the netdev open
function. As a bonus, you can then remove the same two assignments from
slc_close() (see above). They are only used when netif_running(), with
appropiate guards already in place as far as I can see.


> + spin_lock_init(&sl->lock);
> + INIT_WORK(&sl->tx_work, slcan_transmit);
> + init_waitqueue_head(&sl->xcmd_wait);
>
> - err = -EEXIST;
> - /* First make sure we're not already connected. */
> - if (sl && sl->magic == SLCAN_MAGIC)
> - goto err_exit;
> + /* Configure CAN metadata */
> + sl->can.bitrate_const = slcan_bitrate_const;
> + sl->can.bitrate_const_cnt = ARRAY_SIZE(slcan_bitrate_const);
>
> - /* OK. Find a free SLCAN channel to use. */
> - err = -ENFILE;
> - sl = slc_alloc();
> - if (!sl)
> - goto err_exit;
> + /* Configure netdev interface */
> + sl->dev = dev;
> + strscpy(dev->name, "slcan%d", sizeof(dev->name));

The third parameter looks... unintentional :)

What do the maintainers think of dropping the old "slcan" name, and
just allowing this to be a normal canX device? These patches do bring
it closer to that, after all. In this case, this name string magic
could be dropped altogether.


[...]



This looks good to me overall.

Thanks Dario!


Max

2022-07-18 07:16:24

by Oliver Hartkopp

[permalink] [raw]
Subject: Re: [RFC PATCH 2/5] can: slcan: remove legacy infrastructure



On 17.07.22 23:38, Max Staudt wrote:
> Hi Dario,
>
> This looks good, thank you for continuing to look after slcan!
>
> A few comments below.
>
>
>
> On Sat, 16 Jul 2022 19:00:04 +0200
> Dario Binacchi <[email protected]> wrote:
>
> [...]
>
>
>> @@ -68,7 +62,6 @@ MODULE_PARM_DESC(maxdev, "Maximum number of slcan interfaces");
>> SLC_STATE_BE_TXCNT_LEN)
>> struct slcan {
>> struct can_priv can;
>> - int magic;
>>
>> /* Various fields. */
>> struct tty_struct *tty; /* ptr to TTY structure */
>> @@ -84,17 +77,14 @@ struct slcan {
>> int xleft; /* bytes left in XMIT queue */
>>
>> unsigned long flags; /* Flag values/ mode etc */
>> -#define SLF_INUSE 0 /* Channel in use */
>> -#define SLF_ERROR 1 /* Parity, etc. error */
>> -#define SLF_XCMD 2 /* Command transmission */
>> +#define SLF_ERROR 0 /* Parity, etc. error */
>> +#define SLF_XCMD 1 /* Command transmission */
>> unsigned long cmd_flags; /* Command flags */
>> #define CF_ERR_RST 0 /* Reset errors on open */
>> wait_queue_head_t xcmd_wait; /* Wait queue for commands */
>
> I assume xcmd_wait() came in as part of the previous patch series?
>
>
> [...]
>
>
>> /* Send a can_frame to a TTY queue. */
>> @@ -652,25 +637,21 @@ static int slc_close(struct net_device *dev)
>> struct slcan *sl = netdev_priv(dev);
>> int err;
>>
>> - spin_lock_bh(&sl->lock);
>> - if (sl->tty) {
>> - if (sl->can.bittiming.bitrate &&
>> - sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
>> - spin_unlock_bh(&sl->lock);
>> - err = slcan_transmit_cmd(sl, "C\r");
>> - spin_lock_bh(&sl->lock);
>> - if (err)
>> - netdev_warn(dev,
>> - "failed to send close command 'C\\r'\n");
>> - }
>> -
>> - /* TTY discipline is running. */
>> - clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
>> + if (sl->can.bittiming.bitrate &&
>> + sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
>> + err = slcan_transmit_cmd(sl, "C\r");
>> + if (err)
>> + netdev_warn(dev,
>> + "failed to send close command 'C\\r'\n");
>> }
>> +
>> + /* TTY discipline is running. */
>> + clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
>> + flush_work(&sl->tx_work);
>> +
>> netif_stop_queue(dev);
>> sl->rcount = 0;
>> sl->xleft = 0;
>
> I suggest moving these two assignments to slc_open() - see below.
>
>
> [...]
>
>
>> @@ -883,72 +786,50 @@ static int slcan_open(struct tty_struct *tty)
>> if (!tty->ops->write)
>> return -EOPNOTSUPP;
>>
>> - /* RTnetlink lock is misused here to serialize concurrent
>> - * opens of slcan channels. There are better ways, but it is
>> - * the simplest one.
>> - */
>> - rtnl_lock();
>> + dev = alloc_candev(sizeof(*sl), 1);
>> + if (!dev)
>> + return -ENFILE;
>>
>> - /* Collect hanged up channels. */
>> - slc_sync();
>> + sl = netdev_priv(dev);
>>
>> - sl = tty->disc_data;
>> + /* Configure TTY interface */
>> + tty->receive_room = 65536; /* We don't flow control */
>> + sl->rcount = 0;
>> + sl->xleft = 0;
>
> I suggest moving the zeroing to slc_open() - i.e. to the netdev open
> function. As a bonus, you can then remove the same two assignments from
> slc_close() (see above). They are only used when netif_running(), with
> appropiate guards already in place as far as I can see.
>
>
>> + spin_lock_init(&sl->lock);
>> + INIT_WORK(&sl->tx_work, slcan_transmit);
>> + init_waitqueue_head(&sl->xcmd_wait);
>>
>> - err = -EEXIST;
>> - /* First make sure we're not already connected. */
>> - if (sl && sl->magic == SLCAN_MAGIC)
>> - goto err_exit;
>> + /* Configure CAN metadata */
>> + sl->can.bitrate_const = slcan_bitrate_const;
>> + sl->can.bitrate_const_cnt = ARRAY_SIZE(slcan_bitrate_const);
>>
>> - /* OK. Find a free SLCAN channel to use. */
>> - err = -ENFILE;
>> - sl = slc_alloc();
>> - if (!sl)
>> - goto err_exit;
>> + /* Configure netdev interface */
>> + sl->dev = dev;
>> + strscpy(dev->name, "slcan%d", sizeof(dev->name));
>
> The third parameter looks... unintentional :)
>
> What do the maintainers think of dropping the old "slcan" name, and
> just allowing this to be a normal canX device? These patches do bring
> it closer to that, after all. In this case, this name string magic
> could be dropped altogether.
>

I'm fine with it in general. But we have to take into account that there
might be existing setups that still might use the slcan_attach or slcand
mechanic which will likely break after the kernel update.

But in the end the slcan0 shows up everywhere - even in log files, etc.

So we really should name it canX. When people really get in trouble with
it, they can rename the network interface name with the 'ip' tool ...

Best regards,
Oliver

>
> [...]
>
>
>
> This looks good to me overall.
>
> Thanks Dario!
>
>
> Max

2022-07-18 10:25:39

by Marc Kleine-Budde

[permalink] [raw]
Subject: Re: [RFC PATCH 2/5] can: slcan: remove legacy infrastructure

On 18.07.2022 08:57:21, Oliver Hartkopp wrote:
> > What do the maintainers think of dropping the old "slcan" name, and
> > just allowing this to be a normal canX device? These patches do bring
> > it closer to that, after all. In this case, this name string magic
> > could be dropped altogether.
> >
>
> I'm fine with it in general. But we have to take into account that there
> might be existing setups that still might use the slcan_attach or slcand
> mechanic which will likely break after the kernel update.
>
> But in the end the slcan0 shows up everywhere - even in log files, etc.
>
> So we really should name it canX. When people really get in trouble with it,
> they can rename the network interface name with the 'ip' tool ...

Don't break user space! If you don't like slcanX use udev to give it a
proper name.

regards,
Marc

--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung West/Dortmund | Phone: +49-231-2826-924 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |


Attachments:
(No filename) (1.11 kB)
signature.asc (499.00 B)
Download all attachments

2022-07-18 10:44:09

by Oliver Hartkopp

[permalink] [raw]
Subject: Re: [RFC PATCH 2/5] can: slcan: remove legacy infrastructure



On 7/18/22 12:15, Marc Kleine-Budde wrote:
> On 18.07.2022 08:57:21, Oliver Hartkopp wrote:
>>> What do the maintainers think of dropping the old "slcan" name, and
>>> just allowing this to be a normal canX device? These patches do bring
>>> it closer to that, after all. In this case, this name string magic
>>> could be dropped altogether.
>>>
>>
>> I'm fine with it in general. But we have to take into account that there
>> might be existing setups that still might use the slcan_attach or slcand
>> mechanic which will likely break after the kernel update.
>>
>> But in the end the slcan0 shows up everywhere - even in log files, etc.
>>
>> So we really should name it canX. When people really get in trouble with it,
>> they can rename the network interface name with the 'ip' tool ...
>
> Don't break user space! If you don't like slcanX use udev to give it a
> proper name.

Ok. Fine with me too.

IMO it does not break user space when slcan gets the common naming
schema for CAN interface names.

We had the same thing with 'eth0' which is now named enblablabla or
'wlan0' now named wlp2s0.

But I have no strong opinion on that naming ...

Best regards,
Oliver


2022-07-18 10:52:35

by Marc Kleine-Budde

[permalink] [raw]
Subject: Re: [RFC PATCH 5/5] can: slcan: send the listen-only command to the adapter

The subject can be enhanced, as the listen-only command ist not send
unconditionally. What about: "add support for listen-only mode"?

On 16.07.2022 19:00:07, Dario Binacchi wrote:
> In case the bitrate has been set via ip tool, this patch changes the
> driver to send the listen-only ("L\r") command to the adapter.

...but only of CAN_CTRLMODE_LISTENONLY is requested.

What about:

For non-legacy, i.e. ip based configuration, add support for listen-only
mode. If listen-only is requested send a listen-only ("L\r") command
instead of an open ("O\r") command to the adapter..

>
> Signed-off-by: Dario Binacchi <[email protected]>
>
> ---
>
> drivers/net/can/slcan/slcan-core.c | 20 ++++++++++++++++----
> 1 file changed, 16 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/net/can/slcan/slcan-core.c b/drivers/net/can/slcan/slcan-core.c
> index 7a1540507ecd..d97dfeccbf9c 100644
> --- a/drivers/net/can/slcan/slcan-core.c
> +++ b/drivers/net/can/slcan/slcan-core.c
> @@ -711,10 +711,21 @@ static int slcan_netdev_open(struct net_device *dev)
> }
> }
>
> - err = slcan_transmit_cmd(sl, "O\r");
> - if (err) {
> - netdev_err(dev, "failed to send open command 'O\\r'\n");
> - goto cmd_transmit_failed;
> + /* listen-only command overrides open command */

I think this comment can be removed.

> + if (sl->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
> + err = slcan_transmit_cmd(sl, "L\r");
> + if (err) {
> + netdev_err(dev,
> + "failed to send listen-only command 'L\\r'\n");
> + goto cmd_transmit_failed;
> + }
> + } else {
> + err = slcan_transmit_cmd(sl, "O\r");
> + if (err) {
> + netdev_err(dev,
> + "failed to send open command 'O\\r'\n");
> + goto cmd_transmit_failed;
> + }
> }
> }
>
> @@ -801,6 +812,7 @@ static int slcan_open(struct tty_struct *tty)
> /* Configure CAN metadata */
> sl->can.bitrate_const = slcan_bitrate_const;
> sl->can.bitrate_const_cnt = ARRAY_SIZE(slcan_bitrate_const);
> + sl->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY;
>
> /* Configure netdev interface */
> sl->dev = dev;

Marc

--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung West/Dortmund | Phone: +49-231-2826-924 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |


Attachments:
(No filename) (2.40 kB)
signature.asc (499.00 B)
Download all attachments

2022-07-19 01:08:50

by Max Staudt

[permalink] [raw]
Subject: Re: [RFC PATCH 2/5] can: slcan: remove legacy infrastructure

On Mon, 18 Jul 2022 12:23:05 +0200
Oliver Hartkopp <[email protected]> wrote:

> IMO it does not break user space when slcan gets the common naming
> schema for CAN interface names.

For what it's worth, slcan provides a specific ioctl to resolve from
(tty fd) to (netdev name). As far as I can see, any sane program should
use this API to find whatever device it has created, irrespective of
the netdev name.


@ Marc: slcand and slcan_attach in can-utils do not depend on the
slcanX name, as far as I can see. They are the main interfaces to
slcan, and apart from that I can only think of scripts doing netlink
stuff and scanning for slcanX. Does this change your opinion, or is
breaking such scripts already a step too far?

I'm thinking that in other circumstances, I've had cases where devices
were numbered differently just because they enumerated faster or slower
at different boots, or with other kernel versions, or because of media
inserted, etc. - though renumbering is of course one step less than
changing the prefix.


> We had the same thing with 'eth0' which is now named enblablabla or
> 'wlan0' now named wlp2s0.

Actually, the kernel still calls them ethX and wlanX. It's systemd+udev
that renames them to the "unique" names afterwards. It's an extra step
that happens in userspace.

udev is immensely useful, but also great stuff for race conditions :)



Max

2022-07-19 20:12:11

by Marc Kleine-Budde

[permalink] [raw]
Subject: Re: [RFC PATCH 2/5] can: slcan: remove legacy infrastructure

On 19.07.2022 03:03:26, Max Staudt wrote:
> On Mon, 18 Jul 2022 12:23:05 +0200
> Oliver Hartkopp <[email protected]> wrote:
>
> > IMO it does not break user space when slcan gets the common naming
> > schema for CAN interface names.
>
> For what it's worth, slcan provides a specific ioctl to resolve from
> (tty fd) to (netdev name). As far as I can see, any sane program should
> use this API to find whatever device it has created, irrespective of
> the netdev name.
>
>
> @ Marc: slcand and slcan_attach in can-utils do not depend on the
> slcanX name, as far as I can see. They are the main interfaces to
> slcan, and apart from that I can only think of scripts doing netlink
> stuff and scanning for slcanX. Does this change your opinion, or is
> breaking such scripts already a step too far?
>
> I'm thinking that in other circumstances, I've had cases where devices
> were numbered differently just because they enumerated faster or slower
> at different boots, or with other kernel versions, or because of media
> inserted, etc. - though renumbering is of course one step less than
> changing the prefix.

Convinced! Please change the patch to use standard canX interface
naming. Can you add a pointer to the ioctl() to get the network
interface name to the tty fd in the patch description.

> > We had the same thing with 'eth0' which is now named enblablabla or
> > 'wlan0' now named wlp2s0.
>
> Actually, the kernel still calls them ethX and wlanX. It's systemd+udev
> that renames them to the "unique" names afterwards. It's an extra step
> that happens in userspace.
>
> udev is immensely useful, but also great stuff for race conditions :)

Marc

--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung West/Dortmund | Phone: +49-231-2826-924 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |


Attachments:
(No filename) (1.94 kB)
signature.asc (499.00 B)
Download all attachments

2022-07-21 08:21:37

by Dario Binacchi

[permalink] [raw]
Subject: Re: [RFC PATCH 5/5] can: slcan: send the listen-only command to the adapter

Hello Marc,

On Mon, Jul 18, 2022 at 12:22 PM Marc Kleine-Budde <[email protected]> wrote:
>
> The subject can be enhanced, as the listen-only command ist not send
> unconditionally. What about: "add support for listen-only mode"?

I like it.

>
> On 16.07.2022 19:00:07, Dario Binacchi wrote:
> > In case the bitrate has been set via ip tool, this patch changes the
> > driver to send the listen-only ("L\r") command to the adapter.
>
> ...but only of CAN_CTRLMODE_LISTENONLY is requested.
>
> What about:
>
> For non-legacy, i.e. ip based configuration, add support for listen-only
> mode. If listen-only is requested send a listen-only ("L\r") command
> instead of an open ("O\r") command to the adapter..

I agree with you. It's definitely clearer.

Thanks and regards,
Dario
>
> >
> > Signed-off-by: Dario Binacchi <[email protected]>
> >
> > ---
> >
> > drivers/net/can/slcan/slcan-core.c | 20 ++++++++++++++++----
> > 1 file changed, 16 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/net/can/slcan/slcan-core.c b/drivers/net/can/slcan/slcan-core.c
> > index 7a1540507ecd..d97dfeccbf9c 100644
> > --- a/drivers/net/can/slcan/slcan-core.c
> > +++ b/drivers/net/can/slcan/slcan-core.c
> > @@ -711,10 +711,21 @@ static int slcan_netdev_open(struct net_device *dev)
> > }
> > }
> >
> > - err = slcan_transmit_cmd(sl, "O\r");
> > - if (err) {
> > - netdev_err(dev, "failed to send open command 'O\\r'\n");
> > - goto cmd_transmit_failed;
> > + /* listen-only command overrides open command */
>
> I think this comment can be removed.
>
> > + if (sl->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
> > + err = slcan_transmit_cmd(sl, "L\r");
> > + if (err) {
> > + netdev_err(dev,
> > + "failed to send listen-only command 'L\\r'\n");
> > + goto cmd_transmit_failed;
> > + }
> > + } else {
> > + err = slcan_transmit_cmd(sl, "O\r");
> > + if (err) {
> > + netdev_err(dev,
> > + "failed to send open command 'O\\r'\n");
> > + goto cmd_transmit_failed;
> > + }
> > }
> > }
> >
> > @@ -801,6 +812,7 @@ static int slcan_open(struct tty_struct *tty)
> > /* Configure CAN metadata */
> > sl->can.bitrate_const = slcan_bitrate_const;
> > sl->can.bitrate_const_cnt = ARRAY_SIZE(slcan_bitrate_const);
> > + sl->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY;
> >
> > /* Configure netdev interface */
> > sl->dev = dev;
>
> Marc
>
> --
> Pengutronix e.K. | Marc Kleine-Budde |
> Embedded Linux | https://www.pengutronix.de |
> Vertretung West/Dortmund | Phone: +49-231-2826-924 |
> Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |



--

Dario Binacchi

Embedded Linux Developer

[email protected]

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
[email protected]

http://www.amarulasolutions.com

2022-07-25 06:45:21

by Dario Binacchi

[permalink] [raw]
Subject: Re: [RFC PATCH 2/5] can: slcan: remove legacy infrastructure

Hi Max,

First of all thank you for your review, it took me a while to get back
to you because I wanted to
do some analysis and tests regarding the code you suggested I change
and also last week
was very busy.

On Sun, Jul 17, 2022 at 11:38 PM Max Staudt <[email protected]> wrote:
>
> Hi Dario,
>
> This looks good, thank you for continuing to look after slcan!
>
> A few comments below.
>
>
>
> On Sat, 16 Jul 2022 19:00:04 +0200
> Dario Binacchi <[email protected]> wrote:
>
> [...]
>
>
> > @@ -68,7 +62,6 @@ MODULE_PARM_DESC(maxdev, "Maximum number of slcan interfaces");
> > SLC_STATE_BE_TXCNT_LEN)
> > struct slcan {
> > struct can_priv can;
> > - int magic;
> >
> > /* Various fields. */
> > struct tty_struct *tty; /* ptr to TTY structure */
> > @@ -84,17 +77,14 @@ struct slcan {
> > int xleft; /* bytes left in XMIT queue */
> >
> > unsigned long flags; /* Flag values/ mode etc */
> > -#define SLF_INUSE 0 /* Channel in use */
> > -#define SLF_ERROR 1 /* Parity, etc. error */
> > -#define SLF_XCMD 2 /* Command transmission */
> > +#define SLF_ERROR 0 /* Parity, etc. error */
> > +#define SLF_XCMD 1 /* Command transmission */
> > unsigned long cmd_flags; /* Command flags */
> > #define CF_ERR_RST 0 /* Reset errors on open */
> > wait_queue_head_t xcmd_wait; /* Wait queue for commands */
>
> I assume xcmd_wait() came in as part of the previous patch series?
>

Yes, correct.

>
> [...]
>
>
> > /* Send a can_frame to a TTY queue. */
> > @@ -652,25 +637,21 @@ static int slc_close(struct net_device *dev)
> > struct slcan *sl = netdev_priv(dev);
> > int err;
> >
> > - spin_lock_bh(&sl->lock);
> > - if (sl->tty) {
> > - if (sl->can.bittiming.bitrate &&
> > - sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
> > - spin_unlock_bh(&sl->lock);
> > - err = slcan_transmit_cmd(sl, "C\r");
> > - spin_lock_bh(&sl->lock);
> > - if (err)
> > - netdev_warn(dev,
> > - "failed to send close command 'C\\r'\n");
> > - }
> > -
> > - /* TTY discipline is running. */
> > - clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
> > + if (sl->can.bittiming.bitrate &&
> > + sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
> > + err = slcan_transmit_cmd(sl, "C\r");
> > + if (err)
> > + netdev_warn(dev,
> > + "failed to send close command 'C\\r'\n");
> > }
> > +
> > + /* TTY discipline is running. */
> > + clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
> > + flush_work(&sl->tx_work);
> > +
> > netif_stop_queue(dev);
> > sl->rcount = 0;
> > sl->xleft = 0;
>
> I suggest moving these two assignments to slc_open() - see below.
>
>
> [...]
>
>
> > @@ -883,72 +786,50 @@ static int slcan_open(struct tty_struct *tty)
> > if (!tty->ops->write)
> > return -EOPNOTSUPP;
> >
> > - /* RTnetlink lock is misused here to serialize concurrent
> > - * opens of slcan channels. There are better ways, but it is
> > - * the simplest one.
> > - */
> > - rtnl_lock();
> > + dev = alloc_candev(sizeof(*sl), 1);
> > + if (!dev)
> > + return -ENFILE;
> >
> > - /* Collect hanged up channels. */
> > - slc_sync();
> > + sl = netdev_priv(dev);
> >
> > - sl = tty->disc_data;
> > + /* Configure TTY interface */
> > + tty->receive_room = 65536; /* We don't flow control */
> > + sl->rcount = 0;
> > + sl->xleft = 0;
>
> I suggest moving the zeroing to slc_open() - i.e. to the netdev open
> function. As a bonus, you can then remove the same two assignments from
> slc_close() (see above). They are only used when netif_running(), with
> appropiate guards already in place as far as I can see.

I think it is better to keep the code as it is, since at the entry of
the netdev
open function, netif_running already returns true (it is set to true by the
calling function) and therefore it would be less safe to reset the
rcount and xleft
fields.

Thanks and regards,
Dario

>
>
> > + spin_lock_init(&sl->lock);
> > + INIT_WORK(&sl->tx_work, slcan_transmit);
> > + init_waitqueue_head(&sl->xcmd_wait);
> >
> > - err = -EEXIST;
> > - /* First make sure we're not already connected. */
> > - if (sl && sl->magic == SLCAN_MAGIC)
> > - goto err_exit;
> > + /* Configure CAN metadata */
> > + sl->can.bitrate_const = slcan_bitrate_const;
> > + sl->can.bitrate_const_cnt = ARRAY_SIZE(slcan_bitrate_const);
> >
> > - /* OK. Find a free SLCAN channel to use. */
> > - err = -ENFILE;
> > - sl = slc_alloc();
> > - if (!sl)
> > - goto err_exit;
> > + /* Configure netdev interface */
> > + sl->dev = dev;
> > + strscpy(dev->name, "slcan%d", sizeof(dev->name));
>
> The third parameter looks... unintentional :)
>
> What do the maintainers think of dropping the old "slcan" name, and
> just allowing this to be a normal canX device? These patches do bring
> it closer to that, after all. In this case, this name string magic
> could be dropped altogether.
>
>
> [...]
>
>
>
> This looks good to me overall.
>
> Thanks Dario!
>
>
> Max



--

Dario Binacchi

Embedded Linux Developer

[email protected]

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
[email protected]

http://www.amarulasolutions.com

2022-07-25 13:19:36

by Max Staudt

[permalink] [raw]
Subject: Re: [RFC PATCH 2/5] can: slcan: remove legacy infrastructure

On Mon, 25 Jul 2022 08:40:24 +0200
Dario Binacchi <[email protected]> wrote:

> > > @@ -883,72 +786,50 @@ static int slcan_open(struct tty_struct *tty)
> > > if (!tty->ops->write)
> > > return -EOPNOTSUPP;
> > >
> > > - /* RTnetlink lock is misused here to serialize concurrent
> > > - * opens of slcan channels. There are better ways, but it is
> > > - * the simplest one.
> > > - */
> > > - rtnl_lock();
> > > + dev = alloc_candev(sizeof(*sl), 1);
> > > + if (!dev)
> > > + return -ENFILE;
> > >
> > > - /* Collect hanged up channels. */
> > > - slc_sync();
> > > + sl = netdev_priv(dev);
> > >
> > > - sl = tty->disc_data;
> > > + /* Configure TTY interface */
> > > + tty->receive_room = 65536; /* We don't flow control */
> > > + sl->rcount = 0;
> > > + sl->xleft = 0;
> >
> > I suggest moving the zeroing to slc_open() - i.e. to the netdev open
> > function. As a bonus, you can then remove the same two assignments from
> > slc_close() (see above). They are only used when netif_running(), with
> > appropiate guards already in place as far as I can see.
>
> I think it is better to keep the code as it is, since at the entry of
> the netdev
> open function, netif_running already returns true (it is set to true by the
> calling function) and therefore it would be less safe to reset the
> rcount and xleft
> fields.

Wow, great catch!

I wonder why __LINK_STATE_START is set before ->ndo_open() is called...?


Since the drivers are similar, I've checked can327. It is unaffected,
because the counters are additionally guarded by a spinlock. Same in
slcan, where netdev_close() takes the spinlock to reset the counters.

So you *could* move them to netdev_open() *if* they are always guarded
by the slcan lock.

Or, leave it as it is, as it seems to be correct. Your choice :)


Thank you!

Max

2022-07-26 15:09:31

by Dario Binacchi

[permalink] [raw]
Subject: Re: [RFC PATCH 2/5] can: slcan: remove legacy infrastructure

Hi Max,

On Mon, Jul 25, 2022 at 3:09 PM Max Staudt <[email protected]> wrote:
>
> On Mon, 25 Jul 2022 08:40:24 +0200
> Dario Binacchi <[email protected]> wrote:
>
> > > > @@ -883,72 +786,50 @@ static int slcan_open(struct tty_struct *tty)
> > > > if (!tty->ops->write)
> > > > return -EOPNOTSUPP;
> > > >
> > > > - /* RTnetlink lock is misused here to serialize concurrent
> > > > - * opens of slcan channels. There are better ways, but it is
> > > > - * the simplest one.
> > > > - */
> > > > - rtnl_lock();
> > > > + dev = alloc_candev(sizeof(*sl), 1);
> > > > + if (!dev)
> > > > + return -ENFILE;
> > > >
> > > > - /* Collect hanged up channels. */
> > > > - slc_sync();
> > > > + sl = netdev_priv(dev);
> > > >
> > > > - sl = tty->disc_data;
> > > > + /* Configure TTY interface */
> > > > + tty->receive_room = 65536; /* We don't flow control */
> > > > + sl->rcount = 0;
> > > > + sl->xleft = 0;
> > >
> > > I suggest moving the zeroing to slc_open() - i.e. to the netdev open
> > > function. As a bonus, you can then remove the same two assignments from
> > > slc_close() (see above). They are only used when netif_running(), with
> > > appropiate guards already in place as far as I can see.
> >
> > I think it is better to keep the code as it is, since at the entry of
> > the netdev
> > open function, netif_running already returns true (it is set to true by the
> > calling function) and therefore it would be less safe to reset the
> > rcount and xleft
> > fields.
>
> Wow, great catch!
>
> I wonder why __LINK_STATE_START is set before ->ndo_open() is called...?
>
>
> Since the drivers are similar, I've checked can327. It is unaffected,
> because the counters are additionally guarded by a spinlock. Same in
> slcan, where netdev_close() takes the spinlock to reset the counters.
>
> So you *could* move them to netdev_open() *if* they are always guarded
> by the slcan lock.
>
> Or, leave it as it is, as it seems to be correct. Your choice :)

If possible I prefer not to use spin_lock. So I prefer to keep the code as is.
Thanks and regards,
Dario

>
>
> Thank you!
>
> Max



--

Dario Binacchi

Embedded Linux Developer

[email protected]

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
[email protected]

http://www.amarulasolutions.com