Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce netlink/rtnl CAN support #1109

Merged
merged 1 commit into from
Jul 9, 2023
Merged

Conversation

johnernberg
Copy link
Contributor

Adds handling of all the attributes related to CAN.

I was not able to figure out how create an interface similar to the iproute package, so the interface is a bit more low level in that instead of adding bitrate with "bitrate=" one has to package a can_bittiming dict with {"bitrate": }.

The attribute CAN_CLOCK is a struct of one member in the ABI, but it doesn't seem that fields support one-member structs.

A couple of the attributes haven't been tested because I do not have any dongles that support them. E.g. the TDC options.

Fixes #1023

Adds handling of all the attributes related to CAN.
@svinota svinota merged commit c7da937 into svinota:master Jul 9, 2023
14 checks passed
@svinota
Copy link
Owner

svinota commented Jul 9, 2023

Thanks a lot!

@yegorich
Copy link
Contributor

yegorich commented Nov 15, 2023

@johnernberg could you please provide an example with setting a CAN bitrate? I've found the following code but, aside from bringing can0 interface up and down, nothing changes:

from time import sleep
from pyroute2 import IPRoute

with IPRoute() as ip_route:
    can0 = ip_route.link_lookup(ifname='can0')[0]
    link = ip_route.link('get', index=can0)
    if 'state' in link[0] and link[0]['state'] == 'up':
        ip_route.link('set', index=can0, state='down')
        sleep(1)

    ip_route.link('set', index=can0, type='can', bitrate=100000)
    ip_route.link('set', index=can0, state='up')

@yegorich
Copy link
Contributor

Some info about my system. It's Debian Bookworm (Linux 6.1.0). CAN hardware is an SLCAN device.

The following command is working as expected:

ip link set can0 type can bitrate 100000

@johnernberg
Copy link
Contributor Author

@yegorich As explained in the commit message this is a little bit more low level than the iproute command line.

What iproute tools call type is here called 'kind' because that is what netlink uses internally. And the bitrate must be provided using the can_bittiming structure which has a bitrate field.

Try:

ip_route.link('set', index=can0, kind='can', can_bittiming={'bitrate': 100000 })

@yegorich
Copy link
Contributor

@johnernberg it works like a charm. Thank you very much. I'll try to submit a PR with such an example.

@svinota where should I place this example code? examples/iproute?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

setup CAN interfaces
3 participants