aboutsummaryrefslogtreecommitdiffstats
path: root/fs/logfs/readwrite.c
stat options
Period:
Authors:

Commits per author per week (path 'fs/logfs/readwrite.c')

AuthorW35 2025W36 2025W37 2025W38 2025Total
Total00000
='n178' href='#n178'>178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
============================================================================

can.txt

Readme file for the Controller Area Network Protocol Family (aka Socket CAN)

This file contains

  1 Overview / What is Socket CAN

  2 Motivation / Why using the socket API

  3 Socket CAN concept
    3.1 receive lists
    3.2 local loopback of sent frames
    3.3 network security issues (capabilities)
    3.4 network problem notifications

  4 How to use Socket CAN
    4.1 RAW protocol sockets with can_filters (SOCK_RAW)
      4.1.1 RAW socket option CAN_RAW_FILTER
      4.1.2 RAW socket option CAN_RAW_ERR_FILTER
      4.1.3 RAW socket option CAN_RAW_LOOPBACK
      4.1.4 RAW socket option CAN_RAW_RECV_OWN_MSGS
      4.1.5 RAW socket option CAN_RAW_FD_FRAMES
      4.1.6 RAW socket returned message flags
    4.2 Broadcast Manager protocol sockets (SOCK_DGRAM)
    4.3 connected transport protocols (SOCK_SEQPACKET)
    4.4 unconnected transport protocols (SOCK_DGRAM)

  5 Socket CAN core module
    5.1 can.ko module params
    5.2 procfs content
    5.3 writing own CAN protocol modules

  6 CAN network drivers
    6.1 general settings
    6.2 local loopback of sent frames
    6.3 CAN controller hardware filters
    6.4 The virtual CAN driver (vcan)
    6.5 The CAN network device driver interface
      6.5.1 Netlink interface to set/get devices properties
      6.5.2 Setting the CAN bit-timing
      6.5.3 Starting and stopping the CAN network device
    6.6 CAN FD (flexible data rate) driver support
    6.7 supported CAN hardware

  7 Socket CAN resources

  8 Credits

============================================================================

1. Overview / What is Socket CAN
--------------------------------

The socketcan package is an implementation of CAN protocols
(Controller Area Network) for Linux.  CAN is a networking technology
which has widespread use in automation, embedded devices, and
automotive fields.  While there have been other CAN implementations
for Linux based on character devices, Socket CAN uses the Berkeley
socket API, the Linux network stack and implements the CAN device
drivers as network interfaces.  The CAN socket API has been designed
as similar as possible to the TCP/IP protocols to allow programmers,
familiar with network programming, to easily learn how to use CAN
sockets.

2. Motivation / Why using the socket API
----------------------------------------

There have been CAN implementations for Linux before Socket CAN so the
question arises, why we have started another project.  Most existing
implementations come as a device driver for some CAN hardware, they
are based on character devices and provide comparatively little
functionality.  Usually, there is only a hardware-specific device
driver which provides a character device interface to send and
receive raw CAN frames, directly to/from the controller hardware.
Queueing of frames and higher-level transport protocols like ISO-TP
have to be implemented in user space applications.  Also, most
character-device implementations support only one single process to
open the device at a time, similar to a serial interface.  Exchanging
the CAN controller requires employment of another device driver and
often the need for adaption of large parts of the application to the
new driver's API.

Socket CAN was designed to overcome all of these limitations.  A new
protocol family has been implemented which provides a socket interface
to user space applications and which builds upon the Linux network
layer, so to use all of the provided queueing functionality.  A device
driver for CAN controller hardware registers itself with the Linux
network layer as a network device, so that CAN frames from the
controller can be passed up to the network layer and on to the CAN
protocol family module and also vice-versa.  Also, the protocol family
module provides an API for transport protocol modules to register, so
that any number of transport protocols can be loaded or unloaded
dynamically.  In fact, the can core module alone does not provide any
protocol and cannot be used without loading at least one additional
protocol module.  Multiple sockets can be opened at the same time,
on different or the same protocol module and they can listen/send
frames on different or the same CAN IDs.  Several sockets listening on
the same interface for frames with the same CAN ID are all passed the
same received matching CAN frames.  An application wishing to
communicate using a specific transport protocol, e.g. ISO-TP, just
selects that protocol when opening the socket, and then can read and
write application data byte streams, without having to deal with
CAN-IDs, frames, etc.

Similar functionality visible from user-space could be provided by a
character device, too, but this would lead to a technically inelegant
solution for a couple of reasons:

* Intricate usage.  Instead of passing a protocol argument to
  socket(2) and using bind(2) to select a CAN interface and CAN ID, an
  application would have to do all these operations using ioctl(2)s.

* Code duplication.  A character device cannot make use of the Linux
  network queueing code, so all that code would have to be duplicated
  for CAN networking.

* Abstraction.  In most existing character-device implementations, the
  hardware-specific device driver for a CAN controller directly
  provides the character device for the application to work with.
  This is at least very unusual in Unix systems for both, char and
  block devices.  For example you don't have a character device for a
  certain UART of a serial interface, a certain sound chip in your
  computer, a SCSI or IDE controller providing access to your hard
  disk or tape streamer device.  Instead, you have abstraction layers
  which provide a unified character or block device interface to the
  application on the one hand, and a interface for hardware-specific
  device drivers on the other hand.  These abstractions are provided
  by subsystems like the tty layer, the audio subsystem or the SCSI
  and IDE subsystems for the devices mentioned above.

  The easiest way to implement a CAN device driver is as a character
  device without such a (complete) abstraction layer, as is done by most
  existing drivers.  The right way, however, would be to add such a
  layer with all the functionality like registering for certain CAN
  IDs, supporting several open file descriptors and (de)multiplexing
  CAN frames between them, (sophisticated) queueing of CAN frames, and
  providing an API for device drivers to register with.  However, then
  it would be no more difficult, or may be even easier, to use the
  networking framework provided by the Linux kernel, and this is what
  Socket CAN does.

  The use of the networking framework of the Linux kernel is just the
  natural and most appropriate way to implement CAN for Linux.

3. Socket CAN concept
---------------------

  As described in chapter 2 it is the main goal of Socket CAN to
  provide a socket interface to user space applications which builds
  upon the Linux network layer. In contrast to the commonly known
  TCP/IP and ethernet networking, the CAN bus is a broadcast-only(!)
  medium that has no MAC-layer addressing like ethernet. The CAN-identifier
  (can_id) is used for arbitration on the CAN-bus. Therefore the CAN-IDs
  have to be chosen uniquely on the bus. When designing a CAN-ECU
  network the CAN-IDs are mapped to be sent by a specific ECU.
  For this reason a CAN-ID can be treated best as a kind of source address.

  3.1 receive lists

  The network transparent access of multiple applications leads to the
  problem that different applications may be interested in the same
  CAN-IDs from the same CAN network interface. The Socket CAN core
  module - which implements the protocol family CAN - provides several
  high efficient receive lists for this reason. If e.g. a user space
  application opens a CAN RAW socket, the raw protocol module itself
  requests the (range of) CAN-IDs from the Socket CAN core that are
  requested by the user. The subscription and unsubscription of
  CAN-IDs can be done for specific CAN interfaces or for all(!) known
  CAN interfaces with the can_rx_(un)register() functions provided to
  CAN protocol modules by the SocketCAN core (see chapter 5).
  To optimize the CPU usage at runtime the receive lists are split up
  into several specific lists per device that match the requested
  filter complexity for a given use-case.

  3.2 local loopback of sent frames

  As known from other networking concepts the data exchanging
  applications may run on the same or different nodes without any
  change (except for the according addressing information):

         ___   ___   ___                   _______   ___
        | _ | | _ | | _ |                 | _   _ | | _ |
        ||A|| ||B|| ||C||                 ||A| |B|| ||C||
        |___| |___| |___|                 |_______| |___|
          |     |     |                       |       |
        -----------------(1)- CAN bus -(2)---------------

  To ensure that application A receives the same information in the
  example (2) as it would receive in example (1) there is need for
  some kind of local loopback of the sent CAN frames on the appropriate
  node.

  The Linux network devices (by default) just can handle the
  transmission and reception of media dependent frames. Due to the
  arbitration on the CAN bus the transmission of a low prio CAN-ID
  may be delayed by the reception of a high prio CAN frame. To
  reflect the correct* traffic on the node the loopback of the sent
  data has to be performed right after a successful transmission. If
  the CAN network interface is not capable of performing the loopback for
  some reason the SocketCAN core can do this task as a fallback solution.
  See chapter 6.2 for details (recommended).

  The loopback functionality is enabled by default to reflect standard
  networking behaviour for CAN applications. Due to some requests from
  the RT-SocketCAN group the loopback optionally may be disabled for each
  separate socket. See sockopts from the CAN RAW sockets in chapter 4.1.

  * = you really like to have this when you're running analyser tools
      like 'candump' or 'cansniffer' on the (same) node.

  3.3 network security issues (capabilities)

  The Controller Area Network is a local field bus transmitting only
  broadcast messages without any routing and security concepts.
  In the majority of cases the user application has to deal with
  raw CAN frames. Therefore it might be reasonable NOT to restrict
  the CAN access only to the user root, as known from other networks.
  Since the currently implemented CAN_RAW and CAN_BCM sockets can only
  send and receive frames to/from CAN interfaces it does not affect
  security of others networks to allow all users to access the CAN.
  To enable non-root users to access CAN_RAW and CAN_BCM protocol
  sockets the Kconfig options CAN_RAW_USER and/or CAN_BCM_USER may be
  selected at kernel compile time.

  3.4 network problem notifications

  The use of the CAN bus may lead to several problems on the physical
  and media access control layer. Detecting and logging of these lower
  layer problems is a vital requirement for CAN users to identify
  hardware issues on the physical transceiver layer as well as
  arbitration problems and error frames caused by the different
  ECUs. The occurrence of detected errors are important for diagnosis
  and have to be logged together with the exact timestamp. For this
  reason the CAN interface driver can generate so called Error Message
  Frames that can optionally be passed to the user application in the
  same way as other CAN frames. Whenever an error on the physical layer
  or the MAC layer is detected (e.g. by the CAN controller) the driver
  creates an appropriate error message frame. Error messages frames can
  be requested by the user application using the common CAN filter
  mechanisms. Inside this filter definition the (interested) type of
  errors may be selected. The reception of error messages is disabled
  by default. The format of the CAN error message frame is briefly
  described in the Linux header file "include/linux/can/error.h".

4. How to use Socket CAN
------------------------

  Like TCP/IP, you first need to open a socket for communicating over a
  CAN network. Since Socket CAN implements a new protocol family, you
  need to pass PF_CAN as the first argument to the socket(2) system
  call. Currently, there are two CAN protocols to choose from, the raw
  socket protocol and the broadcast manager (BCM). So to open a socket,
  you would write

    s = socket(PF_CAN, SOCK_RAW, CAN_RAW);

  and

    s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM);

  respectively.  After the successful creation of the socket, you would
  normally use the bind(2) system call to bind the socket to a CAN
  interface (which is different from TCP/IP due to different addressing
  - see chapter 3). After binding (CAN_RAW) or connecting (CAN_BCM)
  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 CAN specific socket options
  described below.

  The basic CAN frame structure and the sockaddr structure are defined
  in include/linux/can.h:

    struct can_frame {
            canid_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
            __u8    can_dlc; /* frame payload length in byte (0 .. 8) */
            __u8    data[8] __attribute__((aligned(8)));
    };

  The alignment of the (linear) payload data[] to a 64bit boundary
  allows the user to define own structs and unions to easily access the
  CAN payload. There is no given byteorder on the CAN bus by
  default. A read(2) system call on a CAN_RAW socket transfers a
  struct can_frame to the user space.

  The sockaddr_can structure has an interface index like the
  PF_PACKET socket, that also binds to a specific interface:

    struct sockaddr_can {
            sa_family_t can_family;
            int         can_ifindex;
            union {
                    /* transport protocol class address info (e.g. ISOTP) */
                    struct { canid_t rx_id, tx_id; } tp;

                    /* reserved for future CAN protocols address information */
            } can_addr;
    };

  To determine the interface index an appropriate ioctl() has to
  be used (example for CAN_RAW sockets without error checking):

    int s;
    struct sockaddr_can addr;
    struct ifreq ifr;

    s = socket(PF_CAN, SOCK_RAW, CAN_RAW);

    strcpy(ifr.ifr_name, "can0" );
    ioctl(s, SIOCGIFINDEX, &ifr);

    addr.can_family = AF_CAN;
    addr.can_ifindex = ifr.ifr_ifindex;

    bind(s, (struct sockaddr *)&addr, sizeof(addr));

    (..)

  To bind a socket to all(!) CAN interfaces the interface index must
  be 0 (zero). In this case the socket receives CAN frames from every
  enabled CAN interface. To determine the originating CAN interface
  the system call recvfrom(2) may be used instead of read(2). To send
  on a socket that is bound to 'any' interface sendto(2) is needed to
  specify the outgoing interface.

  Reading CAN frames from a bound CAN_RAW socket (see above) consists
  of reading a struct can_frame:

    struct can_frame frame;

    nbytes = read(s, &frame, sizeof(struct can_frame));

    if (nbytes < 0) {
            perror("can raw socket read");
            return 1;
    }

    /* paranoid check ... */
    if (nbytes < sizeof(struct can_frame)) {
            fprintf(stderr, "read: incomplete CAN frame\n");
            return 1;
    }

    /* do something with the received CAN frame */

  Writing CAN frames can be done similarly, with the write(2) system call:

    nbytes = write(s, &frame, sizeof(struct can_frame));

  When the CAN interface is bound to 'any' existing CAN interface
  (addr.can_ifindex = 0) it is recommended to use recvfrom(2) if the
  information about the originating CAN interface is needed:

    struct sockaddr_can addr;
    struct ifreq ifr;
    socklen_t len = sizeof(addr);
    struct can_frame frame;

    nbytes = recvfrom(s, &frame, sizeof(struct can_frame),
                      0, (struct sockaddr*)&addr, &len);

    /* get interface name of the received CAN frame */
    ifr.ifr_ifindex = addr.can_ifindex;
    ioctl(s, SIOCGIFNAME, &ifr);
    printf("Received a CAN frame from interface %s", ifr.ifr_name);

  To write CAN frames on sockets bound to 'any' CAN interface the
  outgoing interface has to be defined certainly.

    strcpy(ifr.ifr_name, "can0");
    ioctl(s, SIOCGIFINDEX, &ifr);
    addr.can_ifindex = ifr.ifr_ifindex;
    addr.can_family  = AF_CAN;

    nbytes = sendto(s, &frame, sizeof(struct can_frame),
                    0, (struct sockaddr*)&addr, sizeof(addr));

  Remark about CAN FD (flexible data rate) support:

  Generally the handling of CAN FD is very similar to the formerly described
  examples. The new CAN FD capable CAN controllers support two different
  bitrates for the arbitration phase and the payload phase of the CAN FD frame
  and up to 64 bytes of payload. This extended payload length breaks all the
  kernel interfaces (ABI) which heavily rely on the CAN frame with fixed eight
  bytes of payload (struct can_frame) like the CAN_RAW socket. Therefore e.g.
  the CAN_RAW socket supports a new socket option CAN_RAW_FD_FRAMES that
  switches the socket into a mode that allows the handling of CAN FD frames
  and (legacy) CAN frames simultaneously (see section 4.1.5).

  The struct canfd_frame is defined in include/linux/can.h:

    struct canfd_frame {
            canid_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
            __u8    len;     /* frame payload length in byte (0 .. 64) */
            __u8    flags;   /* additional flags for CAN FD */
            __u8    __res0;  /* reserved / padding */
            __u8    __res1;  /* reserved / padding */
            __u8    data[64] __attribute__((aligned(8)));
    };

  The struct canfd_frame and the existing struct can_frame have the can_id,
  the payload length and the payload data at the same offset inside their
  structures. This allows to handle the different structures very similar.
  When the content of a struct can_frame is copied into a struct canfd_frame
  all structure elements can be used as-is - only the data[] becomes extended.

  When introducing the struct canfd_frame it turned out that the data length
  code (DLC) of the struct can_frame was used as a length information as the
  length and the DLC has a 1:1 mapping in the range of 0 .. 8. To preserve
  the easy handling of the length information the canfd_frame.len element
  contains a plain length value from 0 .. 64. So both canfd_frame.len and
  can_frame.can_dlc are equal and contain a length information and no DLC.
  For details about the distinction of CAN and CAN FD capable devices and
  the mapping to the bus-relevant data length code (DLC), see chapter 6.6.

  The length of the two CAN(FD) frame structures define the maximum transfer
  unit (MTU) of the CAN(FD) network interface and skbuff data length. Two
  definitions are specified for CAN specific MTUs in include/linux/can.h :

  #define CAN_MTU   (sizeof(struct can_frame))   == 16  => 'legacy' CAN frame
  #define CANFD_MTU (sizeof(struct canfd_frame)) == 72  => CAN FD frame

  4.1 RAW protocol sockets with can_filters (SOCK_RAW)

  Using CAN_RAW sockets is extensively comparable to the commonly
  known access to CAN character devices. To meet the new possibilities
  provided by the multi user SocketCAN approach, some reasonable
  defaults are set at RAW socket binding time:

  - The filters are set to exactly one filter receiving everything
  - The socket only receives valid data frames (=> no error message frames)
  - The loopback of sent CAN frames is enabled (see chapter 3.2)
  - The socket does not receive its own sent frames (in loopback mode)

  These default settings may be changed before or after binding the socket.
  To use the referenced definitions of the socket options for CAN_RAW
  sockets, include <linux/can/raw.h>.

  4.1.1 RAW socket option CAN_RAW_FILTER

  The reception of CAN frames using CAN_RAW sockets can be controlled
  by defining 0 .. n filters with the CAN_RAW_FILTER socket option.

  The CAN filter structure is defined in include/linux/can.h:

    struct can_filter {
            canid_t can_id;
            canid_t can_mask;
    };

  A filter matches, when

    <received_can_id> & mask == can_id & mask

  which is analogous to known CAN controllers hardware filter semantics.