Skip to contentSkip to conversation

RT-Socket-CAN, TX loopback and further improvements

PATCH In review Wolfgang Grandegger 2 participants 11 messages Nov 14, 2006 – Nov 15, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 RT-Socket-CAN, TX loopback and further improvements

Hi Jan,

attached is a patch implementing the TX loopback, apart from some other fixes and improvements. The TX loopback to foreign local sockets allows to have a net-alike behavior of the CAN bus. Local sockets listening to a device can receive TX messages as well. As discussed, the TX lookback is performed when TX is done (TX done interrupt). As I still think that it will not be used very often, I made it a configurable kernel option. When it's selected, it's _on_ by default and it can be switched off or on again with setsockopt() (to make the Linux Socket-CAN people happy). As long as it's not enabled, it adds little overhead to the driver. If you have no objections I will check it in.

Here is the ChangeLog entry:

2006-11-14 Wolfgang Grandegger <[email protected]>

* ksrc/drivers/can/rtcan_dev.h, ksrc/drivers/can/rtcan_socket.h, ksrc/drivers/can/rtcan_socket.c, ksrc/drivers/can/rtcan_raw.c, ksrc/drivers/can/rtcan_modules.c, ksrc/drivers/can/sja1000/rtcan_sja1000.c, ksrc/drivers/can/mscan/rtcan_mscan.c, ksrc/drivers/can/Kconfig, ksrc/drivers/can/Config.in, src/utils/can/rtcansend.c,
include/rtdm/rtcan.h,: Add feature TX loopback to local sockets.

* ksrc/drivers/can/rtcan_raw.c, include/rtdm/rtcan.h:
Remove locks for the setting and reading of the RX and TX timeout values and add a warning to the documentation.

* src/utils/rtcansend.c: use sendto() by default to avoid binding a default filter and add option "-s" for using bind() and send().

* src/utils/rtcanrecv.c: add option "-R" for relative timestamps.

* ksrc/drivers/can/rtcan_internal.h: use now RTCAN_ASSERT macros when CONFIG_XENO_DRIVERS_RTCAN_DEBUG is set.

* ksrc/drivers/can/mscan/Kconfig, ksrc/drivers/can/sja1000/Kconfig, ksrc/drivers/can/Kconfig: add more help for kernel parameters.

Thanks.

Wolfgang.

#1 lore raw

Attached is the missing patch!

Quoted 48 lines · Wolfgang Grandegger

Hi Jan,

attached is a patch implementing the TX loopback, apart from some other fixes and improvements. The TX loopback to foreign local sockets allows to have a net-alike behavior of the CAN bus. Local sockets listening to a device can receive TX messages as well. As discussed, the TX lookback is performed when TX is done (TX done interrupt). As I still think that it will not be used very often, I made it a configurable kernel option. When it's selected, it's _on_ by default and it can be switched off or on again with setsockopt() (to make the Linux Socket-CAN people happy). As long as it's not enabled, it adds little overhead to the driver. If you have no objections I will check it in.

Here is the ChangeLog entry:

2006-11-14 Wolfgang Grandegger <[email protected]>

* ksrc/drivers/can/rtcan_dev.h, ksrc/drivers/can/rtcan_socket.h, ksrc/drivers/can/rtcan_socket.c, ksrc/drivers/can/rtcan_raw.c, ksrc/drivers/can/rtcan_modules.c, ksrc/drivers/can/sja1000/rtcan_sja1000.c, ksrc/drivers/can/mscan/rtcan_mscan.c, ksrc/drivers/can/Kconfig, ksrc/drivers/can/Config.in, src/utils/can/rtcansend.c,
include/rtdm/rtcan.h,: Add feature TX loopback to local sockets.

* ksrc/drivers/can/rtcan_raw.c, include/rtdm/rtcan.h:
Remove locks for the setting and reading of the RX and TX timeout values and add a warning to the documentation.

* src/utils/rtcansend.c: use sendto() by default to avoid binding a default filter and add option "-s" for using bind() and send().

* src/utils/rtcanrecv.c: add option "-R" for relative timestamps.

* ksrc/drivers/can/rtcan_internal.h: use now RTCAN_ASSERT macros when CONFIG_XENO_DRIVERS_RTCAN_DEBUG is set.

* ksrc/drivers/can/mscan/Kconfig, ksrc/drivers/can/sja1000/Kconfig, ksrc/drivers/can/Kconfig: add more help for kernel parameters.

Thanks.

Wolfgang.

Quoted 6 lines

_______________________________________________
Xenomai-core mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-core

rtdm/rtcan.h +39 -11
Index: include/rtdm/rtcan.h
===================================================================
@@ -120,6 +120,7 @@
  * - Level @b SOL_CAN_RAW : CAN RAW protocol (see @ref CAN_PROTO_RAW)
  *   - Option @ref CAN_RAW_FILTER : CAN filter list
  *   - Option @ref CAN_RAW_ERR_FILTER : CAN error mask
+ *   - Option @ref CAN_RAW_TX_LOOPBACK : CAN TX loopback to local sockets
  *   .
  * .
  * @n
@@ -543,11 +544,9 @@
  * - -EFAULT (It was not possible to access user space memory area at the
  *            specified address.)
  * - -ENOMEM (Not enough memory to fulfill the operation)
- * - -EINVAL (Invalid address family, or invalid length of address structure)
- * - -ENODEV (Invalid CAN interface index)
- * - -EBADF  (Socket is about to be closed)
- * - -EAGAIN (Too many receivers. Old binding (if any) is still active.
- *            Close some sockets and try again.)
+ * - -EINVAL (Invalid length "optlen")
+ * - -ENOSPC (No space to store filter list, check RT-Socket-CAN kernel
+ *            parameters)
  * .
  */
 #define CAN_RAW_FILTER      0x1
@@ -577,16 +576,39 @@
  * Specific return values:
  * - -EFAULT (It was not possible to access user space memory area at the
  *            specified address.)
- * - -ENOMEM (Not enough memory to fulfill the operation)
- * - -EINVAL (Invalid address family, or invalid length of address structure)
- * - -ENODEV (Invalid CAN interface index)
- * - -EBADF  (Socket is about to be closed)
- * - -EAGAIN (Too many receivers. Old binding (if any) is still active.
- *            Close some sockets and try again.)
+ * - -EINVAL (Invalid length "optlen")
  * .
  */
 #define CAN_RAW_ERR_FILTER  0x2
 
+/**
+ * CAN TX loopback
+ *
+ * The TX loopback to other local sockets can be selected with this
+ * @c setsockopt.
+ *
+ * @note The TX loopback feature must be enabled in the kernel and then
+ * the loopback to other local TX sockets is enabled by default.
+ *
+ * @n
+ * @param [in] level @b SOL_CAN_RAW
+ *
+ * @param [in] optname @b CAN_RAW_TX_LOOPBACK
+ *
+ * @param [in] optval Pointer to integer value.
+ *
+ * @param [in] optlen Size of int: sizeof(int).
+ *
+ * Environments: non-RT (RT optional)@n
+ * @n
+ * Specific return values:
+ * - -EFAULT (It was not possible to access user space memory area at the
+ *            specified address.)
+ * - -EINVAL (Invalid length "optlen")
+ * - -EOPNOTSUPP (not supported, check RT-Socket-CAN kernel parameters).
+ */
+#define CAN_RAW_TX_LOOPBACK  0x3
+
 /** @} */
 
 /*!
@@ -933,6 +955,9 @@
  *
  * The default value for a newly created socket is an infinite timeout.
  *
+ * @note The setting of the timeout value is not done atomically to avoid
+ * locks. Please set the value before receiving messages from the socket.
+ *
  * @param [in] arg Pointer to @ref nanosecs_rel_t variable. The value is
  *                interpreted as relative timeout in nanoseconds in case
  *                of a positive value.
@@ -963,6 +988,9 @@
  *
  * The default value for a newly created socket is an infinite timeout.
  *
+ * @note The setting of the timeout value is not done atomically to avoid
+ * locks. Please set the value before sending messages to the socket.
+ *
  * @param [in] arg Pointer to @ref nanosecs_rel_t variable. The value is
  *                interpreted as relative timeout in nanoseconds in case
  *                of a positive value.
ChangeLog +29 -0
Index: ChangeLog
===================================================================
@@ -1,3 +1,32 @@
+2006-11-14  Wolfgang Grandegger  <[email protected]>
+
+	* ksrc/drivers/can/rtcan_dev.h,
+	ksrc/drivers/can/rtcan_socket.h,
+	ksrc/drivers/can/rtcan_socket.c,
+	ksrc/drivers/can/rtcan_raw.c,
+	ksrc/drivers/can/rtcan_modules.c,
+	ksrc/drivers/can/sja1000/rtcan_sja1000.c,
+	ksrc/drivers/can/mscan/rtcan_mscan.c,
+	ksrc/drivers/can/Kconfig,
+	ksrc/drivers/can/Config.in,
+	src/utils/can/rtcansend.c,
+	include/rtdm/rtcan.h,: Add feature TX loopback to local sockets.
+
+	* ksrc/drivers/can/rtcan_raw.c, include/rtdm/rtcan.h:
+	Remove locks for the setting and reading of the RX and TX timeout
+	values and add a warning to the documentation.
+
+	* src/utils/rtcansend.c: use sendto() by default to avoid binding
+	a default filter and add option "-s" for using bind() and send().
+
+	* src/utils/rtcanrecv.c: add option "-R" for relative timestamps.
+
+	* ksrc/drivers/can/rtcan_internal.h: use now RTCAN_ASSERT macros
+	when CONFIG_XENO_DRIVERS_RTCAN_DEBUG is set.
+
+	* ksrc/drivers/can/mscan/Kconfig, ksrc/drivers/can/sja1000/Kconfig,
+	ksrc/drivers/can/Kconfig: add more help for kernel parameters.
+
 2006-11-12  Philippe Gerum  <[email protected]>
 
 	* ksrc/nucleus/heap.c (xnheap_extend): Account for new extents
utils/can/rtcansend.c +43 -13

125 lines not on this page · raw message

utils/can/rtcanrecv.c +17 -8
Index: src/utils/can/rtcanrecv.c
===================================================================
@@ -20,7 +20,8 @@
 	    " -f  --filter=id:mask[:id:mask]... apply filter\n"
 	    " -e  --error=mask      receive error messages\n"
 	    " -t, --timeout=MS      timeout in ms\n"
-	    " -T, --timestamp       with timestamp\n"
+	    " -T, --timestamp       with absolute timestamp\n"
+	    " -R, --timestamp-rel   with relative timestamp\n"
 	    " -v, --verbose         be verbose\n"
 	    " -p, --print=MODULO    print every MODULO message\n"
 	    " -h, --help            this help\n",
@@ -31,7 +32,7 @@
 extern int optind, opterr, optopt;
 
 static int s = -1, verbose = 0, print = 1;
-static nanosecs_rel_t timeout = 0, with_timestamp = 0;
+static nanosecs_rel_t timeout = 0, with_timestamp = 0, timestamp_rel = 0;
 
 RT_TASK rt_task_desc;
 
@@ -87,7 +88,7 @@
     socklen_t addrlen = sizeof(addr);
     struct msghdr msg;
     struct iovec iov;
-    nanosecs_abs_t timestamp;
+    nanosecs_abs_t timestamp, timestamp_prev = 0;
 
     if (with_timestamp) {
 	msg.msg_iov = &iov;
@@ -124,8 +125,13 @@
 
 	if (print && (count % print) == 0) {
 	    printf("#%d: (%d) ", count, addr.can_ifindex);
-	    if (with_timestamp && msg.msg_controllen)
-		printf("%lldns ", timestamp);
+	    if (with_timestamp && msg.msg_controllen) {
+		if (timestamp_rel) {
+		    printf("%lldns ", timestamp - timestamp_prev);
+		    timestamp_prev = timestamp;
+		} else
+		    printf("%lldns ", timestamp);
+	    }
 	    if (frame.can_id & CAN_ERR_FLAG)
 		printf("!0x%08x!", frame.can_id & CAN_ERR_MASK);
 	    else if (frame.can_id & CAN_EFF_FLAG)
@@ -168,6 +174,7 @@
 	{ "error", required_argument, 0, 'e'},
 	{ "timeout", required_argument, 0, 't'},
 	{ "timestamp", no_argument, 0, 'T'},
+	{ "timestamp-rel", no_argument, 0, 'R'},
 	{ 0, 0, 0, 0},
     };
 
@@ -176,7 +183,7 @@
     signal(SIGTERM, cleanup_and_exit);
     signal(SIGINT, cleanup_and_exit);
 
-    while ((opt = getopt_long(argc, argv, "hve:f:t:p:T",
+    while ((opt = getopt_long(argc, argv, "hve:f:t:p:RT",
 			      long_options, NULL)) != -1) {
 	switch (opt) {
 	case 'h':
@@ -217,6 +224,8 @@
 	    timeout = (nanosecs_rel_t)strtoul(optarg, NULL, 0) * 1000000;
 	    break;
 
+	case 'R':
+	    timestamp_rel = 1;
 	case 'T':
 	    with_timestamp = 1;
 	    break;
@@ -242,11 +251,11 @@
     } else {
 	if (verbose)
 	    printf("interface %s\n", argv[optind]);
-	
+
 	strncpy(ifr.ifr_name, argv[optind], IFNAMSIZ);
 	if (verbose)
 	    printf("s=%d, ifr_name=%s\n", s, ifr.ifr_name);
-	
+
 	ret = rt_dev_ioctl(s, SIOCGIFINDEX, &ifr);
 	if (ret < 0) {
 	    fprintf(stderr, "rt_dev_ioctl GET_IFINDEX: %s\n", strerror(-ret));
drivers/can/Kconfig +43 -16

78 lines not on this page · raw message

drivers/can/rtcan_internal.h +2 -3
Index: ksrc/drivers/can/rtcan_internal.h
===================================================================
@@ -34,10 +34,9 @@
 #define LIST_POISON1  ((void *) 0x0)
 #endif
 
-#ifdef CONFIG_RTCAN_CHECKED
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_DEBUG
 #define RTCAN_ASSERT(expr, func) \
-    if (!(expr)) \
-    { \
+    if (!(expr)) { \
         rtdm_printk("Assertion failed! %s:%s:%d %s\n", \
         __FILE__, __FUNCTION__, __LINE__, (#expr)); \
         func \
drivers/can/rtcan_dev.h +5 -1
Index: ksrc/drivers/can/rtcan_dev.h
===================================================================
@@ -141,7 +141,11 @@
 
 #ifdef CONFIG_PROC_FS
     struct proc_dir_entry *proc_root;
-#endif    
+#endif
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+    struct rtcan_skb tx_skb;
+    struct rtcan_socket *tx_socket;
+#endif /* CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK */
 };
 
 
drivers/can/rtcan_module.c +13 -5
Index: ksrc/drivers/can/rtcan_module.c
===================================================================
@@ -176,7 +176,11 @@
      *  0 rtcan0               1 0x00010 1234567890 1234567890 1234567890
      */
     if (!RTCAN_PROC_PRINT("fd Name___________ Filter ErrMask "
-			  "RX_Timeout_ns TX_Timeout_ns RX_BufFull\n"))
+			  "RX_Timeout_ns TX_Timeout_ns RX_BufFull") ||
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+	!RTCAN_PROC_PRINT(" TX_Loopback") ||
+#endif
+	!RTCAN_PROC_PRINT("\n"))
         goto done;
 
     rtdm_lock_get_irqsave(&rtcan_recv_list_lock, lock_ctx);
@@ -196,10 +200,14 @@
 	}
 	rtcan_get_timeout_name(sock->tx_timeout, tx_timeout, 20);
 	rtcan_get_timeout_name(sock->rx_timeout, rx_timeout, 20);
-	if(!RTCAN_PROC_PRINT("%2d %-15s %6d 0x%05x %13s %13s %10d\n", 
-			     context->fd, name, sock->flistlen,
-			     sock->err_mask, rx_timeout, tx_timeout,
-			     sock->rx_buf_full))
+	if (!RTCAN_PROC_PRINT("%2d %-15s %6d 0x%05x %13s %13s %10d",
+			      context->fd, name, sock->flistlen,
+			      sock->err_mask, rx_timeout, tx_timeout,
+			      sock->rx_buf_full) ||
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+	    !RTCAN_PROC_PRINT(" %11d", sock->tx_loopback) ||
+#endif
+	    !RTCAN_PROC_PRINT("\n"))
 	    break;
     }
 
drivers/can/rtcan_socket.c +3 -0
Index: ksrc/drivers/can/rtcan_socket.c
===================================================================
@@ -48,6 +48,9 @@
     sock->flist = NULL;
     sock->err_mask = 0;
     sock->rx_buf_full = 0;
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+    sock->tx_loopback = 1;
+#endif
 
     sock->tx_timeout = RTDM_TIMEOUT_INFINITE;
     sock->rx_timeout = RTDM_TIMEOUT_INFINITE;
drivers/can/rtcan_raw.c +77 -28

183 lines not on this page · raw message

drivers/can/mscan/Kconfig +25 -18

57 lines not on this page · raw message

drivers/can/mscan/rtcan_mscan.c +15 -0
Index: ksrc/drivers/can/mscan/rtcan_mscan.c
===================================================================
@@ -251,6 +251,21 @@
 	regs->cantier = 0;
 	/* Wake up a sender */
 	rtdm_sem_up(&dev->tx_sem);
+
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+	if (dev->tx_socket) {
+	    memcpy((void *)&dev->tx_skb.rb_frame + dev->tx_skb.rb_frame_size,
+		   &timestamp, TIMESTAMP_SIZE);
+
+	    if (recv_lock_free) {
+		recv_lock_free = 0;
+		rtdm_lock_get(&rtcan_recv_list_lock);
+		rtdm_lock_get(&rtcan_socket_lock);
+	    }
+
+	    rtcan_tx_loopback(dev);
+	}
+#endif /* CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK */
     }
 
     /* Wakeup interrupt?  */
drivers/can/rtcan_socket.h +4 -0
Index: ksrc/drivers/can/rtcan_socket.h
===================================================================
@@ -167,6 +167,10 @@
     uint32_t            rx_buf_full;
 
     struct rtcan_filter_list *flist;
+
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+    int tx_loopback;
+#endif
 };
 
 
drivers/can/rtcan_raw.h +4 -0
Index: ksrc/drivers/can/rtcan_raw.h
===================================================================
@@ -32,6 +32,10 @@
 
 void rtcan_rcv(struct rtcan_device *rtcandev, struct rtcan_skb *skb);
 
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+void rtcan_tx_loopback(struct rtcan_device *rtcandev);
+#endif /* CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK */
+
 #ifdef CONFIG_PROC_FS
 int __init rtcan_raw_proto_register(void);
 void __exit rtcan_raw_proto_unregister(void);
drivers/can/Config.in +1 -0
Index: ksrc/drivers/can/Config.in
===================================================================
@@ -9,6 +9,7 @@
 
 if [ "$CONFIG_XENO_DRIVERS_RTCAN" != "n" ]; then 
    bool 'Enable debug output' CONFIG_XENO_DRIVERS_RTCAN_DEBUG
+   bool 'Enable TX loopback to local sockets' CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
    int 'Size of receive ring buffers (must be 2^N)' CONFIG_XENO_DRIVERS_RTCAN_RXBUF_SIZE 1024
    int 'Maximum number of devices' CONFIG_XENO_DRIVERS_RTCAN_MAX_DEVICES 4
    int 'Maximum number of receive filters per device' CONFIG_XENO_DRIVERS_RTCAN_MAX_RECEIVERS 16
drivers/can/sja1000/Kconfig +23 -0
Index: ksrc/drivers/can/sja1000/Kconfig
===================================================================
@@ -5,7 +5,12 @@
 config XENO_DRIVERS_RTCAN_SJA1000_ISA
 	depends on XENO_DRIVERS_RTCAN_SJA1000
 	tristate "Standard ISA controllers"
+	help
 
+	This driver is for CAN devices connected to the ISA bus of a PC
+	or a PC/104 system. The I/O port, interrupt number and a few other
+	hardware specific parameters can be defined via module parameters.
+
 config XENO_DRIVERS_RTCAN_SJA1000_ISA_MAX_DEV
 	depends on XENO_DRIVERS_RTCAN_SJA1000_ISA
 	int "Maximum number of controllers"
@@ -14,7 +19,12 @@
 config XENO_DRIVERS_RTCAN_SJA1000_MEM
 	depends on XENO_DRIVERS_RTCAN_SJA1000
 	tristate "Memory mapped controllers"
+	help
 
+	This driver is for memory mapped CAN devices. The memory address,
+	interrupt number and a few other hardware specific parameters can
+	be defined via module parameters.
+
 config XENO_DRIVERS_RTCAN_SJA1000_MEM_MAX_DEV
 	depends on XENO_DRIVERS_RTCAN_SJA1000_MEM
 	int "Maximum number of controllers"
@@ -23,7 +33,20 @@
 config XENO_DRIVERS_RTCAN_SJA1000_PEAK_PCI
 	depends on XENO_DRIVERS_RTCAN_SJA1000
 	tristate "PEAK PCI Card"
+	help
 
+	This driver is for the PCAN PCI, the PC-PCI CAN plug-in card (1 or
+	2 channel) from PEAK Systems (http://www.peak-system.com). To get
+	the second channel working, Xenomai's shared interrupt support
+	must be enabled.
+
 config XENO_DRIVERS_RTCAN_SJA1000_PEAK_DNG
 	depends on XENO_DRIVERS_RTCAN_SJA1000
 	tristate "PEAK Parallel Port Dongle"
+	help
+
+	This driver is for the PCAN Dongle, the PC parallel port to CAN
+	converter from PEAK Systems (http://www.peak-system.com). You need
+	to disable parallel port support in the kernel (CONFIG_PARPORT) for
+	proper operation. The interface type (SP or EPP), I/O port and
+	interrupt number should be defined via module parameters.
\ No newline at end of file
drivers/can/sja1000/rtcan_sja1000.c +17 -1
Index: ksrc/drivers/can/sja1000/rtcan_sja1000.c
===================================================================
@@ -312,11 +312,27 @@
 	}
 
         /* Transmit Interrupt? */
-        if (irq_source & SJA_IR_TI)
+        if (irq_source & SJA_IR_TI) {
             /* Wake up a sender */
             rtdm_sem_up(&dev->tx_sem);
 
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+	    if (dev->tx_socket) {
+		/* Copy timestamp to skb */
+		memcpy((void *)&dev->tx_skb.rb_frame + dev->tx_skb.rb_frame_size,
+		       &timestamp, TIMESTAMP_SIZE);
 
+	    if (recv_lock_free) {
+		recv_lock_free = 0;
+		rtdm_lock_get(&rtcan_recv_list_lock);
+		rtdm_lock_get(&rtcan_socket_lock);
+	    }
+
+	    rtcan_tx_loopback(dev);
+	    }
+#endif /* CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK */
+	}
+
         /* Receive Interrupt? */
         if (irq_source & SJA_IR_RI) {
 
#2 lore raw

Hi Wolfgang,

some hint in advance for future patches (not only concerning yours):

Annotated patches are easier to read. I hacked my configuration into the wiki on how to generate those via svn diff. Please have a look at

http://www.xenomai.org/index.php/Teaching_-p_to_svn_diff
(Referenced by http://www.xenomai.org/index.php/Contributing_Patches)

Quoted 16 lines · Wolfgang Grandegger

Attached is the missing patch!

Quoted 13 lines · Wolfgang Grandegger

Hi Jan,

attached is a patch implementing the TX loopback, apart from some other fixes and improvements. The TX loopback to foreign local sockets allows to have a net-alike behavior of the CAN bus. Local sockets listening to a device can receive TX messages as well. As discussed, the TX lookback is performed when TX is done (TX done interrupt). As I still think that it will not be used very often, I made it a configurable kernel option. When it's selected, it's _on_ by default and it can be switched off or on again with setsockopt() (to make the Linux Socket-CAN people happy). As long as it's not enabled, it adds little overhead to the driver. If you have no objections I will check it in.

I'm still not happy with the whole situation but I guess this way is the best compromise that can be found without diverging too much from their path. Do you have any news on how the LLCF group now implements this?

...

Quoted 27 lines

Index: src/utils/can/rtcansend.c
===================================================================

utils/can/rtcansend.c +4 -1
@@ -26,7 +26,9 @@
 	    " -l  --loop=COUNT      send message COUNT times\n"
 	    " -c, --count           message count in data[0-3]\n"
 	    " -d, --delay=MS        delay in ms (default = 1ms)\n"
+	    " -s, --send            use send instead of sendto\n"
 	    " -t, --timeout=MS      timeout in ms\n"
+	    " -T, --tx-loopback=0|1 switch TX loopback off or on\n"
 	    " -v, --verbose         be verbose\n"
 	    " -p, --print=MODULO    print every MODULO message\n"
 	    " -h, --help            this help\n",
@@ -37,9 +39,10 @@
 RT_TASK rt_task_desc;
 
 static int s=-1, dlc=0, rtr=0, extended=0, verbose=0, loops=1, delay=1000000;
-static int count=0, print=1;
+static int count=0, print=1, use_send=0, tx_loopback=-1;
 static nanosecs_rel_t timeout = 0;
 static struct can_frame frame;
+static struct sockaddr_can to_addr;
 
 
 void cleanup(void)

utils/can/rtcansend.c line 88

Quoted 10 lines
utils/can/rtcansend.c +6 -1
@@ -77,16 +80,21 @@
         rt_task_sleep(delay);
 	if (count)
 	    memcpy(&frame.data[0], &i, sizeof(i));
-        ret = rt_dev_send(s, (void *)&frame, sizeof(can_frame_t), 0);
+	/* Note: sendto avoids the definiton of a receive filter list */ 
+	if (use_send)
+	    ret = rt_dev_send(s, (void *)&frame, sizeof(can_frame_t), 0);
+	else
+	    ret = rt_dev_sendto(s, (void *)&frame, sizeof(can_frame_t), 0,
+				(struct sockaddr *)&to_addr, sizeof(to_addr));

Hmm, I haven't checked the code, but isn't it possible to register a filter list of zero elements? Wouldn't this have the same effect (/wrt the RX path) than not binding at all? Or what do you avoid this way?

Quoted 23 lines
utils/can/rtcansend.c +2 -3
 	if (ret < 0) {
 	    switch (ret) {
 	    case -ETIMEDOUT:
 		if (verbose)
-		    printf("rt_dev_send: timed out");
+		    printf("rt_dev_send(to): timed out");
 		break;
 	    case -EBADF:
 		if (verbose)
-		    printf("rt_dev_send: aborted because socket was closed");
+		    printf("rt_dev_send(to): aborted because socket was closed");
 		break;
 	    default:
 		fprintf(stderr, "rt_dev_send: %s\n", strerror(-ret));
@@ -111,7 +119,6 @@
 
 int main(int argc, char **argv)
 {
-    struct sockaddr_can addr;
     int i, opt, ret;
     struct ifreq ifr;
     char name[32];

utils/can/rtcansend.c line 138

Quoted 6 lines
utils/can/rtcansend.c +2 -0
@@ -126,7 +133,9 @@
 	{ "print", required_argument, 0, 'p'},
 	{ "loop", required_argument, 0, 'l'},
 	{ "delay", required_argument, 0, 'd'},
+	{ "send", no_argument, 0, 's'},
 	{ "timeout", required_argument, 0, 't'},
+	{ "tx-loopbcak", required_argument, 0, 'T'},

Typo.

Quoted 26 lines

Index: ksrc/drivers/can/Kconfig
===================================================================

drivers/can/Kconfig +8 -7
@@ -1,35 +1,62 @@
 menu "CAN drivers"
 
 config XENO_DRIVERS_RTCAN
-       depends on XENO_SKIN_RTDM
-       tristate "RT-Socket-CAN, CAN raw socket interface"
-       help
-       RT-Socket-CAN is a real-time socket interface for CAN controllers.
+	depends on XENO_SKIN_RTDM
+	tristate "RT-Socket-CAN, CAN raw socket interface"
+	help
+	RT-Socket-CAN is a real-time socket interface for CAN controllers.
 
 config XENO_DRIVERS_RTCAN_DEBUG
-       depends on XENO_DRIVERS_RTCAN
-       bool "Enable debug output"
-       default y
+	depends on XENO_DRIVERS_RTCAN
+	bool "Enable debug output"
+	default y
+	help
 

drivers/can/Kconfig line 24

Quoted 10 lines
drivers/can/Kconfig +10 -0
+	This option activates debugging checks and enhanced output for the
+	RT-Socket-CAN driver. It also allows to list the hardware registers
+	of the registered CAN controllers. It is a recommended option for
+	getting started and analysing potential problems. For production
+	purposes, it should be switched off (for the sake of latency).
+
+config XENO_DRIVERS_RTCAN_TX_LOOPBACK
+	depends on XENO_DRIVERS_RTCAN
+	bool "Enable TX loopback to local sockets"
+	default n

Default is "n", so this line is redundant.

Quoted 22 lines

Index: ksrc/drivers/can/rtcan_raw.c
===================================================================

drivers/can/rtcan_raw.c +5 -1
@@ -68,7 +68,7 @@
 }
 
 
-static inline void rtcan_rcv_deliver(struct rtcan_recv *recv_listener, 
+static void rtcan_rcv_deliver(struct rtcan_recv *recv_listener, 
 				     struct rtcan_skb *skb)
 {
     int size_free;
@@ -152,7 +152,50 @@
     }
 }
 
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
 
+static void rtcan_tx_push(struct rtcan_device *dev, struct rtcan_socket *sock,
+			  can_frame_t *frame)
+{

drivers/can/rtcan_raw.c line 169

Quoted 10 lines
drivers/can/rtcan_raw.c +10 -0
+    struct rtcan_rb_frame *rb_frame = &dev->tx_skb.rb_frame;
+
+    RTCAN_ASSERT(dev->tx_socket == 0,
+		 rtdm_printk("(%d) TX skb still in use", dev->ifindex););
+
+    rb_frame->can_id = frame->can_id;
+    rb_frame->can_dlc = frame->can_dlc;
+    dev->tx_skb.rb_frame_size = EMPTY_RB_FRAME_SIZE;
+    if (frame->can_dlc && !(frame->can_id & CAN_RTR_FLAG)) {
+	memcpy(rb_frame->data, frame->data, frame->can_dlc);

I bet blindly copying always 8 bytes will be even more efficient than the two lines above... :)

Quoted 15 lines
drivers/can/rtcan_raw.c +15 -0
+	dev->tx_skb.rb_frame_size += frame->can_dlc;
+    }
+    rb_frame->can_ifindex = dev->ifindex;
+    dev->tx_socket = sock;
+}
+
+void rtcan_tx_loopback(struct rtcan_device *dev)
+{
+    /* Entry in reception list, begin with head */
+    struct rtcan_recv *recv_listener = dev->recv_list;
+    struct rtcan_rb_frame *frame = &dev->tx_skb.rb_frame;
+
+    while (recv_listener != NULL) {
+	dev->rx_count++;
+	if ((dev->tx_socket != recv_listener->sock) &&

drivers/can/rtcan_raw.c line 194

Quoted 10 lines
drivers/can/rtcan_raw.c +10 -0
+	    rtcan_accept_msg(frame->can_id, &recv_listener->can_filter)) {
+	    recv_listener->match_count++;
+	    rtcan_rcv_deliver(recv_listener, &dev->tx_skb);
+	}
+	recv_listener = recv_listener->next;
+    }
+    dev->tx_socket = NULL;
+}
+
+EXPORT_SYMBOL(rtcan_tx_loopback);

EXPORT_SYMBOL_GPL, please.

So this loopback model builds (for now?) on the assumptions that no driver will ever queue more than one outgoing frame per device in hardware, right?

drivers/can/rtcan_raw.c line 944

Quoted 9 lines
drivers/can/rtcan_raw.c +5 -0
@@ -894,6 +937,12 @@
     /* We got access */
 
 
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+    /* Push message onto stack for loopback when TX done */
+    if (sock->tx_loopback)
+	rtcan_tx_push(dev, sock, frame);
+#endif /* CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK */

Hmm, I would prefer to define something like

if (rtcan_tx_loopback_enabled(sock))

    rtcan_tx_push(dev, sock, frame);

with rtcan_tx_loopback_enabled resolving to 0 in the configured-out case. Thus some #ifdefs could be moved from the code to central places in header files.

drivers/can/mscan/rtcan_mscan.c line 256

Quoted 11 lines

Index: ksrc/drivers/can/mscan/rtcan_mscan.c
===================================================================

drivers/can/mscan/rtcan_mscan.c +3 -0
@@ -251,6 +251,21 @@
 	regs->cantier = 0;
 	/* Wake up a sender */
 	rtdm_sem_up(&dev->tx_sem);
+
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+	if (dev->tx_socket) {

Same here. A macro like rtcan_tx_loopback_pending(dev) would avoid the #ifdef.

drivers/can/rtcan_raw.h line 36

Quoted 10 lines

Index: ksrc/drivers/can/rtcan_raw.h
===================================================================

drivers/can/rtcan_raw.h +2 -0
@@ -32,6 +32,10 @@
 
 void rtcan_rcv(struct rtcan_device *rtcandev, struct rtcan_skb *skb);
 
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+void rtcan_tx_loopback(struct rtcan_device *rtcandev);

Prototypes don't need the #ifdef (same applies to the CONFIG_PROC_FS below, BTW).

Quoted 5 lines
drivers/can/rtcan_raw.h +2 -0
+#endif /* CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK */
+
 #ifdef CONFIG_PROC_FS
 int __init rtcan_raw_proto_register(void);
 void __exit rtcan_raw_proto_unregister(void);

...

Quoted 16 lines

Index: ksrc/drivers/can/sja1000/rtcan_sja1000.c
===================================================================

drivers/can/sja1000/rtcan_sja1000.c +4 -1
@@ -312,11 +312,27 @@
 	}
 
         /* Transmit Interrupt? */
-        if (irq_source & SJA_IR_TI)
+        if (irq_source & SJA_IR_TI) {
             /* Wake up a sender */
             rtdm_sem_up(&dev->tx_sem);
 
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+	    if (dev->tx_socket) {
+		/* Copy timestamp to skb */

drivers/can/sja1000/rtcan_sja1000.c line 331

Quoted 10 lines
drivers/can/sja1000/rtcan_sja1000.c +9 -0
+		memcpy((void *)&dev->tx_skb.rb_frame + dev->tx_skb.rb_frame_size,
+		       &timestamp, TIMESTAMP_SIZE);
 
+	    if (recv_lock_free) {
+		recv_lock_free = 0;
+		rtdm_lock_get(&rtcan_recv_list_lock);
+		rtdm_lock_get(&rtcan_socket_lock);
+	    }
+
+	    rtcan_tx_loopback(dev);

[I just noticed the redundancy with the MSCAN code]

I think the timestamp should rather be passed to rtcan_tc_loopback and set there. Or, if passing that value increases the code size needlessly, rtcan_tx_loopback should be defined like

static inline void
rtcan_tx_loopback(struct rtcan_device *dev, nanosecs_abs_t timestamp)

{
    <set timestamp>
    __rtcan_tx_loopback(dev);
}

where __rtcan_tx_loopback is the uninlined code. That makes the driver code leaner. Locking will hopefully change anyway, so nothing to do on this part for now.

drivers/can/sja1000/rtcan_sja1000.c line 338

Quoted 7 lines
drivers/can/sja1000/rtcan_sja1000.c +4 -0
+	    }
+#endif /* CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK */
+	}
+
         /* Receive Interrupt? */
         if (irq_source & SJA_IR_RI) {
 

Last remark: For the sake of completeness, tx-loopback should also be implemented in the virtual CAN driver.

Jan

#3 lore raw
Quoted 9 lines · Jan Kiszka

Hi Wolfgang,

some hint in advance for future patches (not only concerning yours):

Annotated patches are easier to read. I hacked my configuration into the wiki on how to generate those via svn diff. Please have a look at

http://www.xenomai.org/index.php/Teaching_-p_to_svn_diff
(Referenced by http://www.xenomai.org/index.php/Contributing_Patches)

Done.

Quoted 18 lines
Quoted 16 lines · Wolfgang Grandegger

Attached is the missing patch!

Quoted 13 lines · Wolfgang Grandegger

Hi Jan,

attached is a patch implementing the TX loopback, apart from some other fixes and improvements. The TX loopback to foreign local sockets allows to have a net-alike behavior of the CAN bus. Local sockets listening to a device can receive TX messages as well. As discussed, the TX lookback is performed when TX is done (TX done interrupt). As I still think that it will not be used very often, I made it a configurable kernel option. When it's selected, it's _on_ by default and it can be switched off or on again with setsockopt() (to make the Linux Socket-CAN people happy). As long as it's not enabled, it adds little overhead to the driver. If you have no objections I will check it in.

Quoted 3 lines

I'm still not happy with the whole situation but I guess this way is the best compromise that can be found without diverging too much from their path. Do you have any news on how the LLCF group now implements this?

No. For the time being I want to offer this option to the users mainly to get feedback and gain experience. The API is defined and the user should therefore not suffer from future internal re-implementations.

Quoted 26 lines

...

Quoted 25 lines

Index: src/utils/can/rtcansend.c
===================================================================

utils/can/rtcansend.c +4 -1
@@ -26,7 +26,9 @@
 	    " -l  --loop=COUNT      send message COUNT times\n"
 	    " -c, --count           message count in data[0-3]\n"
 	    " -d, --delay=MS        delay in ms (default = 1ms)\n"
+	    " -s, --send            use send instead of sendto\n"
 	    " -t, --timeout=MS      timeout in ms\n"
+	    " -T, --tx-loopback=0|1 switch TX loopback off or on\n"
 	    " -v, --verbose         be verbose\n"
 	    " -p, --print=MODULO    print every MODULO message\n"
 	    " -h, --help            this help\n",
@@ -37,9 +39,10 @@
 RT_TASK rt_task_desc;
 
 static int s=-1, dlc=0, rtr=0, extended=0, verbose=0, loops=1, delay=1000000;
-static int count=0, print=1;
+static int count=0, print=1, use_send=0, tx_loopback=-1;
 static nanosecs_rel_t timeout = 0;
 static struct can_frame frame;
+static struct sockaddr_can to_addr;
 
 
Quoted 16 lines

utils/can/rtcansend.c line 88

Quoted 12 lines
utils/can/rtcansend.c +6 -1
 void cleanup(void)
@@ -77,16 +80,21 @@
         rt_task_sleep(delay);
 	if (count)
 	    memcpy(&frame.data[0], &i, sizeof(i));
-        ret = rt_dev_send(s, (void *)&frame, sizeof(can_frame_t), 0);
+	/* Note: sendto avoids the definiton of a receive filter list */ 
+	if (use_send)
+	    ret = rt_dev_send(s, (void *)&frame, sizeof(can_frame_t), 0);
+	else
+	    ret = rt_dev_sendto(s, (void *)&frame, sizeof(can_frame_t), 0,
+				(struct sockaddr *)&to_addr, sizeof(to_addr));

Hmm, I haven't checked the code, but isn't it possible to register a filter list of zero elements? Wouldn't this have the same effect (/wrt the RX path) than not binding at all? Or what do you avoid this way?

If you call bind, one filter entry accepting all messages is added to the filter list of the device. This has been discussed on the Socket-CAN-ML in the thread https://lists.berlios.de/pipermail/socketcan-core/2006-July/000272.html
and the result was, that rt_dev_sendto could be used to avoid the bind. I don't know if a zero elements list is legal. I will ask.

Quoted 90 lines
Quoted 23 lines
utils/can/rtcansend.c +2 -3
 	if (ret < 0) {
 	    switch (ret) {
 	    case -ETIMEDOUT:
 		if (verbose)
-		    printf("rt_dev_send: timed out");
+		    printf("rt_dev_send(to): timed out");
 		break;
 	    case -EBADF:
 		if (verbose)
-		    printf("rt_dev_send: aborted because socket was closed");
+		    printf("rt_dev_send(to): aborted because socket was closed");
 		break;
 	    default:
 		fprintf(stderr, "rt_dev_send: %s\n", strerror(-ret));
@@ -111,7 +119,6 @@
 
 int main(int argc, char **argv)
 {
-    struct sockaddr_can addr;
     int i, opt, ret;
     struct ifreq ifr;
     char name[32];

utils/can/rtcansend.c line 138

Quoted 6 lines
utils/can/rtcansend.c +2 -0
@@ -126,7 +133,9 @@
 	{ "print", required_argument, 0, 'p'},
 	{ "loop", required_argument, 0, 'l'},
 	{ "delay", required_argument, 0, 'd'},
+	{ "send", no_argument, 0, 's'},
 	{ "timeout", required_argument, 0, 't'},
+	{ "tx-loopbcak", required_argument, 0, 'T'},

Typo.

Quoted 26 lines

Index: ksrc/drivers/can/Kconfig
===================================================================

drivers/can/Kconfig +8 -7
@@ -1,35 +1,62 @@
 menu "CAN drivers"
 
 config XENO_DRIVERS_RTCAN
-       depends on XENO_SKIN_RTDM
-       tristate "RT-Socket-CAN, CAN raw socket interface"
-       help
-       RT-Socket-CAN is a real-time socket interface for CAN controllers.
+	depends on XENO_SKIN_RTDM
+	tristate "RT-Socket-CAN, CAN raw socket interface"
+	help
+	RT-Socket-CAN is a real-time socket interface for CAN controllers.
 
 config XENO_DRIVERS_RTCAN_DEBUG
-       depends on XENO_DRIVERS_RTCAN
-       bool "Enable debug output"
-       default y
+	depends on XENO_DRIVERS_RTCAN
+	bool "Enable debug output"
+	default y
+	help
 

drivers/can/Kconfig line 24

Quoted 10 lines
drivers/can/Kconfig +10 -0
+	This option activates debugging checks and enhanced output for the
+	RT-Socket-CAN driver. It also allows to list the hardware registers
+	of the registered CAN controllers. It is a recommended option for
+	getting started and analysing potential problems. For production
+	purposes, it should be switched off (for the sake of latency).
+
+config XENO_DRIVERS_RTCAN_TX_LOOPBACK
+	depends on XENO_DRIVERS_RTCAN
+	bool "Enable TX loopback to local sockets"
+	default n

Default is "n", so this line is redundant.

Quoted 19 lines

Index: ksrc/drivers/can/rtcan_raw.c
===================================================================

drivers/can/rtcan_raw.c +2 -1
@@ -68,7 +68,7 @@
 }
 
 
-static inline void rtcan_rcv_deliver(struct rtcan_recv *recv_listener, 
+static void rtcan_rcv_deliver(struct rtcan_recv *recv_listener, 
 				     struct rtcan_skb *skb)
 {
     int size_free;
@@ -152,7 +152,50 @@
     }
 }
 
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
 
Quoted 16 lines

drivers/can/rtcan_raw.c line 169

Quoted 13 lines
drivers/can/rtcan_raw.c +13 -0
+static void rtcan_tx_push(struct rtcan_device *dev, struct rtcan_socket *sock,
+			  can_frame_t *frame)
+{
+    struct rtcan_rb_frame *rb_frame = &dev->tx_skb.rb_frame;
+
+    RTCAN_ASSERT(dev->tx_socket == 0,
+		 rtdm_printk("(%d) TX skb still in use", dev->ifindex););
+
+    rb_frame->can_id = frame->can_id;
+    rb_frame->can_dlc = frame->can_dlc;
+    dev->tx_skb.rb_frame_size = EMPTY_RB_FRAME_SIZE;
+    if (frame->can_dlc && !(frame->can_id & CAN_RTR_FLAG)) {
+	memcpy(rb_frame->data, frame->data, frame->can_dlc);

I bet blindly copying always 8 bytes will be even more efficient than the two lines above... :)

Be aware that rb_frame->data is not aligned on 4 bytes. But likely you are right.

Quoted 19 lines
Quoted 19 lines
drivers/can/rtcan_raw.c +19 -0
+	dev->tx_skb.rb_frame_size += frame->can_dlc;
+    }
+    rb_frame->can_ifindex = dev->ifindex;
+    dev->tx_socket = sock;
+}
+
+void rtcan_tx_loopback(struct rtcan_device *dev)
+{
+    /* Entry in reception list, begin with head */
+    struct rtcan_recv *recv_listener = dev->recv_list;
+    struct rtcan_rb_frame *frame = &dev->tx_skb.rb_frame;
+
+    while (recv_listener != NULL) {
+	dev->rx_count++;
+	if ((dev->tx_socket != recv_listener->sock) &&
+	    rtcan_accept_msg(frame->can_id, &recv_listener->can_filter)) {
+	    recv_listener->match_count++;
+	    rtcan_rcv_deliver(recv_listener, &dev->tx_skb);
+	}
Quoted 8 lines

drivers/can/rtcan_raw.c line 194

Quoted 6 lines
drivers/can/rtcan_raw.c +6 -0
+	recv_listener = recv_listener->next;
+    }
+    dev->tx_socket = NULL;
+}
+
+EXPORT_SYMBOL(rtcan_tx_loopback);

EXPORT_SYMBOL_GPL, please.

OK, forgot that.

Quoted 4 lines

So this loopback model builds (for now?) on the assumptions that no driver will ever queue more than one outgoing frame per device in hardware, right?

Yes! But do we really want to queue more than one message in hardware? It will break real-time to some extend. Furthermore I have done some test with MSCAN-MPC5200 filling up 3 buffers and I did not realized any significant improvement of the throughput, also at 1 MB/sec. Support of more than one buffer requires some device dependent re-implementation, not only for the TX loopback. But that's feasible as well.

Quoted 13 lines

drivers/can/rtcan_raw.c line 944

Quoted 10 lines
drivers/can/rtcan_raw.c +5 -0
@@ -894,6 +937,12 @@
     /* We got access */
 
 
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+    /* Push message onto stack for loopback when TX done */
+    if (sock->tx_loopback)
+	rtcan_tx_push(dev, sock, frame);
+#endif /* CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK */

Hmm, I would prefer to define something like

Quoted 6 lines

if (rtcan_tx_loopback_enabled(sock))

    rtcan_tx_push(dev, sock, frame);

with rtcan_tx_loopback_enabled resolving to 0 in the configured-out case. Thus some #ifdefs could be moved from the code to central places in header files.

OK, here I can avoid the #ifdef's ...

Quoted 6 lines
Quoted 6 lines

Index: ksrc/drivers/can/mscan/rtcan_mscan.c
===================================================================

drivers/can/mscan/rtcan_mscan.c +0 -0
@@ -251,6 +251,21 @@
 	regs->cantier = 0;
Quoted 8 lines

drivers/can/mscan/rtcan_mscan.c line 256

Quoted 5 lines
drivers/can/mscan/rtcan_mscan.c +3 -0
 	/* Wake up a sender */
 	rtdm_sem_up(&dev->tx_sem);
+
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+	if (dev->tx_socket) {

Same here. A macro like rtcan_tx_loopback_pending(dev) would avoid the #ifdef.

...but not here. Or does the optimizer remove the if block?

Quoted 6 lines
Quoted 6 lines

Index: ksrc/drivers/can/rtcan_raw.h
===================================================================

drivers/can/rtcan_raw.h +0 -0
@@ -32,6 +32,10 @@
 
Quoted 7 lines

drivers/can/rtcan_raw.h line 36

Quoted 4 lines
drivers/can/rtcan_raw.h +2 -0
 void rtcan_rcv(struct rtcan_device *rtcandev, struct rtcan_skb *skb);
 
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+void rtcan_tx_loopback(struct rtcan_device *rtcandev);

Prototypes don't need the #ifdef (same applies to the CONFIG_PROC_FS below, BTW).

OK.

Quoted 43 lines
Quoted 5 lines
drivers/can/rtcan_raw.h +2 -0
+#endif /* CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK */
+
 #ifdef CONFIG_PROC_FS
 int __init rtcan_raw_proto_register(void);
 void __exit rtcan_raw_proto_unregister(void);

...

Quoted 16 lines

Index: ksrc/drivers/can/sja1000/rtcan_sja1000.c
===================================================================

drivers/can/sja1000/rtcan_sja1000.c +4 -1
@@ -312,11 +312,27 @@
 	}
 
         /* Transmit Interrupt? */
-        if (irq_source & SJA_IR_TI)
+        if (irq_source & SJA_IR_TI) {
             /* Wake up a sender */
             rtdm_sem_up(&dev->tx_sem);
 
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+	    if (dev->tx_socket) {
+		/* Copy timestamp to skb */

drivers/can/sja1000/rtcan_sja1000.c line 331

Quoted 10 lines
drivers/can/sja1000/rtcan_sja1000.c +9 -0
+		memcpy((void *)&dev->tx_skb.rb_frame + dev->tx_skb.rb_frame_size,
+		       &timestamp, TIMESTAMP_SIZE);
 
+	    if (recv_lock_free) {
+		recv_lock_free = 0;
+		rtdm_lock_get(&rtcan_recv_list_lock);
+		rtdm_lock_get(&rtcan_socket_lock);
+	    }
+
+	    rtcan_tx_loopback(dev);

[I just noticed the redundancy with the MSCAN code]

I think the timestamp should rather be passed to rtcan_tc_loopback and set there. Or, if passing that value increases the code size needlessly, rtcan_tx_loopback should be defined like

static inline void
rtcan_tx_loopback(struct rtcan_device *dev, nanosecs_abs_t timestamp)

Quoted 8 lines
{
    <set timestamp>
    __rtcan_tx_loopback(dev);
}

where __rtcan_tx_loopback is the uninlined code. That makes the driver code leaner. Locking will hopefully change anyway, so nothing to do on this part for now.

Or do it directly in rtcan_tx_loopback() and rtcan_recv(). Would it be OK to read the time in these functions as well or should it be done, as is, at the beginning of the ISR? I have already planned some similar rewrite when Xenomai 2.3. is out, hopefully together with the new locking.

Quoted 11 lines

drivers/can/sja1000/rtcan_sja1000.c line 338

Quoted 7 lines
drivers/can/sja1000/rtcan_sja1000.c +4 -0
+	    }
+#endif /* CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK */
+	}
+
         /* Receive Interrupt? */
         if (irq_source & SJA_IR_RI) {
 

Last remark: For the sake of completeness, tx-loopback should also be implemented in the virtual CAN driver.

Ah, I missed that.

Thanks for feedback.

Wolfgang.

#4 lore raw
Quoted 36 lines · Wolfgang Grandegger
Quoted 12 lines

drivers/can/rtcan_raw.c line 944

Quoted 9 lines
drivers/can/rtcan_raw.c +5 -0
@@ -894,6 +937,12 @@
     /* We got access */
 
 
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+    /* Push message onto stack for loopback when TX done */
+    if (sock->tx_loopback)
+    rtcan_tx_push(dev, sock, frame);
+#endif /* CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK */

Hmm, I would prefer to define something like

Quoted 6 lines

if (rtcan_tx_loopback_enabled(sock))

    rtcan_tx_push(dev, sock, frame);

with rtcan_tx_loopback_enabled resolving to 0 in the configured-out case. Thus some #ifdefs could be moved from the code to central places in header files.

OK, here I can avoid the #ifdef's ...

Quoted 6 lines
Quoted 6 lines

Index: ksrc/drivers/can/mscan/rtcan_mscan.c
===================================================================

drivers/can/mscan/rtcan_mscan.c (working copy) renamed from drivers/can/mscan/rtcan_mscan.c (revision 1834) +0 -0
@@ -251,6 +251,21 @@
     regs->cantier = 0;
Quoted 8 lines

drivers/can/mscan/rtcan_mscan.c (working copy) line 256

Quoted 5 lines
drivers/can/mscan/rtcan_mscan.c (working copy) renamed from drivers/can/mscan/rtcan_mscan.c (revision 1834) +3 -0
     /* Wake up a sender */
     rtdm_sem_up(&dev->tx_sem);
+
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+    if (dev->tx_socket) {

Same here. A macro like rtcan_tx_loopback_pending(dev) would avoid the #ifdef.

Quoted 1 line

...but not here. Or does the optimizer remove the if block?

AFAIK, all relevant gcc versions kill blocks like

if (0) {
	...
}

from the binary. But one problem might appear: the block content must be compilable in any case...

Quoted 11 lines
Quoted 11 lines

I think the timestamp should rather be passed to rtcan_tc_loopback and set there. Or, if passing that value increases the code size needlessly, rtcan_tx_loopback should be defined like

static inline void
rtcan_tx_loopback(struct rtcan_device *dev, nanosecs_abs_t timestamp)

{
    <set timestamp>
    __rtcan_tx_loopback(dev);
}
Quoted 8 lines
Quoted 3 lines

where __rtcan_tx_loopback is the uninlined code. That makes the driver code leaner. Locking will hopefully change anyway, so nothing to do on this part for now.

Or do it directly in rtcan_tx_loopback() and rtcan_recv(). Would it be OK to read the time in these functions as well or should it be done, as is, at the beginning of the ISR? I have already planned some similar rewrite when Xenomai 2.3. is out, hopefully together with the new locking.

A few cycles offset of the timestamp doesn't matter here. We should just avoid reading it more than once as this can be a costly operation on some systems.

Jan

#8 lore raw
In reply to Jan Kiszka

Hi Jan,

Quoted 1 line · Jan Kiszka

Hi Wolfgang,

[...deletions...]

Quoted 2 lines

Last remark: For the sake of completeness, tx-loopback should also be implemented in the virtual CAN driver.

The virtual CAN driver already works like TX loopback. It routes TX messages to local foreign sockets (but without sending them to any bus).

Wolfgang.

#5 lore raw
In reply to Jan Kiszka
Quoted 35 lines · Jan Kiszka
Quoted 32 lines · Wolfgang Grandegger
Quoted 11 lines

drivers/can/rtcan_raw.c line 944

Quoted 9 lines
drivers/can/rtcan_raw.c +5 -0
@@ -894,6 +937,12 @@
     /* We got access */
 
 
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+    /* Push message onto stack for loopback when TX done */
+    if (sock->tx_loopback)
+    rtcan_tx_push(dev, sock, frame);
+#endif /* CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK */

Hmm, I would prefer to define something like

Quoted 6 lines

if (rtcan_tx_loopback_enabled(sock))

    rtcan_tx_push(dev, sock, frame);

with rtcan_tx_loopback_enabled resolving to 0 in the configured-out case. Thus some #ifdefs could be moved from the code to central places in header files.

OK, here I can avoid the #ifdef's ...

Quoted 5 lines
Quoted 5 lines

Index: ksrc/drivers/can/mscan/rtcan_mscan.c
===================================================================

drivers/can/mscan/rtcan_mscan.c (working copy) renamed from drivers/can/mscan/rtcan_mscan.c (revision 1834) +0 -0
@@ -251,6 +251,21 @@
Quoted 8 lines

drivers/can/mscan/rtcan_mscan.c (working copy) line 256

Quoted 6 lines
drivers/can/mscan/rtcan_mscan.c (working copy) renamed from drivers/can/mscan/rtcan_mscan.c (revision 1834) +3 -0
     regs->cantier = 0;
     /* Wake up a sender */
     rtdm_sem_up(&dev->tx_sem);
+
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+    if (dev->tx_socket) {

Same here. A macro like rtcan_tx_loopback_pending(dev) would avoid the #ifdef.

Quoted 1 line

...but not here. Or does the optimizer remove the if block?

Quoted 8 lines

AFAIK, all relevant gcc versions kill blocks like

if (0) {
	...
}

from the binary. But one problem might appear: the block content must be compilable in any case...

I'm going to checks this.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

I think the timestamp should rather be passed to rtcan_tc_loopback and set there. Or, if passing that value increases the code size needlessly, rtcan_tx_loopback should be defined like

static inline void
rtcan_tx_loopback(struct rtcan_device *dev, nanosecs_abs_t timestamp)

{
    <set timestamp>
    __rtcan_tx_loopback(dev);
}

where __rtcan_tx_loopback is the uninlined code. That makes the driver code leaner. Locking will hopefully change anyway, so nothing to do on this part for now.

Quoted 8 lines
Quoted 4 lines

Or do it directly in rtcan_tx_loopback() and rtcan_recv(). Would it be OK to read the time in these functions as well or should it be done, as is, at the beginning of the ISR? I have already planned some similar rewrite when Xenomai 2.3. is out, hopefully together with the new locking.

A few cycles offset of the timestamp doesn't matter here. We should just avoid reading it more than once as this can be a costly operation on some systems.

Hm, what does "costly operation" mean? Does it make sense then to read the time only when really necessary e.g. not for pure TX done interrupts and if nobody request them?

Wolfgang.

Quoted 2 lines

Jan

#9 lore raw
In reply to Wolfgang Grandegger
Quoted 11 lines · Wolfgang Grandegger

Hi Jan,

Quoted 1 line · Jan Kiszka

Hi Wolfgang,

[...deletions...]

Quoted 2 lines

Last remark: For the sake of completeness, tx-loopback should also be implemented in the virtual CAN driver.

The virtual CAN driver already works like TX loopback. It routes TX messages to local foreign sockets (but without sending them to any bus).

There is a minor difference: When application A is attached to a socket on virtual CAN device 1, B to device 2, and C to 1 again, a message sent by A will only be received by B. TX loopback as you are introducing it should make C receive it as well - in order to have a fully compatible test environment.

Jan

#6 lore raw
In reply to Wolfgang Grandegger
Quoted 68 lines · Wolfgang Grandegger
Quoted 35 lines · Jan Kiszka
Quoted 32 lines · Wolfgang Grandegger
Quoted 11 lines

drivers/can/rtcan_raw.c line 944

Quoted 9 lines
drivers/can/rtcan_raw.c +5 -0
@@ -894,6 +937,12 @@
     /* We got access */
 
 
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+    /* Push message onto stack for loopback when TX done */
+    if (sock->tx_loopback)
+    rtcan_tx_push(dev, sock, frame);
+#endif /* CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK */

Hmm, I would prefer to define something like

Quoted 6 lines

if (rtcan_tx_loopback_enabled(sock))

    rtcan_tx_push(dev, sock, frame);

with rtcan_tx_loopback_enabled resolving to 0 in the configured-out case. Thus some #ifdefs could be moved from the code to central places in header files.

OK, here I can avoid the #ifdef's ...

Quoted 5 lines
Quoted 5 lines

Index: ksrc/drivers/can/mscan/rtcan_mscan.c
===================================================================

drivers/can/mscan/rtcan_mscan.c (working copy) renamed from drivers/can/mscan/rtcan_mscan.c (revision 1834) +0 -0
@@ -251,6 +251,21 @@
Quoted 8 lines

drivers/can/mscan/rtcan_mscan.c (working copy) line 256

Quoted 6 lines
drivers/can/mscan/rtcan_mscan.c (working copy) renamed from drivers/can/mscan/rtcan_mscan.c (revision 1834) +3 -0
     regs->cantier = 0;
     /* Wake up a sender */
     rtdm_sem_up(&dev->tx_sem);
+
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+    if (dev->tx_socket) {

Same here. A macro like rtcan_tx_loopback_pending(dev) would avoid the #ifdef.

Quoted 1 line

...but not here. Or does the optimizer remove the if block?

Quoted 8 lines

AFAIK, all relevant gcc versions kill blocks like

if (0) {
    ...
}

from the binary. But one problem might appear: the block content must be compilable in any case...

I'm going to checks this.

Quoted 21 lines
Quoted 20 lines
Quoted 7 lines

I think the timestamp should rather be passed to rtcan_tc_loopback and set there. Or, if passing that value increases the code size needlessly,
rtcan_tx_loopback should be defined like

static inline void
rtcan_tx_loopback(struct rtcan_device *dev, nanosecs_abs_t timestamp)

Quoted 8 lines
{
    <set timestamp>
    __rtcan_tx_loopback(dev);
}

where __rtcan_tx_loopback is the uninlined code. That makes the driver code leaner. Locking will hopefully change anyway, so nothing to do on this part for now.

Or do it directly in rtcan_tx_loopback() and rtcan_recv(). Would it be OK to read the time in these functions as well or should it be done, as is, at the beginning of the ISR? I have already planned some similar rewrite when Xenomai 2.3. is out, hopefully together with the new locking.

Quoted 7 lines
Quoted 3 lines

A few cycles offset of the timestamp doesn't matter here. We should just avoid reading it more than once as this can be a costly operation on some systems.

Hm, what does "costly operation" mean? Does it make sense then to read the time only when really necessary e.g. not for pure TX done interrupts and if nobody request them?

I'm thinking about the TSC emulation on old or lowest-end x86 systems. Also, converting the ticks into nanoseconds may take some cycles on slow 32 bit systems. So it might be a good idea to check - where possible - if timestamps are needed.

Jan

#10 lore raw
In reply to Jan Kiszka
Quoted 9 lines · Jan Kiszka
Quoted 8 lines · Wolfgang Grandegger

Hi Jan,

Quoted 1 line · Jan Kiszka

Hi Wolfgang,

[...deletions...]

Quoted 2 lines

Last remark: For the sake of completeness, tx-loopback should also be implemented in the virtual CAN driver.

Quoted 8 lines
Quoted 2 lines

The virtual CAN driver already works like TX loopback. It routes TX messages to local foreign sockets (but without sending them to any bus).

There is a minor difference: When application A is attached to a socket on virtual CAN device 1, B to device 2, and C to 1 again, a message sent by A will only be received by B. TX loopback as you are introducing it should make C receive it as well - in order to have a fully compatible test environment.

Ah, I missed the case (rx_dev == tx_dev). This should work then:

	/* Deliver to all other devices on the virtual bus */
	for (i = 0; i < devices; i++) {
		rx_dev = rtcan_virt_devs[i];
		if (rx_dev->state == CAN_STATE_ACTIVE) {
			if (tx_dev != rx_dev) {
				rx_frame->can_ifindex = rx_dev->ifindex;
				rtcan_rcv(rx_dev, &skb);
			} else if (rtcan_tx_loopback_pending(tx_dev))
				rtcan_tx_loopback(tx_dev);
		}
	}

Thanks.

Wolfgang.

#7 lore raw
In reply to Jan Kiszka
Quoted 69 lines · Jan Kiszka
Quoted 68 lines · Wolfgang Grandegger
Quoted 34 lines · Jan Kiszka
Quoted 32 lines · Wolfgang Grandegger
Quoted 11 lines

drivers/can/rtcan_raw.c line 944

Quoted 9 lines
drivers/can/rtcan_raw.c +5 -0
@@ -894,6 +937,12 @@
     /* We got access */
 
 
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+    /* Push message onto stack for loopback when TX done */
+    if (sock->tx_loopback)
+    rtcan_tx_push(dev, sock, frame);
+#endif /* CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK */

Hmm, I would prefer to define something like

Quoted 6 lines

if (rtcan_tx_loopback_enabled(sock))

    rtcan_tx_push(dev, sock, frame);

with rtcan_tx_loopback_enabled resolving to 0 in the configured-out case. Thus some #ifdefs could be moved from the code to central places in header files.

OK, here I can avoid the #ifdef's ...

Quoted 5 lines
Quoted 5 lines

Index: ksrc/drivers/can/mscan/rtcan_mscan.c
===================================================================

drivers/can/mscan/rtcan_mscan.c (working copy) renamed from drivers/can/mscan/rtcan_mscan.c (revision 1834) +0 -0
@@ -251,6 +251,21 @@
Quoted 8 lines

drivers/can/mscan/rtcan_mscan.c (working copy) line 256

Quoted 6 lines
drivers/can/mscan/rtcan_mscan.c (working copy) renamed from drivers/can/mscan/rtcan_mscan.c (revision 1834) +3 -0
     regs->cantier = 0;
     /* Wake up a sender */
     rtdm_sem_up(&dev->tx_sem);
+
+#ifdef CONFIG_XENO_DRIVERS_RTCAN_TX_LOOPBACK
+    if (dev->tx_socket) {

Same here. A macro like rtcan_tx_loopback_pending(dev) would avoid the #ifdef.

Quoted 1 line

...but not here. Or does the optimizer remove the if block?

Quoted 8 lines

AFAIK, all relevant gcc versions kill blocks like

if (0) {
    ...
}

from the binary. But one problem might appear: the block content must be compilable in any case...

I'm going to checks this.

Quoted 23 lines
Quoted 12 lines
Quoted 12 lines

I think the timestamp should rather be passed to rtcan_tc_loopback and set there. Or, if passing that value increases the code size needlessly,
rtcan_tx_loopback should be defined like

static inline void
rtcan_tx_loopback(struct rtcan_device *dev, nanosecs_abs_t timestamp)

{
    <set timestamp>
    __rtcan_tx_loopback(dev);
}
Quoted 8 lines
Quoted 3 lines

where __rtcan_tx_loopback is the uninlined code. That makes the driver code leaner. Locking will hopefully change anyway, so nothing to do on this part for now.

Or do it directly in rtcan_tx_loopback() and rtcan_recv(). Would it be OK to read the time in these functions as well or should it be done, as is, at the beginning of the ISR? I have already planned some similar rewrite when Xenomai 2.3. is out, hopefully together with the new locking.

A few cycles offset of the timestamp doesn't matter here. We should just avoid reading it more than once as this can be a costly operation on some systems.

Quoted 8 lines
Quoted 3 lines

Hm, what does "costly operation" mean? Does it make sense then to read the time only when really necessary e.g. not for pure TX done interrupts and if nobody request them?

I'm thinking about the TSC emulation on old or lowest-end x86 systems. Also, converting the ticks into nanoseconds may take some cycles on slow 32 bit systems. So it might be a good idea to check - where possible - if timestamps are needed.

I have now moved the reading and copying of the timestamp into the common functions rtcan_recv() and rtcan_tx_loopback() as it is unlikely that more than one interrupt source is handled by the ISR. A further improvement would be to maintain a timestamp request count in struct rtcan_device (it's on my TODO list).

Wolfgang.

Quoted 3 lines

Jan