8

How can I create several virtual sockets and link them together to create a virtual bus?

I want to simulate an application in which many nodes communicate to each other via CAN.

Shabbir Hussain
  • 2,600
  • 2
  • 17
  • 25
  • Leaving a link to a very similar question: [How to create virtual CAN port on linux? (C++) ](https://stackoverflow.com/questions/21022749/how-to-create-virtual-can-port-on-linux-c) – mab0189 Mar 23 '21 at 20:18

1 Answers1

9

All you need is cangw tool from can-utils. Create two virtual interfaces:

ip link add dev vcan0 type vcan
ip link add dev vcan1 type vcan
ip link set up vcan0
ip link set up vcan1

Create routing rule, so that all packets coming to vcan0 will be sent to vcan1:

cangw -A -s vcan0 -d vcan1 -e

Listen to vcan1 in one terminal:

candump vcan1

And send packet from another terminal:

cansend vcan0 123#0011

You'll see, that candump will get this CAN packet:

vcan1  123   [2]  00 11
yegorich
  • 4,653
  • 3
  • 31
  • 37
  • 1
    I keep getting " netlink error -95 (Operation not supported)" any ideas? – Shabbir Hussain Nov 12 '15 at 19:48
  • Make sure CONFIG_CAN_GW is activated in your kernel. You can compile it either as part of the kernel or as a module. – yegorich Nov 13 '15 at 10:29
  • 1
    I had to load it with "modprobe can-gw" – Shabbir Hussain Nov 15 '15 at 04:34
  • 1
    Hi, that blog link seems to take me to an irrelevant page. Can you fix it? Also, I noticed that your routing rule only works in one direction, which isn't really how a bus should work. So is it correct that I should also make another routine roule in the opposite direction? – 9a3eedi Apr 03 '17 at 11:57
  • @9a3eedi I have removed the link. I haven't tried the other way around, but you can send your question to the linux-can mailing list and get an answer from cangw developers themselves. – yegorich Apr 04 '17 at 10:38
  • 1
    I tried the other way around and it seems to work the way it should. But that's just a quick assessment, I don't know if there are any catches. It should be OK though – 9a3eedi Apr 12 '17 at 06:49