{"record":{"id":"8bce9ca1dec87425","repo":"commaai/openpilot","slug":"failed-to-get-interface-index-for-s","errorCode":null,"errorMessage":"Failed to get interface index for %s\n","messagePattern":"Failed to get interface index for (.+?)\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"openpilot/tools/cabana/streams/socketcanstream.cc","lineNumber":59,"sourceCode":"  ::close(fd);\n  return true;\n}\n\nbool SocketCanStream::connect() {\n  sock_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);\n  if (sock_fd < 0) {\n    fprintf(stderr, \"Failed to create CAN socket\\n\");\n    return false;\n  }\n\n  // Enable CAN-FD\n  int fd_enable = 1;\n  setsockopt(sock_fd, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &fd_enable, sizeof(fd_enable));\n\n  struct ifreq ifr = {};\n  strncpy(ifr.ifr_name, config.device.c_str(), IFNAMSIZ - 1);\n  if (ioctl(sock_fd, SIOCGIFINDEX, &ifr) < 0) {\n    fprintf(stderr, \"Failed to get interface index for %s\\n\", config.device.c_str());\n    ::close(sock_fd);\n    sock_fd = -1;\n    return false;\n  }\n\n  struct sockaddr_can addr = {};\n  addr.can_family = AF_CAN;\n  addr.can_ifindex = ifr.ifr_ifindex;\n  if (bind(sock_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {\n    fprintf(stderr, \"Failed to bind CAN socket\\n\");\n    ::close(sock_fd);\n    sock_fd = -1;\n    return false;\n  }\n\n  // Set read timeout so the thread can check for interruption\n  struct timeval tv = {.tv_sec = 0, .tv_usec = 100000};  // 100ms\n  setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/commaai/openpilot/blob/516ec1e68203439a73f340f1d0b3b91eabc626ee/openpilot/tools/cabana/streams/socketcanstream.cc#L41-L77","documentation":"Emitted by SocketCanStream::connect() when ioctl(sock_fd, SIOCGIFINDEX, &ifr) fails after a successful socket() — the kernel could not resolve config.device (the interface name typed in the stream settings) to an interface index. The socket is closed, sock_fd reset to -1, and connect() returns false.","triggerScenarios":"Starting a SocketCAN stream with a `config.device` string that does not match any network interface: typo ('can0' vs 'can1', trailing space), the CAN interface not brought up yet (`ip link set can0 up` missing), USB CAN adapter not plugged/enumerated so no canX exists, or a name longer than IFNAMSIZ-1 truncated by the strncpy. Note strncpy copies IFNAMSIZ-1 bytes, so names at the limit silently truncate and resolve to nothing.","commonSituations":"Typing the wrong interface name in cabana's SocketCAN device field; slcan/socketcand adapter not yet attached; interface named differently on this host (can1, vcan0 for virtual); whitespace copied into the device field.","solutions":["Run `ip link show` (or `ifconfig -a`) and copy the exact interface name (e.g. can0) into the device field.","Bring the interface up: `sudo ip link set can0 up type can bitrate 500000` (or your bus's rate); if the adapter is missing, plug it in and re-check.","Trim whitespace and stay under 15 chars (IFNAMSIZ-1) — longer names are silently truncated by the strncpy before the ioctl.","For bench testing without hardware, create a virtual bus: `sudo ip link add dev vcan0 type vcan && sudo ip link set vcan0 up` and use vcan0."],"exampleFix":"# before\n# stderr: Failed to get interface index for can1   (host only has can0)\nip link   # shows: can0\n\n# after\n# set cabana SocketCAN device to: can0\nsudo ip link set can0 up type can bitrate 500000\ncabana   # ioctl(SIOCGIFINDEX) resolves, bind proceeds","handlingStrategy":"validation","validationCode":"// Verify the interface exists (and is CAN) before starting the stream:\n#include <net/if.h>\n#include <sys/ioctl.h>\nbool canInterfaceExists(const std::string &ifname) {\n  if (ifname.empty() || ifname.size() >= IFNAMSIZ) return false;  // strncpy truncates at IFNAMSIZ-1\n  int fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);\n  if (fd < 0) return false;\n  ifreq ifr = {};\n  strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);\n  bool ok = ioctl(fd, SIOCGIFINDEX, &ifr) == 0;\n  ::close(fd);\n  return ok;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Populate the device field from `ip link` output rather than typing from memory.","Bring the interface up (`ip link set canX up ...`) before connecting.","Reject interface names longer than 15 chars — the code silently truncates before the ioctl.","Create vcan0 for hardware-free testing."],"tags":["socketcan","interface","ioctl","configuration","linux"],"backgroundTag":null,"analyzedSha":"516ec1e68203439a73f340f1d0b3b91eabc626ee","analyzedAt":"2026-08-15T00:17:37.461Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}