Skip to contentSkip to conversation

CAN: add SAE J1939 protocol

RFC Superseded Kurt Van Dijck 4 participants 30 messages Mar 14, 2011 – Apr 23, 2011 10 messages not in the archive

Subsystems (7)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover CAN: add SAE J1939 protocol

This series adds SAE J1939 support to the current net-next-2.6. For the rtnetlink operations to work, a 2.6.38-rc1 is required.

1/6: can: extend sockaddr_can to include j1939 members 2/6: can: add rtnetlink support
3/6: can: make struct proto const
4/6: j1939: initial import of SAE J1939
5/6: j1939: add documentation & MAINTAINERS 6/6: iproute2: use CAN & J1939

Still a lot of things probably need explanation...

Kind regards,
Kurt Van Dijck
EIA Electronics

#2 lore raw

RFC v3 1/6 can: extend sockaddr_can to include j1939 members

In reply to a message not in the archive

This patch prepares struct sockaddr_can for SAE J1939. The size of this structure increases. To stay binary compatible, the required_size macro is introduced for existing CAN protocols.

---

include/linux/can.h +19 -1
index d183333..9c2523c 100644
@@ -67,7 +67,8 @@ struct can_frame {
 #define CAN_TP20	4 /* VAG Transport Protocol v2.0 */
 #define CAN_MCNET	5 /* Bosch MCNet */
 #define CAN_ISOTP	6 /* ISO 15765-2 Transport Protocol */
-#define CAN_NPROTO	7
+#define CAN_J1939	7 /* SAE J1939 */
+#define CAN_NPROTO	8
 
 #define SOL_CAN_BASE 100
 
@@ -84,6 +85,23 @@ struct sockaddr_can {
 		/* transport protocol class address information (e.g. ISOTP) */
 		struct { canid_t rx_id, tx_id; } tp;
 
+		/* J1939 address information */
+		struct {
+			/* 8 byte name when using dynamic addressing */
+			__u64 name;
+			/*
+			 * pgn:
+			 * 8bit: PS in PDU2 case, else 0
+			 * 8bit: PF
+			 * 1bit: DP
+			 * 1bit: reserved
+			 */
+			__u32 pgn;
+
+			/* 1byte address */
+			__u8 addr;
+		} j1939;
+
 		/* reserved for future CAN protocols address information */
 	} can_addr;
 };
include/linux/can/core.h +9 -0
index 6c507be..653c33e 100644
@@ -42,6 +42,15 @@ struct can_proto {
 	struct proto     *prot;
 };
 
+/*
+ * required_size
+ * macro to find the minimum size of a struct
+ * that includes a requested member
+ */
+#define required_size(member, struct_type) \
+	(offsetof(typeof(struct_type), member) + \
+	 sizeof(((typeof(struct_type) *)(0))->member))
+
 /* function prototypes for the CAN networklayer core (af_can.c) */
 
 extern int  can_proto_register(struct can_proto *cp);
net/can/bcm.c +2 -2
index 092dc88..b286e45 100644
@@ -1256,7 +1256,7 @@ static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
 		struct sockaddr_can *addr =
 			(struct sockaddr_can *)msg->msg_name;
 
-		if (msg->msg_namelen < sizeof(*addr))
+		if (msg->msg_namelen < required_size(can_ifindex, *addr))
 			return -EINVAL;
 
 		if (addr->can_family != AF_CAN)
@@ -1493,7 +1493,7 @@ static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
 	struct sock *sk = sock->sk;
 	struct bcm_sock *bo = bcm_sk(sk);
 
-	if (len < sizeof(*addr))
+	if (len < required_size(can_ifindex, *addr))
 		return -EINVAL;
 
 	if (bo->bound)
net/can/raw.c +2 -2
index 883e9d7..a5f1f41 100644
@@ -350,7 +350,7 @@ static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
 	int err = 0;
 	int notify_enetdown = 0;
 
-	if (len < sizeof(*addr))
+	if (len < required_size(can_ifindex, *addr))
 		return -EINVAL;
 
 	lock_sock(sk);
@@ -649,7 +649,7 @@ static int raw_sendmsg(struct kiocb *iocb, struct socket *sock,
 		struct sockaddr_can *addr =
 			(struct sockaddr_can *)msg->msg_name;
 
-		if (msg->msg_namelen < sizeof(*addr))
+		if (msg->msg_namelen < required_size(can_ifindex, *addr))
 			return -EINVAL;
 
 		if (addr->can_family != AF_CAN)
#5 lore raw

RFC v3 2/6 can: add rtnetlink support

In reply to a message not in the archive

This patch adds rtnetlink support for AF_CAN. This support is really a multiplexer towards the different CAN protocols.

---

include/linux/can/core.h +25 -2
index 653c33e..430c446 100644
@@ -18,6 +18,7 @@
 #include <linux/can.h>
 #include <linux/skbuff.h>
 #include <linux/netdevice.h>
+#include <net/rtnetlink.h>
 
 #define CAN_VERSION "20090105"
 
@@ -40,6 +41,28 @@ struct can_proto {
 	int              protocol;
 	struct proto_ops *ops;
 	struct proto     *prot;
+
+	const struct rtnl_af_ops *rtnl_link_ops;
+	/*
+	 * hooks for rtnl hooks
+	 * for the *dump* functions, cb->args[0] is reserved
+	 * for use by af_can.c, so keep your fingers off.
+	 */
+	rtnl_doit_func rtnl_new_addr;
+	rtnl_doit_func rtnl_del_addr;
+	rtnl_dumpit_func rtnl_dump_addr;
+};
+
+/*
+ * this is quite a dirty hack:
+ * reuse the second byte of a rtnetlink msg
+ * to indicate the precise protocol.
+ * The major problem is that is may conflict
+ * with the prefixlen in struct ifaddrmsg.
+ */
+struct rtgencanmsg {
+	unsigned char		rtgen_family;
+	unsigned char		can_protocol;
 };
 
 /*
@@ -53,8 +76,8 @@ struct can_proto {
 
 /* function prototypes for the CAN networklayer core (af_can.c) */
 
-extern int  can_proto_register(struct can_proto *cp);
-extern void can_proto_unregister(struct can_proto *cp);
+extern int  can_proto_register(const struct can_proto *cp);
+extern void can_proto_unregister(const struct can_proto *cp);
 
 extern int  can_rx_register(struct net_device *dev, canid_t can_id,
 			    canid_t mask,
net/can/af_can.c +264 -34

375 lines not on this page · raw message

net/can/bcm.c +1 -1
index b286e45..ac1961d 100644
@@ -1596,7 +1596,7 @@ static struct proto bcm_proto __read_mostly = {
 	.init       = bcm_init,
 };
 
-static struct can_proto bcm_can_proto __read_mostly = {
+static const struct can_proto bcm_can_proto = {
 	.type       = SOCK_DGRAM,
 	.protocol   = CAN_BCM,
 	.ops        = &bcm_ops,
net/can/raw.c +1 -1
index a5f1f41..9ad3dfc 100644
@@ -769,7 +769,7 @@ static struct proto raw_proto __read_mostly = {
 	.init       = raw_init,
 };
 
-static struct can_proto raw_can_proto __read_mostly = {
+static const struct can_proto raw_can_proto = {
 	.type       = SOCK_RAW,
 	.protocol   = CAN_RAW,
 	.ops        = &raw_ops,
#6 lore raw

RFC v3 3/6 can: make struct proto const

In reply to a message not in the archive

can_ioctl is the only reason for struct proto to be non-const. script/check-patch.pl suggests struct proto be const. This patch performs the necessary change.

---

include/linux/can/core.h +3 -1
index 430c446..0767cc6 100644
@@ -39,7 +39,7 @@
 struct can_proto {
 	int              type;
 	int              protocol;
-	struct proto_ops *ops;
+	const struct proto_ops *ops;
 	struct proto     *prot;
 
 	const struct rtnl_af_ops *rtnl_link_ops;
@@ -78,6 +78,8 @@ struct rtgencanmsg {
 
 extern int  can_proto_register(const struct can_proto *cp);
 extern void can_proto_unregister(const struct can_proto *cp);
+extern int can_sock_ioctl(struct socket *sock, unsigned int cmd,
+		unsigned long arg);
 
 extern int  can_rx_register(struct net_device *dev, canid_t can_id,
 			    canid_t mask,
net/can/af_can.c +2 -5
index db59c6e..c1f8c05 100644
@@ -139,7 +139,7 @@ static inline void can_put_proto(const struct can_proto *cp)
  * af_can socket functions
  */
 
-static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
+int can_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 {
 	struct sock *sk = sock->sk;
 
@@ -152,6 +152,7 @@ static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 		return -ENOIOCTLCMD;
 	}
 }
+EXPORT_SYMBOL(can_sock_ioctl);
 
 static void can_sock_destruct(struct sock *sk)
 {
@@ -720,10 +721,6 @@ int can_proto_register(const struct can_proto *cp)
 		err = -EBUSY;
 	} else {
 		proto_tab[proto] = cp;
-
-		/* use generic ioctl function if not defined by module */
-		if (!cp->ops->ioctl)
-			cp->ops->ioctl = can_ioctl;
 	}
 	spin_unlock(&proto_tab_lock);
 
net/can/bcm.c +2 -2
index ac1961d..fd89542 100644
@@ -1569,7 +1569,7 @@ static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
 	return size;
 }
 
-static struct proto_ops bcm_ops __read_mostly = {
+static const struct proto_ops bcm_ops = {
 	.family        = PF_CAN,
 	.release       = bcm_release,
 	.bind          = sock_no_bind,
@@ -1578,7 +1578,7 @@ static struct proto_ops bcm_ops __read_mostly = {
 	.accept        = sock_no_accept,
 	.getname       = sock_no_getname,
 	.poll          = datagram_poll,
-	.ioctl         = NULL,		/* use can_ioctl() from af_can.c */
+	.ioctl         = can_sock_ioctl,
 	.listen        = sock_no_listen,
 	.shutdown      = sock_no_shutdown,
 	.setsockopt    = sock_no_setsockopt,
net/can/raw.c +2 -2
index 9ad3dfc..8581596 100644
@@ -742,7 +742,7 @@ static int raw_recvmsg(struct kiocb *iocb, struct socket *sock,
 	return size;
 }
 
-static struct proto_ops raw_ops __read_mostly = {
+static const struct proto_ops raw_ops = {
 	.family        = PF_CAN,
 	.release       = raw_release,
 	.bind          = raw_bind,
@@ -751,7 +751,7 @@ static struct proto_ops raw_ops __read_mostly = {
 	.accept        = sock_no_accept,
 	.getname       = raw_getname,
 	.poll          = datagram_poll,
-	.ioctl         = NULL,		/* use can_ioctl() from af_can.c */
+	.ioctl         = can_sock_ioctl,
 	.listen        = sock_no_listen,
 	.shutdown      = sock_no_shutdown,
 	.setsockopt    = raw_setsockopt,
#15 lore raw

RFC v3 4/6 j1939: initial import of SAE J1939

In reply to a message not in the archive

This patch contains the main SAE J1939 stack.

scripts/checkpatch.pl reports 3 errors like:
ERROR: return is not a function, parentheses are not required #3819: FILE: net/can/j1939/transport.c:246:
+ return (dat[2] << 8) + (dat[1] << 0);

I don't see how to fix the checkpatch.pl error _and_ improving the code.

---

include/linux/can/Kbuild +1 -0
index 8cb05aa..0364eef 100644
@@ -2,3 +2,4 @@ header-y += raw.h
 header-y += bcm.h
 header-y += error.h
 header-y += netlink.h
+header-y += j1939.h
include/linux/can/j1939.h new file +100 -0

100 lines not on this page · raw message

net/can/Kconfig +1 -0
index 89395b2..7feb58c 100644
@@ -40,5 +40,6 @@ config CAN_BCM
 	  CAN messages are used on the bus (e.g. in automotive environments).
 	  To use the Broadcast Manager, use AF_CAN with protocol CAN_BCM.
 
+source "net/can/j1939/Kconfig"
 
 source "drivers/net/can/Kconfig"
net/can/Makefile +2 -0
index 2d3894b..953d851 100644
@@ -10,3 +10,5 @@ can-raw-y		:= raw.o
 
 obj-$(CONFIG_CAN_BCM)	+= can-bcm.o
 can-bcm-y		:= bcm.o
+
+obj-$(CONFIG_CAN_J1939)	+= j1939/
net/can/j1939/Kconfig new file +22 -0
new file mode 100644
index 0000000..74d2a86
@@ -0,0 +1,22 @@
+#
+# SAE J1939 network layer core configuration
+#
+
+config CAN_J1939
+	tristate "SAE J1939"
+	depends on CAN
+	---help---
+	  SAE J1939
+	  Say Y to have in-kernel support for j1939 socket type. This
+	  allows communication according to SAE j1939.
+	  The relevant parts in kernel are
+	  SAE j1939-21 (datalink & transport protocol)
+	  & SAE j1939-81 (network management).
+
+config CAN_J1939_DEBUG
+	bool "debug SAE J1939"
+	depends on CAN_J1939
+	default n
+	---help---
+	  Say Y to add extra debug code (via printk) in the j1939 stack
+
net/can/j1939/Makefile new file +14 -0
new file mode 100644
index 0000000..7ca2fc9
@@ -0,0 +1,14 @@
+
+obj-$(CONFIG_CAN_J1939)	+= can-j1939.o
+
+can-j1939-objs := main.o \
+	proc.o bus.o \
+	rtnl.o \
+	socket.o \
+	address-claim.o transport.o \
+	promisc.o filter.o
+
+ifeq ($(CONFIG_CAN_J1939_DEBUG),y)
+	EXTRA_CFLAGS += -DDEBUG
+endif
+
net/can/j1939/address-claim.c new file +527 -0

527 lines not on this page · raw message

net/can/j1939/bus.c new file +533 -0

533 lines not on this page · raw message

net/can/j1939/filter.c new file +76 -0

76 lines not on this page · raw message

net/can/j1939/j1939-priv.h new file +287 -0

287 lines not on this page · raw message

net/can/j1939/main.c new file +454 -0

454 lines not on this page · raw message

net/can/j1939/proc.c new file +104 -0

104 lines not on this page · raw message

net/can/j1939/promisc.c new file +43 -0

43 lines not on this page · raw message

net/can/j1939/rtnl.c new file +314 -0

314 lines not on this page · raw message

net/can/j1939/socket.c new file +983 -0

511 lines not on this page · raw message

net/can/j1939/transport.c new file +1481 -0

1,481 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.

#16 lore raw

RFC v3 5/6 j1939: add documentation and MAINTAINERS

In reply to a message not in the archive

This patch adds the documentation and MAINTAINERS. This is seperated from 4/6 for size constraints on berlios' SocketCAN mailing list.

---

Documentation/networking/j1939.txt new file +532 -0
new file mode 100644
index 0000000..690e0a0
@@ -0,0 +1,532 @@
+============================================================================
+
+j1939.txt
+
+Readme file for the J1939 Protocol
+
+This file contains
+
+  1 Overview / What is j1939
+    1.1 specifications used
+
+  2 Motivation
+
+  3 J1939 concepts
+    3.1 socket type
+    3.2 addressing
+    3.3 priority
+    3.4 PGN
+    3.5 filtering
+    3.6 destinations with dynamic address
+
+  4 How to use J1939
+    4.1 rtnetlink interface
+    4.2 API calls
+        4.2.1 Message flags during sendmsg
+    4.3 dynamic addressing
+
+  5 socket options
+    5.1 SO_J1939_FILTER
+    5.2 SO_J1939_PROMISC
+    5.3 SO_J1939_RECV_OWN
+    5.4 SO_J1939_RECV_DEST
+    5.5 SO_J1939_RECV_PRIO
+    5.6 SO_J1939_SEND_PRIO
+    5.7 SO_J1939_DEST_MASK
+
+  6 can-j1939 procfs interface
+    6.1 /proc/net/can-j1939/ecu
+    6.2 /proc/net/can-j1939/filter
+    6.3 /proc/net/can-j1939/sock
+    6.4 /proc/net/can-j1939/transport
+
+  7 can-j1939 SYSCTL
+    7.1 /proc/sys/net/can-j1939/transport_max_payload_in_bytes
+    7.2 /proc/sys/net/can-j1939/transport_cts_nr_of_frames
+    7.3 /proc/sys/net/can-j1939/transport_tx_retry_ms
+
+  8 Credits
+
+============================================================================
+
+1. Introduction
+--------------------------------
+
+  SAE J1939 defines a higher layer protocol on CAN. It implements a more
+  sophisticated addressing scheme and extends the maximum packet size above
+  8 bytes. Several derived specifications exists, which differ from the
+  original j1939 on the application level, like MilCAN, NMEA2000 and
+  especially ISO-11783 (ISOBUS). This last one specifies the so-called ETP
+  (Extended Transport Protocol) which is has been included in this
+  implementation. This inclusion results in a maximum packet size of
+  ((2^24)-1)*7 bytes
+
+
+1.1 specifications used
+
+  SAE J1939-21 : data link layer
+  SAE J1939-81 : network management
+  ISO 11783-6  : Virtual Terminal (Extended Transport Protocol)
+
+
+2. Motivation
+--------------------------------
+
+  Given the fact there's something like SocketCAN with an API similar to BSD
+  sockets, we found some reasons to justify a kernel implementation for the
+  addressing and transport methods used by J1939.
+
+  * addressing:
+    When a process on an ECU communicates via j1939, it should not necessarily
+    know its source address. Although at least 1 process per ECU should know
+    the source address. Other processes should be able to reuse that address.
+    This way, address parameters for different processes cooperating for the
+    same ECU, are not duplicated.
+    This way of working is closely related to the unix concept where programs
+    do just 1 thing, and do it well.
+
+  * dynamic addressing:
+    Address Claiming in J1939 is time critical. Furthermore data transport
+    should be handled properly during the address negotiation. Putting these
+    functionality in the kernel eliminates this functionality as a requirement
+    for _every_ userspace process that communicates via J1939. This results in
+    a consistent J1939 bus with proper addressing.
+
+  * transport:
+    Both TP & ETP reuse some PGN's to relay big packets over them. Different
+    processes may thus use the same TP & ETP PGN's without actually knowing it.
+    The individual TP & ETP sessions _must_ be serialized (synchronised)
+    between different processes. The kernel solves this problem properly, and
+    eliminates the serialisation (synchronisation) as a requirement for
+    _every_ userspace process that communicates via J1939.
+
+  J1939 defines some other features (relaying, gateway, Fast Packet transport,
+  ...). In-kernel code for these would not contribute to protocol stability.
+  Therefore, these parts are left to userspace.
+
+  The j1939 sockets operate on CAN network devices (see SocketCAN). Any j1939
+  userspace library operating on CAN raw sockets will still operate properly.
+  Since such library does not communicate with the in-kernel implementation,
+  care must be taken that these 2 do not interfere. In practice, this means
+  they cannot share ECU addresses. A single ECU (or virtual ECU) address is
+  used by the library exclusively, or by the in-kernel system exclusively.
+
+
+3. J1939 concepts
+--------------------------------
+
+3.1 PGN
+
+  The PGN (Parameter Group Number) is a number to identify a packet. The PGN
+  is composed as follows:
+   1 bit  : Reserved Bit
+   1 bit  : Data Page
+   8 bits : PF (PDU Format)
+   8 bits : PS (PDU Specific)
+
+  In J1939-21, distinction is made between PDU1 Format (where PF < 240) and
+  PDU2 Format (where PF >= 240). Furthermore, when using PDU2 Format, the
+  PS-field contains a so-called Group Extension, which is part of the PGN.
+  When using PDU2 Format, the Group Extension is set in the PS-field.
+
+  On the other hand, when using PDU1 Format, the PS-field contains a so-called
+  Destination Address, which is _not_ part of the PGN. When communicating a
+  PGN from userspace to kernel (or visa versa) and PDU2 Format is used, the
+  PS-field of the PGN shall be set to zero. The Destination Address shall be
+  set elsewhere.
+
+  Regarding PGN mapping to 29-bit CAN identifier, the Destination Address
+  shall be get/set from/to the apropriate bits of the identifier by the kernel.
+
+
+3.2 addressing
+
+  Both static and dynamic addressing methods can be used.
+
+  For static addresses, no extra checks are made by the kernel, and provided
+  addresses are considered right. This responsibility is for the OEM or system
+  integrator.
+
+  For dynamic addressing, so-called Address Claiming, extra support is forseen
+  in the kernel. In J1939 any ECU is known by it's 64-bit NAME. At the moment
+  of succesfull address claim, the kernel keeps track of both NAME and source
+  address being claimed. This serves as a base for filter schemes. By default,
+  packets with a destination that is not locally, will be rejected soon after
+  reception.
+
+  Mixed mode packets (from a static to a dynamic address or vice versa) are
+  allowed. The BSD sockets define seperate API calls for getting/setting the
+  local & remote address and are applicable for J1939 sockets.
+
+
+3.3 Filtering
+
+  Similar to SocketCAN, j1939 defines filters per socket that a user can set
+  in order to receive a subset of the j1939 traffic. Filtering can base on
+  * SA
+  * NAME
+  * PGN
+
+  There is a semantic difference with SocketCAN with regard to filtering.
+  When multiple filters are in place for a single socket, and a packet comes
+  in that matches several of those filters, the packet is only received once
+  for that socket.
+  The rationale behind this difference originates in the filter capabilities.
+  Where SocketCAN filters on only 1 orthogonal (can id), J1939 can filter
+  on 3 orthogonal properties (sa, name, pgn).
+
+  When a filter on the SA is set, j1939 traffic with a matching SA, but with
+  its NAME set (aka having claimed SA successfully) will match, although
+  the filter would not match its NAME.
+
+  Filtering on priority is _not_ supported.
+
+
+4. How to use J1939
+--------------------------------
+
+4.1 rtnetlink interface
+
+  Per default j1939 is not active. Specifying can_ifindex != 0 in bind(2)
+  or connect(2) needs an active j1939 on that interface. You must have done
+  $ ip link set canX j1939 on
+  on that interface.
+
+  $ ip link set canX j1939 down
+  disables j1939 on canX.
+
+  Assigning addresses is done via
+  $ ip addr add dev canX j1939 0xXX
+  statically or
+  $ ip addr add dev canX j1939 name 0xXX
+  dynamically. In the latter case, address claiming must take place
+  before other traffic can leave.
+
+  Removing addresses is done similarly via
+  $ ip addr del dev canX j1939 0xXX
+  $ ip addr del dev canX j1939 name 0xXX
+
+  A static address cannot be assigned together with a 64bit name.
+
+4.2 API calls
+
+  Like TCP/IP and CAN, you first need to open a socket for communicating over a
+  CAN network. To use j1939, include <include/linux/j1939.h>. From there,
+  <include/linux/can.h> will be included too.
+  To open a socket, you would write
+
+    s = socket(PF_CAN, SOCK_DGRAM, CAN_J1939);
+
+  J1939 does use SOCK_DGRAM sockets. In the j1939 specification, connections are
+  mentioned in the context of transport protocol sessions. These still deliver
+  packets to the other end (using several CAN packets).
+  SOCK_STREAM is never appropriate.
+
+  After the successful creation of the socket, you would normally use the
+  bind(2) and/or connect(2) system call to bind the socket to a CAN interface
+  (which is different from TCP/IP due to different addressing) After binding
+  and/or connecting the socket, you can read(2) and write(2) from/to the socket
+  or use send(2), sendto(2), sendmsg(2) and the recv* counterpart operations on
+  the socket as usual. There are also J1939 specific socket options described
+  below.
+
+  In order to send data, a bind(2) must have succeeded. bind(2) assigns a local
+  address to a socket. For this to succeed, you can only choose addresses
+  that have been assigned earlier (see 4.1). When an empty address is assigned
+  (ie. SA=0xff && name=0), a default is taken for the device that is bound to.
+
+  Different from CAN is that the payload data is just the data that get send,
+  without it's header info. The header info is derived from the sockaddr
+  supplied to bind(2), connect(2), sendto(2) and recvfrom(2). A write(2) with
+  size 4 will result in a packet with 4 bytes.
+
+  The sockaddr structure has extensions for use with j1939 as specified below:
+      struct sockaddr_can {
+         sa_family_t can_family;
+         int         can_ifindex;
+         union {
+            struct {
+               __u64 name;
+               __u32 pgn;
+               __u8  addr;
+            } j1939;
+         } can_addr;
+      }
+
+  can_family & can_ifindex serve the same purpose as for other SocketCAN sockets.
+
+  can_addr.j1939.pgn specifies the PGN (max 0x3ffff). Individual bits are
+  specified above.
+
+  can_addr.j1939.name contains the 64-bit J1939 NAME.
+
+  can_addr.j1939.addr contains the source address.
+
+  When sending data, the source address is applied as follows: If
+  can_addr.j1939.name != 0 the NAME is looked up by the kernel and the
+  corresponding Source Address is used. If can_addr.j1939.name == 0,
+  can_addr.j1939.addr is used.
+
+  After a bind(2), the local address is assigned, i.e. the source address.
+  After a connect(2), the remote address is assigned, i.e. the destination
+  address.
+
+  Both write(2) and send(2) will send a packet with local address from bind,
+  remote address from connect(2). When the address was not set, a broadcast is
+  sent. The PGN is used from bind(2) or overruled with sendto(2), which will
+  override the destination address when valid, and the PGN when valid.
+
+  Both read(2) and recv(2) will receive packets matching the sockets filters.
+  recvfrom(2) will receive these packets with originator's address.
+
+  When creating a socket, reasonable defaults have been set. Some options can be
+  modified with setsockopt(2) & getsockopt(2).
+
+4.2.1 Message flags during sendmsg
+
+  send(2), sendto(2) and sendmsg(2) take a 'flags' argument. J1939 interpretes
+  these flags during outgoing traffic:
+
+  * MSG_DONTWAIT determines nonblocking operation. When a packet must wait for
+    any reason, -EAGAIN is returned.
+
+  * MSG_SYN
+    Packets flagged with MSG_SYN will wait for all pending packets on a socket
+    to be sent before trying to send. This means that if a socket just started
+    a Transport Protocol session, a packet with MSG_SYN will wait for that
+    session to complete before proceeding.
+    Traffic without MSG_SYN (on that very same socket) will still continue.
+
+4.3 Dynamic Addressing
+
+  Distinction has to be made in and using the claimed address and doing an
+  address claim. To use an already claimed address, one has to fill in the
+  j1939.name member and provide it to bind(2). If the name had claimed an
+  address earlier, all further PGN's being sent will use that address. And the
+  j1939.addr member will be ignored.
+
+  An exception on this is pgn 0x0ee00. This is the "Address Claim/Cannot Claim
+  Address" message and when the kernel will use the j1939.addr member for that
+  pgn if necessary.
+
+  To claim an address, bind(2) with:
+  j1939.pgn  set to 0x0ee00
+  j1939.addr set to the desired Source Address.
+  j1939.name set to the NAME you want the Source Address to claim to.
+
+  Afterwards do a write(2) with data set to the NAME (Little Endian). If the
+  NAME provided, does not match the j1939.name provided to bind(2), EPROTO
+  will be returned. One might use sendto(2) also to send the Addres Claim. In
+  that case, the j1939.addr member must be set to the broadcast address (255)
+  and the j1939.pgn must be set to 0x0ee00. If This combination is not given,
+  EPROTO is returned.
+
+  If no-one else contest the address claim within 250ms after transmission, the
+  kernel marks the NAME-SA assignment as valid. The valid assignment will be
+  kept, among other valid NAME-SA assignments. From that point, any socket
+  bound to the NAME can send packets.
+
+  If another ECU claims the address, the kernel will mark the NAME-SA expired.
+  No socket bound to the NAME can send packets (other than address claims).
+  To claim another address, some socket bound to NAME, must bind(2) again,
+  but with only j1939.addr changed to the new SA, and must then send a
+  valid address claim packet. This restarts the state machine in the kernel
+  (and any other participant on the bus) for this NAME.
+
+
+5 Socket Options
+--------------------------------
+
+  j1939 sockets have some options that are configurable via setsockopt(2).
+  Each of those options is initialized with a reasonable default.
+
+
+5.1 SO_J1939_FILTER
+
+  As mentioned above, J1939 supports filtering in both NAME, Source Address
+  and PGN. All members must match.
+
+   struct j1939_filter filter = {
+      .name         = ...
+      .name_mask   = ...
+      .addr         = ...
+      .addr_mask   = ...
+      .pgn         = ...
+      .pgn_mask   = ...
+   }
+
+    setsockopt(s, SOL_CAN_J1939, SO_J1939_FILTER, &filter, sizeof(filter));
+
+
+5.2 SO_J1939_PROMISC
+
+  When set, j1939 will receive all packets, not just those with a destination
+  on the local system.
+  default off.
+
+    int promisc = 1; /* 0 = disabled (default), 1 = enabled */
+
+    setsockopt(s, SOL_CAN_J1939, SO_J1939_PROMISC, &promisc, sizeof(promisc));
+
+
+5.3 SO_J1939_RECV_OWN
+
+  All the sent j1939 packets are looped back in the system.
+  The reception of the j1939 packets on the same socket that was
+  sending the j1939 packet is assumed to be unwanted and therefore
+  disabled by default. This default behaviour may be changed on
+  demand:
+
+    int recv_own_msgs = 1; /* 0 = disabled (default), 1 = enabled */
+
+    setsockopt(s, SOL_CAN_J1939, SO_J1939_RECV_OWN,
+               &recv_own_msgs, sizeof(recv_own_msgs));
+
+
+5.4 SO_J1939_RECV_DEST
+
+  Received j1939 packets that make their way up to the socket, had a destination
+  address matching the socket's local address. This can have several reasons:
+  - broadcast packet.
+  - destination spec matches the local address
+  - destination spec matches _a_ local address on the system, and the socket
+    had no local address defined.
+  - SO_J1939_PROMISC was set
+  If the user is interested in the original destination spec, SO_J1939_RECV_DEST
+  can be changed to 1 to receive the destination spec with each packet.
+  The destination is attached to the msghdr in the recvmsg(2) call.
+  It can be extracted using cmsg(3) macros, with
+  cmsg_level == SOL_J1939 && cmsg_type == SCM_J1939_DEST.
+  the returned data is a struct sockaddr_can, with the regular j1939 fields
+  stuffed. Mind that PGN is the same as the PGN in sockaddr after recvfrom.
+
+
+5.5 SO_J1939_RECV_PRIO
+
+  As stated earlier, the priority field is stripped very soon. In order to
+  allow retreiving the packet's priority, SO_J1939_RECV_PRIO can be set to 1.
+
+  As a result, an extra int will be attached during recvmsg(2), similar
+  as in SO_J1939_RECV_DEST, but with cmsg_type == SCM_J1939_PRIO
+
+6.6 SO_J1939_SEND_PRIO
+
+  To set the priority field for outgoing packets, the SO_J1939_SEND_PRIO can
+  be changed. This int field specifies the priority that will be used.
+  j1939 defines a priority between 0 and 7 inclusive,
+  with 7 the lowest priority.
+  Per default, the priority is set to 6 (conforming J1939).
+  This priority socket option operates on the same value that is modified
+  with
+
+    setsockopt(s, SOL_SOCKET, SO_PRIORITY, &pri, sizeof(pri))
+
+  socketoption, with a difference that SOL_SOCKET/SO_PRIORITY is defined with
+  0 the lowest priority. SOL_CAN_J1939/SO_J1939_SEND_PRIO inverts this value
+  for you.
+
+5.7 SO_J1939_DEST_MASK
+
+  When a destination is specified by its name (and thus using dynamic addressing),
+  and such name should be unique amongst the world, it may be hard to predict the
+  name of eg. a gearbox controller on the bus, although its type and manufacturer
+  are know. This is because the serial number is part of the name.
+  To simplify specifying a destination, a per-socket destination mask is provided
+  that is activated whenever a destination name is wanted. Any bits cleared in
+  this mask are ignored during the lookup. As a result, more than 1 ECU may match
+  this name/mask pair. In all cases, the first match is used.
+  The API is thus capable of specifying a name for eg. the gearbox controller,
+  without knowing its serial number.
+  This mask can mask out any part in the name. Note there's only 1 mask per socket.
+
+  this mask is default set to mask the serial number.
+
+  when can_addr.j1939.name is used for destination in outgoing packets
+  (see bind(2), sendto(2)), the exact name is often not known due to serial
+  numbers in it.
+  Therefore, SO_J1939_DEST_MASK sets an uint64_t mask that will be used
+  for resolving these names. Only the bits set to 1 in the mask will be
+  evaluated for find the destination name.
+  Per default, the mask is set to mask out the serial number
+  (0xffffffffffe00000ULL)
+
+  to mask out only the manufacturer code (bits 21-31), do
+
+    uint64_t mask = 0xffffffff001fffffULL;
+
+    setsockopt(s, SOL_CAN_J1939, SO_J1939_DEST_MASK, &mask, sizeof(mask));
+
+
+6. /proc/net/can-j1939 Interface.
+--------------------------------
+
+  Files giving you a view on the in-kernel operation of J1939 are located at:
+  /proc/net/j1939.
+
+6.1 /proc/net/can-j1939/ecu
+
+  This file gives an overview of the known ECU's to the kernel.
+  - iface : network interface they operate on.
+  - SA : current address.
+  - name : 64bit NAME
+  - flags : 'L' = local, 'R' = remote
+
+6.2 /proc/net/can-j1939/filter
+
+6.3 /proc/net/can-j1939/sock
+
+  This file gives a list of all j1939 sockets currently open.
+  - iface : network interface
+  - flags :
+    'b' : bound
+    'c' : connected
+    'P' : PROMISC
+    'o' : RECV_OWN
+    'd' : RECV_DEST
+    'p' : RECV_PRIO
+  - local: [NAME],SA
+  - remote: [NAME]/MASK,DA
+  - pgn : PGN
+  - prio : priority
+  - pending : # packets pending (see MSG_SYN on 4.2.1)
+
+6.4 /proc/net/can-j1939/transport
+
+  This file shows a list of pending transport sessions
+  - iface
+  - src : XX (addr) or XXXXXXXXXXXXXXXX (name)
+  - dst : XX or XXXXXXXXXXXXXXXX or '-' (broadcast)
+  - pgn :
+  - done/total : current # transferred bytes / total
+
+
+7. /proc/sys/net/can-j1939 - SYSCTL
+--------------------------------
+
+  Via these sysctl files, some parameters of the j1939 module can be tuned.
+
+7.1 /proc/sys/net/can-j1939/transport_max_payload_in_bytes [int]
+
+  Is the maximum packet size to accept on both transmit & receive side.
+  Bigger packets will be rejected (local sender), aborted (local receiver)
+  or ignored (broadcasts & remote recievers in PROMISC).
+
+7.2 /proc/sys/net/can-j1939/transport_cts_nr_frames [int]
+
+  Controls the number of packets to allow between consecutive CTS frames
+  (default 255).
+  This number is communicated within the CTS frame from receiver to transmitter.
+  Setting this has effect on received transport sessions only.
+
+7.3 /proc/sys/net/can-j1939/transport_tx_retry_ms [int]
+
+  Controls how many time to wait before retrying to send an individual TP
+  flow or data packet after transmission failure (default 20).
+
+
+8. Credits
+--------------------------------
+
+  Kurt Van Dijck (j1939 core, transport protocol, API)
+  Pieter Beyens (j1939 core, address claiming)
+
MAINTAINERS +8 -0
index 75760e7..a7d7c17 100644
@@ -1631,6 +1631,14 @@ F:	include/linux/can/error.h
 F:	include/linux/can/netlink.h
 F:	include/linux/can/platform/
 
+CAN-J1939 NETWORK LAYER
+M:	Kurt Van Dijck <kurt.van.dijck-/[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	net/can/j1939/
+F:	include/linux/can/j1939.h
+
 CELL BROADBAND ENGINE ARCHITECTURE
 M:	Arnd Bergmann <[email protected]>
 L:	linuxppc-dev-uLR06cmDAlY/[email protected]
#38 lore raw

RFC v3 6/6 iproute2: add CAN and J1939 rtnetlink support

In reply to a message not in the archive

Add iproute2 support for CAN and J1939 rtnetlink.

---

Makefile +4 -1
index c03d74c..efd06de 100644
@@ -27,9 +27,12 @@ ADDLIB+=dnet_ntop.o dnet_pton.o
 #options for ipx
 ADDLIB+=ipx_ntop.o ipx_pton.o
 
+#options for j1939
+ADDLIB+=j1939.o
+
 CC = gcc
 HOSTCC = gcc
-CCOPTS = -D_GNU_SOURCE -O2 -Wstrict-prototypes -Wall
+CCOPTS = -D_GNU_SOURCE -Wstrict-prototypes -Wall
 CFLAGS = $(CCOPTS) -I../include $(DEFINES)
 YACCFLAGS = -d -t -v
 
include/linux/can.h new file +129 -0

129 lines not on this page · raw message

include/linux/can/j1939.h new file +93 -0

93 lines not on this page · raw message

include/utils.h +18 -1
index 3da6998..b6344d5 100644
@@ -95,8 +95,15 @@ extern __u8* hexstring_a2n(const char *str, __u8 *buf, int blen);
 
 extern const char *format_host(int af, int len, const void *addr,
 			       char *buf, int buflen);
-extern const char *rt_addr_n2a(int af, int len, const void *addr,
+/* 'address with protocol' n2a */
+extern const char *rt_addrpr_n2a(int af, int protocol, int len, const void *addr,
 			       char *buf, int buflen);
+static inline const char *rt_addr_n2a(int af, int len, const void *addr,
+			       char *buf, int buflen)
+{
+	return rt_addrpr_n2a(af, 0, len, addr, buf, buflen);
+
+}
 
 void missarg(const char *) __attribute__((noreturn));
 void invarg(const char *, const char *) __attribute__((noreturn));
@@ -111,6 +118,16 @@ int dnet_pton(int af, const char *src, void *addr);
 const char *ipx_ntop(int af, const void *addr, char *str, size_t len);
 int ipx_pton(int af, const char *src, void *addr);
 
+/* j1939 */
+extern const char *j1939_ntop(int af, const void *addr, size_t vlen,
+		char *str, size_t len);
+extern const char *j1939_link_attrtop(struct rtattr *nla);
+
+extern int j1939_addr_args(int argc, char *argv[],
+		struct nlmsghdr *msg, int msg_size);
+extern int j1939_link_args(int argc, char *argv[],
+		struct nlmsghdr *msg, int msg_size);
+
 extern int __iproute2_hz_internal;
 extern int __get_hz(void);
 
ip/ip.c +3 -1
index b127d57..50fcb1c 100644
@@ -46,7 +46,7 @@ static void usage(void)
 "where  OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |\n"
 "                   tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm }\n"
 "       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
-"                    -f[amily] { inet | inet6 | ipx | dnet | link } |\n"
+"                    -f[amily] { inet | inet6 | ipx | dnet | link  | can} |\n"
 "                    -l[oops] { maximum-addr-flush-attempts } |\n"
 "                    -o[neline] | -t[imestamp] | -b[atch] [filename] |\n"
 "                    -rc[vbuf] [size]}\n");
@@ -181,6 +181,8 @@ int main(int argc, char **argv)
 				preferred_family = AF_PACKET;
 			else if (strcmp(argv[1], "ipx") == 0)
 				preferred_family = AF_IPX;
+			else if (strcmp(argv[1], "can") == 0)
+				preferred_family = AF_CAN;
 			else if (strcmp(argv[1], "help") == 0)
 				usage();
 			else
ip/ipaddress.c +78 -15

182 lines not on this page · raw message

ip/iplink.c +8 -0
index cb2c4f5..06fc34c 100644
@@ -27,6 +27,7 @@
 #include <string.h>
 #include <sys/ioctl.h>
 #include <linux/sockios.h>
+#include <linux/can.h>
 
 #include "rt_names.h"
 #include "utils.h"
@@ -71,6 +72,7 @@ void iplink_usage(void)
 	fprintf(stderr, "	                  [ vf NUM [ mac LLADDR ]\n");
 	fprintf(stderr, "				   [ vlan VLANID [ qos VLAN-QOS ] ]\n");
 	fprintf(stderr, "				   [ rate TXRATE ] ] \n");
+	fprintf(stderr, "			  [ j1939 { on | off } ]\n");
 	fprintf(stderr, "       ip link show [ DEVICE ]\n");
 
 	if (iplink_have_newlink()) {
@@ -383,6 +385,12 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req,
 				  *argv, strlen(*argv));
 			argc--; argv++;
 			break;
+		} else if (matches(*argv, "j1939") == 0) {
+			ret = j1939_link_args(argc, argv, &req->n, sizeof(*req));
+			if (ret < 0)
+				return ret;
+			argc -= ret;
+			argv += ret;
 		} else {
 			if (strcmp(*argv, "dev") == 0) {
 				NEXT_ARG();
lib/j1939.c new file +153 -0

153 lines not on this page · raw message

lib/utils.c +8 -1
index a60d884..02dda80 100644
@@ -25,6 +25,7 @@
 #include <linux/pkt_sched.h>
 #include <time.h>
 #include <sys/time.h>
+#include <linux/can.h>
 
 
 #include "utils.h"
@@ -513,7 +514,8 @@ int __get_user_hz(void)
 	return sysconf(_SC_CLK_TCK);
 }
 
-const char *rt_addr_n2a(int af, int len, const void *addr, char *buf, int buflen)
+const char *rt_addrpr_n2a(int af, int protocol, int len, const void *addr,
+		char *buf, int buflen)
 {
 	switch (af) {
 	case AF_INET:
@@ -527,6 +529,11 @@ const char *rt_addr_n2a(int af, int len, const void *addr, char *buf, int buflen
 		memcpy(dna.a_addr, addr, 2);
 		return dnet_ntop(af, &dna, buf, buflen);
 	}
+	case AF_CAN:
+		switch (protocol) {
+		case CAN_J1939:
+			return j1939_ntop(af, addr, len, buf, buflen);
+		}
 	default:
 		return "???";
 	}
man/man8/ip.8 +21 -3
index 8d55fa9..d17e06a 100644
@@ -23,7 +23,7 @@ ip \- show / manipulate routing, devices, policy routing and tunnels
 \fB\-s\fR[\fItatistics\fR] |
 \fB\-r\fR[\fIesolve\fR] |
 \fB\-f\fR[\fIamily\fR] {
-.BR inet " | " inet6 " | " ipx " | " dnet " | " link " } | "
+.BR inet " | " inet6 " | " ipx " | " dnet " | " link " | " can " } | "
 \fB\-o\fR[\fIneline\fR] }
 
 .ti -8
@@ -95,7 +95,9 @@ ip \- show / manipulate routing, devices, policy routing and tunnels
 .B qos
 .IR VLAN-QOS " ] ] ["
 .B rate
-.IR TXRATE " ]"
+.IR TXRATE " ] |"
+.br
+.BR j1939 " { " on " | " off " }"
 
 .ti -8
 .B ip link show
@@ -126,7 +128,9 @@ ip \- show / manipulate routing, devices, policy routing and tunnels
 .B  label
 .IR STRING " ] [ "
 .B  scope
-.IR SCOPE-ID " ]"
+.IR SCOPE-ID " ] | "
+.B  j1939
+.IR IFADDRJ1939 " ] "
 
 .ti -8
 .IR SCOPE-ID " := "
@@ -142,6 +146,15 @@ ip \- show / manipulate routing, devices, policy routing and tunnels
 tentative " | " deprecated " | " dadfailed " | " temporary " ]"
 
 .ti -8
+.IR J1939IFADDR " := [ " SA " ] [ "
+.B  name
+.IR " NODENAME " ]
+.br
+.IR SA " := " U8
+.br
+.IR NODENAME " := " U64
+
+.ti -8
 .BR "ip addrlabel" " { " add " | " del " } " prefix
 .BR PREFIX " [ "
 .B dev
@@ -1038,6 +1051,11 @@ Setting this parameter to 0 disables rate limiting. The
 parameter must be specified.
 .in -8
 
+.TP
+.BR "j1939 on " or "j1939 off"
+Enable or disable SAE J1939 on the device. This will only
+work when the device is a CAN device.
+
 .PP
 .B Warning:
 If multiple parameter changes are requested,
#7 lore raw

Re: [RFC v3 3/6] can: make struct proto const

In reply to Kurt Van Dijck
Quoted 50 lines · Kurt Van Dijck

can_ioctl is the only reason for struct proto to be non-const. script/check-patch.pl suggests struct proto be const. This patch performs the necessary change.

---

include/linux/can/core.h +3 -1
index 430c446..0767cc6 100644
@@ -39,7 +39,7 @@
 struct can_proto {
 	int              type;
 	int              protocol;
-	struct proto_ops *ops;
+	const struct proto_ops *ops;
 	struct proto     *prot;
 
 	const struct rtnl_af_ops *rtnl_link_ops;
@@ -78,6 +78,8 @@ struct rtgencanmsg {
 
 extern int  can_proto_register(const struct can_proto *cp);
 extern void can_proto_unregister(const struct can_proto *cp);
+extern int can_sock_ioctl(struct socket *sock, unsigned int cmd,
+		unsigned long arg);
 
 extern int  can_rx_register(struct net_device *dev, canid_t can_id,
 			    canid_t mask,
net/can/af_can.c +2 -1
index db59c6e..c1f8c05 100644
@@ -139,7 +139,7 @@ static inline void can_put_proto(const struct can_proto *cp)
  * af_can socket functions
  */
 
-static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
+int can_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 {
 	struct sock *sk = sock->sk;
 
@@ -152,6 +152,7 @@ static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 		return -ENOIOCTLCMD;
 	}
 }
+EXPORT_SYMBOL(can_sock_ioctl);
 
 static void can_sock_destruct(struct sock *sk)
 {

net/can/af_can.c int can_proto_register(const struct can_proto *cp) · line 726

Quoted 7 lines
net/can/af_can.c +0 -4
@@ -720,10 +721,6 @@ int can_proto_register(const struct can_proto *cp)
 		err = -EBUSY;
 	} else {
 		proto_tab[proto] = cp;
-
-		/* use generic ioctl function if not defined by module */
-		if (!cp->ops->ioctl)
-			cp->ops->ioctl = can_ioctl;

Hmm, you actually fixed a race / bug, since we installed in proto_tab[] a pointer to a not yet initted ops structure.

I suggest you change patch title and changelog to properly describe the bug fix.

#3 lore raw

Re: [RFC v3 1/6] can: extend sockaddr_can to include j1939 members

In reply to Kurt Van Dijck

include/linux/can/core.h struct can_proto { · line 53

Quoted 9 lines · Kurt Van Dijck
include/linux/can/core.h +9 -0
+/*
+ * required_size
+ * macro to find the minimum size of a struct
+ * that includes a requested member
+ */
+#define required_size(member, struct_type) \
+	(offsetof(typeof(struct_type), member) + \
+	 sizeof(((typeof(struct_type) *)(0))->member))
+

Hmm, most similar macros reverse arguments.

(For example : offsetof())

What about

#define required_size(TYPE, MEMBER) ...

#4 lore raw

Re: [RFC v3 1/6] can: extend sockaddr_can to include j1939 members

Quoted 12 lines · Eric Dumazet

include/linux/can/core.h struct can_proto { · line 53

Quoted 9 lines
include/linux/can/core.h +9 -0
+/*
+ * required_size
+ * macro to find the minimum size of a struct
+ * that includes a requested member
+ */
+#define required_size(member, struct_type) \
+	(offsetof(typeof(struct_type), member) + \
+	 sizeof(((typeof(struct_type) *)(0))->member))
+

Hmm, most similar macros reverse arguments.

Quoted 1 line

(For example : offsetof())

I had 'list_entry' in mind.

Quoted 4 lines

What about

#define required_size(TYPE, MEMBER) ...

I'm not against that ...
If I receive no complaint for that, I can alter this in the next series.

Quoted 2 lines

Thanks,
Kurt

#8 lore raw

Re: [RFC v3 3/6] can: make struct proto const

In reply to Eric Dumazet
Quoted 5 lines · Eric Dumazet
Quoted 4 lines · Kurt Van Dijck

can_ioctl is the only reason for struct proto to be non-const. script/check-patch.pl suggests struct proto be const. This patch performs the necessary change.

Quoted 13 lines

net/can/af_can.c int can_proto_register(const struct can_proto *cp) · line 726

Quoted 10 lines

static void can_sock_destruct(struct sock *sk)
{

net/can/af_can.c +0 -4
@@ -720,10 +721,6 @@ int can_proto_register(const struct can_proto *cp)
 		err = -EBUSY;
 	} else {
 		proto_tab[proto] = cp;
-
-		/* use generic ioctl function if not defined by module */
-		if (!cp->ops->ioctl)
-			cp->ops->ioctl = can_ioctl;

Hmm, you actually fixed a race / bug, since we installed in proto_tab[] a pointer to a not yet initted ops structure.

I see your point.
The reason I modified this was, as described, checkpatch.pl was complaining. Anyway, this sequence was protected by a spinlock 'proto_tab_lock'. I don't think it was a race.

Did I miss something?
Kurt
_______________________________________________
Socketcan-core mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/socketcan-core

#9 lore raw

Re: [RFC v3 3/6] can: make struct proto const

Quoted 6 lines · Kurt Van Dijck

I see your point.
The reason I modified this was, as described, checkpatch.pl was complaining. Anyway, this sequence was protected by a spinlock 'proto_tab_lock'. I don't think it was a race.

Did I miss something?

As soon as proto_tab[proto] = cp; is done, another thread on another cpu can read the pointer and follow it.

Hmm, I missed can_create() also uses the spinlock protection, so you're probably right.

It seems a bit overkill :(
phonet uses RCU for example.

#10 lore raw

Re: [RFC v3 3/6] can: make struct proto const

Quoted 9 lines · Eric Dumazet
Quoted 6 lines · Kurt Van Dijck

I see your point.
The reason I modified this was, as described, checkpatch.pl was complaining. Anyway, this sequence was protected by a spinlock 'proto_tab_lock'. I don't think it was a race.

Did I miss something?

Quoted 8 lines

As soon as proto_tab[proto] = cp; is done, another thread on another cpu can read the pointer and follow it.

Hmm, I missed can_create() also uses the spinlock protection, so you're probably right.

It seems a bit overkill :(
phonet uses RCU for example.

I'll keep that in mind.
Switching CAN protos to RCU is far beyond to goal of this patch :-)

Thanks,
Kurt
_______________________________________________
Socketcan-core mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/socketcan-core

#39 lore raw
In reply to a message not in the archive
Quoted 4 lines · Kurt Van Dijck

This series adds SAE J1939 support to the current net-next-2.6. For the rtnetlink operations to work, a 2.6.38-rc1 is required.

Quoted 8 lines

1/6: can: extend sockaddr_can to include j1939 members 2/6: can: add rtnetlink support
3/6: can: make struct proto const
4/6: j1939: initial import of SAE J1939
5/6: j1939: add documentation & MAINTAINERS 6/6: iproute2: use CAN & J1939

Still a lot of things probably need explanation...

I had not mentioned the major differences with v2 (which went to berlios SocketCAN mailing list only).

* replace magic constants with macro's
* use a static protocol stack, not a dynamic one.
* remove feature that have limited use and are difficult to explain.
* renamed some structure types to increase code readability.
* block on transport sessions until CAN frame is queued.

* fix bugs.

Kurt

maintainer
#11 lore raw

Re: [RFC v3 3/6] can: make struct proto const

In reply to Kurt Van Dijck
Quoted 10 lines · Kurt Van Dijck
Quoted 9 lines · Eric Dumazet
Quoted 6 lines · Kurt Van Dijck

I see your point.
The reason I modified this was, as described, checkpatch.pl was complaining. Anyway, this sequence was protected by a spinlock 'proto_tab_lock'. I don't think it was a race.

Did I miss something?

Quoted 8 lines
Quoted 8 lines

As soon as proto_tab[proto] = cp; is done, another thread on another cpu can read the pointer and follow it.

Hmm, I missed can_create() also uses the spinlock protection, so you're probably right.

It seems a bit overkill :(
phonet uses RCU for example.

Hello Eric,

the RCU code in phonet is worth to consider. But loading and removing of protocols is far away from being a hot path ... what would be the advantages to move to RCU here?

Quoted 2 lines

I'll keep that in mind.
Switching CAN protos to RCU is far beyond to goal of this patch :-)

Anyway making the struct proto a const (and set the default defines in the CAN protocols) is a nice cleanup. Let's wait for the feedback about the RCU from Eric ... then this patch could be put into mainline independently from the j1939 protocol.

Thanks,
Oliver

#13 lore raw

Re: [RFC v3 3/6] can: make struct proto const

In reply to a message not in the archive
Quoted 17 lines · Oliver Hartkopp
Quoted 16 lines · Kurt Van Dijck
Quoted 15 lines · Eric Dumazet
Quoted 6 lines · Kurt Van Dijck

I see your point.
The reason I modified this was, as described, checkpatch.pl was complaining. Anyway, this sequence was protected by a spinlock 'proto_tab_lock'. I don't think it was a race.

Did I miss something?

As soon as proto_tab[proto] = cp; is done, another thread on another cpu can read the pointer and follow it.

Hmm, I missed can_create() also uses the spinlock protection, so you're probably right.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

It seems a bit overkill :(
phonet uses RCU for example.

Hello Eric,

the RCU code in phonet is worth to consider. But loading and removing of protocols is far away from being a hot path ... what would be the advantages to move to RCU here?

Does RCU avoid a spinlock?

Quoted 8 lines
Quoted 3 lines

I'll keep that in mind.
Switching CAN protos to RCU is far beyond to goal of this patch :-)

Anyway making the struct proto a const (and set the default defines in the CAN protocols) is a nice cleanup. Let's wait for the feedback about the RCU from Eric ... then this patch could be put into mainline independently from the j1939 protocol.

ack.

Kurt
_______________________________________________
Socketcan-core mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/socketcan-core

#14 lore raw

Re: [RFC v3 3/6] can: make struct proto const

In reply to Oliver Hartkopp
Quoted 6 lines · Oliver Hartkopp

Hello Eric,

the RCU code in phonet is worth to consider. But loading and removing of protocols is far away from being a hot path ... what would be the advantages to move to RCU here?

can_create() could avoid taking the spinlock.

I have no idea if its a critical path, I guess not.

#18 lore raw

Re: [RFC v3 5/6] j1939: add documentation and MAINTAINERS

In reply to a message not in the archive
Quoted 1 line · Kurt Van Dijck

This patch adds the documentation and MAINTAINERS.

Hello Kurt,

even after our F2F-discussion on the Embedded World i'm still not convinced, why it should be a good idea to handle all the address claiming process inside the kernel.

Besides the fact, that other j1939 implementation are *completely* implemented in userspace (and can cope with the time restrictions), i do not see why you put the address claiming into the kernel and not only the transport layer stuff.

The address claiming can be compared to something like DHCP or DNS in the internet protocol world, that are both handled and implemented in userspace apps or userspace libraries.

E.g. these bits from the documentation look like you are starting some kind of 'addressing service' daemon:

Documentation/networking/j1939.txt line 196

Quoted 9 lines

+4.1 rtnetlink interface

Documentation/networking/j1939.txt new file +8 -0
+
+  Per default j1939 is not active. Specifying can_ifindex != 0 in bind(2)
+  or connect(2) needs an active j1939 on that interface. You must have done
+  $ ip link set canX j1939 on
+  on that interface.
+
+  $ ip link set canX j1939 down
+  disables j1939 on canX.

You are activating the 'addressing service' on specific CAN interfaces.

Then you suggest to attach static and/or dynamic addresses to the interface.

Documentation/networking/j1939.txt line 203

Quoted 6 lines
Documentation/networking/j1939.txt new file +6 -0
+  Assigning addresses is done via
+  $ ip addr add dev canX j1939 0xXX
+  statically or
+  $ ip addr add dev canX j1939 name 0xXX
+  dynamically. In the latter case, address claiming must take place
+  before other traffic can leave.

like you would have using DHCP/DNS (adapted for j1939) ...

Documentation/networking/j1939.txt line 209

Quoted 5 lines
Documentation/networking/j1939.txt new file +5 -0
+  Removing addresses is done similarly via
+  $ ip addr del dev canX j1939 0xXX
+  $ ip addr del dev canX j1939 name 0xXX
+
+  A static address cannot be assigned together with a 64bit name.

Ah. You provide two kernel interfaces instead of handling the address claiming in userspace and provide only one simple (static) interface to the kernel.

This artifact brings this fact out again:

Documentation/networking/j1939.txt line 268

Quoted 8 lines
Documentation/networking/j1939.txt new file +8 -0
+  can_addr.j1939.name contains the 64-bit J1939 NAME.
+
+  can_addr.j1939.addr contains the source address.
+
+  When sending data, the source address is applied as follows: If
+  can_addr.j1939.name != 0 the NAME is looked up by the kernel and the
+  corresponding Source Address is used. If can_addr.j1939.name == 0,
+  can_addr.j1939.addr is used.

Yes. You are providing two programming interfaces to the kernel that can be used exclusive-OR only.

As other j1939 implementations implement the address claiming in userspace too, there's no necessity to implement this inside the kernel. DHCP and DNS can also lead to address changes and some 'new j1939 addressing daemon' could be implemented in a way that allows to tell registered j1939 apps address changes in a fast way (trigger/signal/select/whatever). I don't know how much of the 4838 lines of code from your RFC can be removed - but i assume it would vastly reduce the complexity of your posted j1939 implementation.

If you would implement only the j1939 transport layer stuff (using static addressing) together with some userspace 'address claiming' daemon and/or library (compared to DNS) this would make more sense to me. Especially it makes the kernel API clear and simple(!). As a side-effect this would remove the need for a separate system-wide configuration which would need to be added with your suggested netlink extensions in af_can.c, iproute2 and the other adaptions to the current code to extend the sockaddr_can in size.

I know you're a fan of j1939 address claiming inside the kernel but IMO this does not fit other networking addressing concepts inside the kernel and messes up the kernel API for j1939, sigh.

If it makes sense to you, i would offer my help to implement the address claiming daemon based on CAN_RAW and CAN_BCM ... 8-)

Anyway: Thanks for you contribution!

Oliver

#20 lore raw

Re: [RFC v3 5/6] j1939: add documentation and MAINTAINERS

In reply to a message not in the archive
Quoted 3 lines · Oliver Hartkopp

On 14.03.2011 14:59, Kurt Van Dijck wrote:

Hello Kurt,

hello Oliver :-)

Quoted 2 lines

even after our F2F-discussion on the Embedded World i'm still not convinced,

ok

Are you aware of the fact that, since your experience with dynamic addressing is limited, ignoring this completely will leave you yet with a working, operational J1939 stack?
When address claiming is not in place, just work with the static addresses. It'll work. The addition of address claiming in kernel just looses you a few cpu clock cycles.

At least, that's the intention of the API.
The patches I sent have proven to have some faults yet. I'm again working on an update.

Quoted 2 lines

why it should be a good idea to handle all the address claiming process inside the kernel.

IMO, it's a good idea to have the kernel support the address claiming process. Why:
* IMO, J1939 implicitely puts address claiming below Transport protocol. As such, you can't really put Transport protocol in kernel & address claim in userspace.
* In kernel, an ECU can 'atomically' switch addresses,
AND have pending transport sessions (in kernel) use the new address.
* in kernel, a sendto() can be held (block) during address claim delays (not yet implemented).
* In kernel, you can let userspace participate on a J1939 bus _correctly_ in an imperative way, rather than the volutary userspace library setups.

The above are arguments to minimize the required overhead in userspace for supporting address claiming.

I don't want the kernel to handle _all_ the address claiming. Why:
* address claiming _is_ a policy related thing. I don't put policies in kernel. I learned that as soon that a policy is implemented, our customer wants it different.
But our customer should _not_ try modifying the non-policy related part of address claiming.

Think of this scenario: we put only transport sessions in kernel. So, you instruct the kernel to send few kB on j1939 bus, from (A)0x80. What will happen when (B)0x80 kicks (A) from its 0x80 using address claiming.
(A) will use (eg.) 0x82. But your transport session is still pending, and using 0x80 ==>> illegal.

My implementation lets the kernel follow the address claiming traffic, that was initiated from userspace, so it can handle this situation correctly.

Also note that this is the smallest example to make the point. More elaborated and complex examples exist.

The important thing, IMO, is how this is controlled from userspace. Yes, you can instruct the kernel to do magic trickery then, but my implementation uses the regular API's for this. I just accepted the fact that the 64bit name is the real reference to indicate an ECU. So I put that in the sockaddr_can too.

Quoted 3 lines

Besides the fact, that other j1939 implementation are *completely* implemented in userspace (and can cope with the time restrictions),

They probably do, on a voluntary basis.

When spread across different processes cooperating to the same ECU, they have no means to guarantee anything. So, every application should carefully respect the guidelines for the userspace library.
And after that is accomplished, do they take care of the following scenario:
Process A sends a PGN regularly
Process B deals with address claims.
When process B emits a new address, process A should _not_ send new PGN's for 250msec. That is the 'time restriction'
However, their interprocess communication will race with the in-kernel CAN queue's. So, A may emit PGN's after the address claim, but before the 'address notification' I don't see a proper way to deal with it.

Quoted 2 lines

i do not see why you
put the address claiming into the kernel and not only the transport layer stuff.

see above.

Quoted 4 lines

The address claiming can be compared to something like DHCP or DNS in the internet protocol world, that are both handled and implemented in userspace apps or userspace libraries.

Yes and no.
Yes: DHCP also deals with 'dynamic addresses'. The policies applied there could be used for J1939 too.
No: DHCP operates in a master-slave way, J1939 does not.
DHCP does not handle conflicts, since the master should not give conflicting leases. J1939 must deal with conflicts.

A proper comparison on IP would be to eliminate the DHCP server, and let each client 'just use an IP', and when another ethernet card happens to have chosen the exact same IP, the winner is decided (on both ends) based on the MAC address. This would introduce loads of problems in IP too.
The atomic operation & conflict handling are kernel parts IMHO.

another comparison would be ARP, where each node holds its own table, carefully built from passing network traffic.

The 64bit 'name' is not a name as in DNS, it's a unique ID.

Quoted 3 lines

E.g. these bits from the documentation look like you are starting some kind of 'addressing service' daemon:

Yep, to implement the 'address choosing' policy.
I'm still testing all J1939 requirements here, but when this is ready, I feel I will need to publish such program.

Quoted 12 lines

Documentation/networking/j1939.txt line 196

Quoted 10 lines

+4.1 rtnetlink interface

Documentation/networking/j1939.txt new file +8 -0
+
+  Per default j1939 is not active. Specifying can_ifindex != 0 in bind(2)
+  or connect(2) needs an active j1939 on that interface. You must have done
+  $ ip link set canX j1939 on
+  on that interface.
+
+  $ ip link set canX j1939 down
+  disables j1939 on canX.

You are activating the 'addressing service' on specific CAN interfaces.

Since no autoprobing of protocols is possible on CAN (in contrast to Ethernet).

Quoted 2 lines

Then you suggest to attach static and/or dynamic addresses to the interface.

Correction: you assign a static address, or a 'name' for use with dynamic addressing. the name for dynamic addressing is not a 'dynamic address'.

Quoted 9 lines

Documentation/networking/j1939.txt line 203

Quoted 7 lines
Documentation/networking/j1939.txt new file +6 -0
+  Assigning addresses is done via
+  $ ip addr add dev canX j1939 0xXX
+  statically or
+  $ ip addr add dev canX j1939 name 0xXX
+  dynamically. In the latter case, address claiming must take place
+  before other traffic can leave.

like you would have using DHCP/DNS (adapted for j1939) ...

The userspace part for address claiming (in fact, the right CAN frames should appear to fullfill the address claiming requirements) can then bind to the name, and claim addresses for it. The kernel will reccognize those, and act upon them. The kernel will at no point initiate an address claim.

Quoted 8 lines

Documentation/networking/j1939.txt line 209

Quoted 6 lines
Documentation/networking/j1939.txt new file +5 -0
+  Removing addresses is done similarly via
+  $ ip addr del dev canX j1939 0xXX
+  $ ip addr del dev canX j1939 name 0xXX
+
+  A static address cannot be assigned together with a 64bit name.

Ah. You provide two kernel interfaces

Yep, because there are two distinct use cases to address.
With dynamic addressing, the 8bit address is just a temporary placeholder, which may change in the future. This makes a bad discriminator to differentiate between hosts. therefore, to specify a host to the kernel (as source or destination), the 8bit address is irrelevant at that point in time.
Therefore, the kernel _needs_ knowledge of the 64bit name, just to provide a decent interface to userspace for source/destination identification.

Quoted 2 lines

instead of handling the address claiming
in userspace and provide only one simple (static) interface to the kernel.

And this is no option IMO.
Just above, I indicated that the kernel needs the 64bit name. Pairing with the 8bit address in userspace only will:
* add a serious amount of userspace code & userspace timing requirements.
* create a complex library/IPC API.
* not solve the case for pending transport sessions.
* still unspecified how the kernel should treat messages that have specified a 64bit destination name with different 8bit destination address.
IMO, doing these things in userspace are unnecessarily complicated.

Quoted 14 lines

This artifact brings this fact out again:

Documentation/networking/j1939.txt line 268

Quoted 8 lines
Documentation/networking/j1939.txt new file +8 -0
+  can_addr.j1939.name contains the 64-bit J1939 NAME.
+
+  can_addr.j1939.addr contains the source address.
+
+  When sending data, the source address is applied as follows: If
+  can_addr.j1939.name != 0 the NAME is looked up by the kernel and the
+  corresponding Source Address is used. If can_addr.j1939.name == 0,
+  can_addr.j1939.addr is used.

Yes. You are providing two programming interfaces to the kernel that can be used exclusive-OR only.

ack.

Quoted 6 lines

As other j1939 implementations implement the address claiming in userspace too, there's no necessity to implement this inside the kernel. DHCP and DNS can also lead to address changes and some 'new j1939 addressing daemon' could be implemented in a way that allows to tell registered j1939 apps address changes in a fast way (trigger/signal/select/whatever). I don't know how much

This is like saying: the race condition could be improved. But it's still there, isn't it?

Quoted 1 line

of the 4838 lines of code from your RFC can be removed - but i assume it would

+/- 700
which will move to every userspace application, and they'd still not be correct.

Quoted 6 lines

vastly reduce the complexity of your posted j1939 implementation.

If you would implement only the j1939 transport layer stuff (using static addressing) together with some userspace 'address claiming' daemon and/or library (compared to DNS) this would make more sense to me. Especially it makes the kernel API clear and simple(!).

and crippled.
Being clear and simple is no justification for introducing race conditions!

Quoted 2 lines

As a side-effect this would remove
the need for a separate system-wide configuration

Why is that a problem. I look at it as a powerfull administrative tool. The fact that J1939 is the first CAN protocol with a 'real' addressing scheme seems a major problem.

Quoted 2 lines

which would need to be added
with your suggested netlink extensions in af_can.c, iproute2

It works, and IMO fits in the policies.

Quoted 1 line

and the other adaptions to the current code to extend the sockaddr_can in size.

Sooner or later, when CAN protocols are added, the sockaddr_can will grow. Not letting this grow is equivalent of saying that no CAN protocols will be added.

This fits in the current policies. why do BSD socket functions invented the af_family & socklen_t stuff. Because they faced this problem before. Putting J1939 inside AF_CAN implies handling this kind of stuff. And I handled it. I haven't recompiled my old can tools for ages now, but they still work.

Quoted 2 lines

I know you're a fan of j1939 address claiming inside the kernel

I'm no fan. I only respect the fact that it's the only right place.

Quoted 1 line

but IMO this does not fit other networking addressing concepts inside the kernel

IPv6 automatic router detection?
At least, I disagree.

Quoted 1 line

and messes up the kernel API for j1939, sigh.

I don't think my proposed API is a mess. It my way of addressing existing problems. The fact that some userspace library for tons of dollars does not solve my problem is IMO no reason to make the same fault.

Quoted 3 lines

If it makes sense to you, i would offer my help to implement the address claiming daemon based on CAN_RAW and CAN_BCM ... 8-)

Thanks for the offer.
I have no clue yet how to do the necessary IPC in a way that userspace would be easy to work with _UNDER ALL CIRCUMSTANCES_ (sorry for the capitals).

Quoted 2 lines

Anyway: Thanks for you contribution!

Thanks for looking at the API.
For what it's worth, since the discussion started, I also think the API has improved a lot. Thanks for that!

Kurt

Quoted 2 lines

Oliver

#21 lore raw

SAE J1939: update

In reply to a message not in the archive

Oliver,

Some progress:

I dropped the state-machine for the address claiming. This makes it less confusing I hope.

I dropped SO_J1939_DEST_MASK. This could be done in userspace by accessing a procfs file, or additional rtnetlink ... Lookup up remote ecu's is not strictly necessary in kernel. I have no idea yet how such library would look like.

Can userspace tools be attached to the socketCAN subversion can-utils, or would a seperate dir be necessary?

I'd say 'suggestions are welcome' but I know you'd suggest to remove address claiming..

Kurt

maintainer
#22 lore raw

Re: SAE J1939: update

Quoted 6 lines · Kurt Van Dijck

Oliver,

Some progress:

I dropped the state-machine for the address claiming. This makes it less confusing I hope.

Thanks.

Quoted 4 lines

I dropped SO_J1939_DEST_MASK. This could be done in userspace by accessing a procfs file, or additional rtnetlink ... Lookup up remote ecu's is not strictly necessary in kernel. I have no idea yet how such library would look like.

I'll check that with my colleague Urs, who knows very good, which concepts and 'mechanics' are commonly used for such kind of requirements.

Quoted 2 lines

Can userspace tools be attached to the socketCAN subversion can-utils, or would a seperate dir be necessary?

I assume about 3-4 tools (like an AC-daemon, some dump or sniffer tool and a library with some header file) to emerge in can-utils then, which should be ok. There could also be some test-programms or src-samples that could be placed in trunk/test .

Quoted 2 lines

I'd say 'suggestions are welcome' but I know you'd suggest to remove address claiming..

I'm really very interested in having the address claiming - just somewhere in userspace :-)

But of course it might become an ambitious target to support the address claiming with an appropriate infrastructure to help users to fulfill their needs without getting confused.

Thanks & best regards,
Oliver

#23 lore raw

Re: [RFC v3 5/6] j1939: rename NAME to UUID?

In reply to a message not in the archive

Oliver et.al.,

Quoted 4 lines · Oliver Hartkopp

On 14.03.2011 14:59, Kurt Van Dijck wrote:

Then you suggest to attach static and/or dynamic addresses to the interface.

Quoted 9 lines

Documentation/networking/j1939.txt line 203

Quoted 6 lines
Documentation/networking/j1939.txt new file +6 -0
+  Assigning addresses is done via
+  $ ip addr add dev canX j1939 0xXX
+  statically or
+  $ ip addr add dev canX j1939 name 0xXX
+  dynamically. In the latter case, address claiming must take place
+  before other traffic can leave.

like you would have using DHCP/DNS (adapted for j1939) ...

I suspect the confustion with DHCP/DNS comes free with the used terminology.

Specifications talk about a 64bit NAME, where is actually is a 64bit UUID. Calling this number a UUID may clarify things, but leaves the spec in the terminology.

one would then do:

$ ip addr add dev canX j1939 uuid XXXX

Would that be a good way to progress?

Kurt

#25 lore raw

Re: [RFC v3 5/6] j1939: rename NAME to UUID?

In reply to a message not in the archive
Quoted 18 lines · Kurt Van Dijck

Oliver et.al.,

Quoted 4 lines · Oliver Hartkopp

On 14.03.2011 14:59, Kurt Van Dijck wrote:

Then you suggest to attach static and/or dynamic addresses to the interface.

Quoted 9 lines

Documentation/networking/j1939.txt line 203

Quoted 6 lines
Documentation/networking/j1939.txt new file +6 -0
+  Assigning addresses is done via
+  $ ip addr add dev canX j1939 0xXX
+  statically or
+  $ ip addr add dev canX j1939 name 0xXX
+  dynamically. In the latter case, address claiming must take place
+  before other traffic can leave.

like you would have using DHCP/DNS (adapted for j1939) ...

I suspect the confustion with DHCP/DNS comes free with the used terminology.

Quoted 8 lines

Specifications talk about a 64bit NAME, where is actually is a 64bit UUID. Calling this number a UUID may clarify things, but leaves the spec in the terminology.

one would then do:

$ ip addr add dev canX j1939 uuid XXXX

Would that be a good way to progress?

Hello Kurt,

i don't know if it helps - at least for j1939 users - to rename the NAME for j1939 address claiming to UUID which is usually 128 bit long an has a pretty different understanding than the J1939 NAME which stands for

1. Arbitrary address bit
2. Industry group, length 3 bits
3. Vehicle system instance, length 4 bits
4. Vehicle system, length 7 bits
5. Reserved bit
6. Function, length 8 bits
7. Function instance, length 5 bits
8. ECU instance, length 3 bits
9. Manufacturer code, length 11 bits
10. Identity number, length 21 bits

(from http://www.kvaser.com/en/about-can/higher-layer-protocols/36.html)

This is not comparable to the ideas from RFC 4122 ...

Thinking about the approach to implement the j1939 address claiming (AC) in userspace, i discovered two ways which could both be hidden inside some easy-to-use helper functions:

1. implement a thread (e.g. within a library) which opens a CAN_RAW socket on a specific CAN-interface and takes care of the AC procedure and monitors ongoing AC procedures on the bus. In this case every j1939 application requiring AC internally would monitor all the AC handling on itself (which should be no general problem - written only once).

2. create j1939ac daemon(s) using PF_UNIX-sockets to be named e.g. j1939ac_can0, j1939ac_can1, etc. - these daemons take care for all AC requirements of the host it is running on. The PF_UNIX-sockets are used in SOCK_DGRAM mode and only the j1939 processes that need AC can then register their NAME by sending a request datagram, and get back the j1939-address once it is claimed (and all the updates on changes). As the j1939ac daemons are running on the same host as the j1939 application processes, optional the process' PID could be provided to the daemon during the registering process, so that the daemon can send a signal to a signal handler of the application process (if you would like to omit the select() syscall to handle both the j1939 and PF_UNIX sockets).

-> <Req><Name="A3B5667799332242" PID="12345">
<- <Resp><ACState="claimed" Name="A3B5667799332242" Address="1B">
(some time)
<- <Resp><ACState="changed" Name="A3B5667799332242" Address="1C">

This is a sketch that could be put into simple C-structs that are sent via the PF_UNIX DGRAM socket.

In all suggested cases (using a thread, daemon with/without signal) the AC procedure can be managed in userspace without real pain. But especially with less pain than putting the AC process into kernelspace and provide your suggested socket API with bind/connect/... in very different manners.

Regards,
Oliver

#27 lore raw

Re: [RFC v3 5/6] j1939: rename NAME to UUID?

In reply to a message not in the archive
Quoted 45 lines · Oliver Hartkopp
Quoted 18 lines · Kurt Van Dijck

Oliver et.al.,

Quoted 4 lines · Oliver Hartkopp

On 14.03.2011 14:59, Kurt Van Dijck wrote:

Then you suggest to attach static and/or dynamic addresses to the interface.

Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+  Assigning addresses is done via
+  $ ip addr add dev canX j1939 0xXX
+  statically or
+  $ ip addr add dev canX j1939 name 0xXX
+  dynamically. In the latter case, address claiming must take place
+  before other traffic can leave.

like you would have using DHCP/DNS (adapted for j1939) ...

I suspect the confustion with DHCP/DNS comes free with the used terminology.

Quoted 8 lines

Specifications talk about a 64bit NAME, where is actually is a 64bit UUID. Calling this number a UUID may clarify things, but leaves the spec in the terminology.

one would then do:

$ ip addr add dev canX j1939 uuid XXXX

Would that be a good way to progress?

Hello Kurt,

i don't know if it helps - at least for j1939 users - to rename the NAME for j1939 address claiming to UUID which is usually 128 bit long an has a pretty different understanding than the J1939 NAME which stands for

1. Arbitrary address bit
2. Industry group, length 3 bits
3. Vehicle system instance, length 4 bits
4. Vehicle system, length 7 bits
5. Reserved bit
6. Function, length 8 bits
7. Function instance, length 5 bits
8. ECU instance, length 3 bits
9. Manufacturer code, length 11 bits
10. Identity number, length 21 bits

Quoted 3 lines

(from http://www.kvaser.com/en/about-can/higher-layer-protocols/36.html)

This is not comparable to the ideas from RFC 4122 ...

please note that for address claiming, this interpretation should not be used as such, and the whole 64bit NAME is regarded as a 'Unique Number' :-)

I was not aware of RFC 4122 :-)

Never mind this idea, I'll stick to 'NAME' as that is what j1939 users are used to. I wanted to avoid people thinking on DNS, but I should not introduce other misunderstandings.

Kurt

#28 lore raw

Re: [RFC v3 5/6] j1939: rename NAME to UUID?

In reply to a message not in the archive
Quoted 30 lines · Oliver Hartkopp
Quoted 1 line · Kurt Van Dijck

Oliver et.al.,

Thinking about the approach to implement the j1939 address claiming (AC) in userspace, i discovered two ways which could both be hidden inside some easy-to-use helper functions:

1. implement a thread (e.g. within a library) which opens a CAN_RAW socket on a specific CAN-interface and takes care of the AC procedure and monitors ongoing AC procedures on the bus. In this case every j1939 application requiring AC internally would monitor all the AC handling on itself (which should be no general problem - written only once).

2. create j1939ac daemon(s) using PF_UNIX-sockets to be named e.g. j1939ac_can0, j1939ac_can1, etc. - these daemons take care for all AC requirements of the host it is running on. The PF_UNIX-sockets are used in SOCK_DGRAM mode and only the j1939 processes that need AC can then register their NAME by sending a request datagram, and get back the j1939-address once it is claimed (and all the updates on changes). As the j1939ac daemons are running on the same host as the j1939 application processes, optional the process' PID could be provided to the daemon during the registering process, so that the daemon can send a signal to a signal handler of the application process (if you would like to omit the select() syscall to handle both the j1939 and PF_UNIX sockets).

-> <Req><Name="A3B5667799332242" PID="12345">
<- <Resp><ACState="claimed" Name="A3B5667799332242" Address="1B">
(some time)
<- <Resp><ACState="changed" Name="A3B5667799332242" Address="1C">

Quoted 5 lines

This is a sketch that could be put into simple C-structs that are sent via the PF_UNIX DGRAM socket.

In all suggested cases (using a thread, daemon with/without signal) the AC procedure can be managed in userspace without real pain.

I seriously doubt this statement.
define 'real pain'.

Quoted 3 lines

But especially with
less pain than putting the AC process into kernelspace and provide your suggested socket API with bind/connect/... in very different manners.

* You're only counting LOC in kernel, and not in userspace.
* The constructs you present create some kind of infrastructure. You will need a torough documentation of the 'good practice' since that's crucial to the correct operation of the stack. Did you ever consider the impact on the userspace application side.
Remember that userspace programs should be easy to write.
I found your proposals impact application development very hard.

I still think my passive support in kernel performs better and gives an easier API to get things done with.

I have it all operating, both embedded and on dual-core 64bit system. I need to git pull the latest updates, and I'll send a revised patchset tomorrow.

Kind regards,
Kurt

#30 lore raw

Re: [RFC v3 5/6] j1939: rename NAME to UUID?

In reply to a message not in the archive
Quoted 31 lines · Kurt Van Dijck
Quoted 30 lines · Oliver Hartkopp
Quoted 1 line · Kurt Van Dijck

Oliver et.al.,

Thinking about the approach to implement the j1939 address claiming (AC) in userspace, i discovered two ways which could both be hidden inside some easy-to-use helper functions:

1. implement a thread (e.g. within a library) which opens a CAN_RAW socket on a specific CAN-interface and takes care of the AC procedure and monitors ongoing AC procedures on the bus. In this case every j1939 application requiring AC internally would monitor all the AC handling on itself (which should be no general problem - written only once).

2. create j1939ac daemon(s) using PF_UNIX-sockets to be named e.g. j1939ac_can0, j1939ac_can1, etc. - these daemons take care for all AC requirements of the host it is running on. The PF_UNIX-sockets are used in SOCK_DGRAM mode and only the j1939 processes that need AC can then register their NAME by sending a request datagram, and get back the j1939-address once it is claimed (and all the updates on changes). As the j1939ac daemons are running on the same host as the j1939 application processes, optional the process' PID could be provided to the daemon during the registering process, so that the daemon can send a signal to a signal handler of the application process (if you would like to omit the select() syscall to handle both the j1939 and PF_UNIX sockets).

-> <Req><Name="A3B5667799332242" PID="12345">
<- <Resp><ACState="claimed" Name="A3B5667799332242" Address="1B">
(some time)
<- <Resp><ACState="changed" Name="A3B5667799332242" Address="1C">

Quoted 7 lines
Quoted 5 lines

This is a sketch that could be put into simple C-structs that are sent via the PF_UNIX DGRAM socket.

In all suggested cases (using a thread, daemon with/without signal) the AC procedure can be managed in userspace without real pain.

I seriously doubt this statement.
define 'real pain'.

The same pain as writing a DNS or DHCP daemon which is doing a similar job ...

Quoted 4 lines
Quoted 3 lines

But especially with
less pain than putting the AC process into kernelspace and provide your suggested socket API with bind/connect/... in very different manners.

* You're only counting LOC in kernel, and not in userspace.

Yes! Things that can be left out of the kernel, should be implemented and maintained in userspace - at least to reduce complexity and potential security issues.

Quoted 4 lines

* The constructs you present create some kind of infrastructure. You will need a torough documentation of the 'good practice' since that's crucial to the correct operation of the stack. Did you ever consider the impact on the userspace application side.

Yes.

Quoted 2 lines

Remember that userspace programs should be easy to write. I found your proposals impact application development very hard.

define 'very hard' ;-)

Quoted 2 lines

I still think my passive support in kernel performs better and gives an easier API to get things done with.

Kurt, the problem for me is, that you constantly state that your approach is the best. For me it is not.

The major issue in your implementation is the lack of the possibility to simulate several j1939 ECUs on one Linux host talking to each other via virtual CAN busses to create a complete j1939 network. And so far you did not address this request.

There are several j1939 implementations that are running (or can be made running) completely in userspace using raw-sockets. Obviously many people are convenient with these implementations.

I'm fine to place the j1939 data transfer part (supporting the segmented transfer of long j1939 PDUs) into the kernel - but not all the address claiming and the binding of j1939 addresses to network interfaces that also kills the requested feature of simulating a complete j1939 network.

As an amicable approach i would suggest to proceed in two steps:

1. post and mainline the j1939 bits that deal with the data transfer only
- no address claiming / j1939 name handling
- no binding of j1939 addresses to CAN network devices
- no extensions in the current af_can.c
- etc.

2. discuss the implementation of the (optional) j1939 address claiming

For me this approach makes sense for j1939 newbies and also experienced j1939 users that may become interested in the Linux mainline implementation.

Then - discussing with a larger number of potential and real j1939 users - we should face the address claiming and its possible implementation options.

Best regards,
Oliver

#32 lore raw

Re: [RFC v3 5/6] j1939: rename NAME to UUID?

In reply to a message not in the archive
Quoted 3 lines

Kurt, the problem for me is, that you constantly state that your approach is the best. For me it is not.

no userspace counterpart can handle transient conditions like kernel can...

Quoted 5 lines

The major issue in your implementation is the lack of the possibility to simulate several j1939 ECUs on one Linux host talking to each other via virtual CAN busses to create a complete j1939 network. And so far you did not address this request.

Oliver,
I tried to explain already several times that this stack _IS_ capable of having several j1939 ECU's on one linux host, talking to any CAN bus, virtual or physical.
I agree that if this was not the case, your arguments would have been valid.

The major improvement (IMHO) of my in-kernel j1939 stack is that several applications can also contribute to the same ECU, without protocol violations.

side note: this is not even a matter introduced with address claiming :-0

Quoted 1 line

ever done this?

$ ip addr add 192.168.0.1/24 dev eth0
$ ip addr add 192.168.1.1/24 dev eth0

Likewise I do now:

$ ip addr add j1939 0x20 dev can0
$ ip addr add j1939 0x21 dev can0

I see no problem there.

With address claiming:

$ ip addr add j1939 name 0123456789ABCDE0 dev can0
$ ip addr add j1939 name 0123456789ABCDE1 dev can0
and my daemon to choose addresses (posted later today on can-utils)
$ jacd --range 0x20-0x30 0123456789ABCDE0 can0
$ jacd --range 0x20-0x30 0123456789ABCDE1 can0

No, no typos here. both ECU's will resolve conflicts on CAN, on the same host! The second will ECU will finally get 0x21, _as should be_ per J1939.

Oliver,
The way I understand your request, this addressed that. What did I miss up to here?

I skipped a lot of your original email since the issue addressed here seems to be source of misunderstanding.

Kind regards,
Kurt

#33 lore raw

Re: [RFC v3 5/6] j1939: rename NAME to UUID?

In reply to a message not in the archive
Quoted 2 lines · Kurt Van Dijck

I need to git pull the latest updates, and I'll send a revised patchset tomorrow.

Oliver,

I did not forget, but something came in between. next week for sure.

#35 lore raw

Re: [RFC v3 5/6] j1939: rename NAME to UUID?

In reply to a message not in the archive
Quoted 6 lines · Kurt Van Dijck
Quoted 2 lines · Kurt Van Dijck

I need to git pull the latest updates, and I'll send a revised patchset tomorrow.

Oliver,

I did not forget, but something came in between. next week for sure.

Oh, no problem.

I'm constantly working on my reply to your latest answer 8-)

BTW: Do you have any sample code that shows, how the sending (and receiving) of the same BAM message would look like in the static and in the address-claimed case?? I'm not really able to extract this information from your posted documentation.

I would like to know

- what has to be configured before (from the user and the admin)
- how does the application source code look like in the requested four cases

I think we're a bit stuck in the discussion of the 'easy to use' socket API without having real-world examples.

Thanks,
Oliver

#37 lore raw

Re: [RFC v3 5/6] j1939: rename NAME to UUID?

In reply to a message not in the archive
Quoted 17 lines · Oliver Hartkopp
Quoted 6 lines · Kurt Van Dijck
Quoted 2 lines · Kurt Van Dijck

I need to git pull the latest updates, and I'll send a revised patchset tomorrow.

Oliver,

I did not forget, but something came in between. next week for sure.

Oh, no problem.

I'm constantly working on my reply to your latest answer 8-)

BTW: Do you have any sample code that shows, how the sending (and receiving) of the same BAM message would look like in the static and in the address-claimed case?? I'm not really able to extract this information from your posted documentation.

Quoted 7 lines

I would like to know

- what has to be configured before (from the user and the admin)
- how does the application source code look like in the requested four cases

I think we're a bit stuck in the discussion of the 'easy to use' socket API without having real-world examples.

ack.

Since I think I got the stack stable now (and some LOC dropped), I agree that 'simple' real-world examples may document a lot.

Quoted 3 lines

Thanks,
Oliver

Kurt