2018-01-27 17:59:27 +01:00
|
|
|
/*
|
|
|
|
* CAN c support to connect to the Linux host SocketCAN interfaces
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013-2014 Jin Yang
|
|
|
|
* Copyright (c) 2014-2018 Pavel Pisa
|
|
|
|
*
|
|
|
|
* Initial development supported by Google GSoC 2013 from RTEMS project slot
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2019-05-23 16:35:07 +02:00
|
|
|
|
2018-01-27 17:59:27 +01:00
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qemu/log.h"
|
Include qemu/main-loop.h less
In my "build everything" tree, changing qemu/main-loop.h triggers a
recompile of some 5600 out of 6600 objects (not counting tests and
objects that don't depend on qemu/osdep.h). It includes block/aio.h,
which in turn includes qemu/event_notifier.h, qemu/notify.h,
qemu/processor.h, qemu/qsp.h, qemu/queue.h, qemu/thread-posix.h,
qemu/thread.h, qemu/timer.h, and a few more.
Include qemu/main-loop.h only where it's needed. Touching it now
recompiles only some 1700 objects. For block/aio.h and
qemu/event_notifier.h, these numbers drop from 5600 to 2800. For the
others, they shrink only slightly.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20190812052359.30071-21-armbru@redhat.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com>
2019-08-12 07:23:50 +02:00
|
|
|
#include "qemu/main-loop.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2018-01-27 17:59:27 +01:00
|
|
|
#include "qapi/error.h"
|
|
|
|
#include "chardev/char.h"
|
|
|
|
#include "qemu/sockets.h"
|
|
|
|
#include "qemu/error-report.h"
|
|
|
|
#include "net/can_emu.h"
|
|
|
|
#include "net/can_host.h"
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <linux/can.h>
|
|
|
|
#include <linux/can/raw.h>
|
2020-09-03 22:43:22 +02:00
|
|
|
#include "qom/object.h"
|
2018-01-27 17:59:27 +01:00
|
|
|
|
|
|
|
#ifndef DEBUG_CAN
|
|
|
|
#define DEBUG_CAN 0
|
|
|
|
#endif /*DEBUG_CAN*/
|
|
|
|
|
|
|
|
#define TYPE_CAN_HOST_SOCKETCAN "can-host-socketcan"
|
2020-09-16 20:25:19 +02:00
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(CanHostSocketCAN, CAN_HOST_SOCKETCAN)
|
2018-01-27 17:59:27 +01:00
|
|
|
|
|
|
|
#define CAN_READ_BUF_LEN 5
|
2020-09-03 22:43:22 +02:00
|
|
|
struct CanHostSocketCAN {
|
2018-01-27 17:59:27 +01:00
|
|
|
CanHostState parent;
|
|
|
|
char *ifname;
|
|
|
|
|
|
|
|
qemu_can_filter *rfilter;
|
|
|
|
int rfilter_num;
|
|
|
|
can_err_mask_t err_mask;
|
|
|
|
|
|
|
|
qemu_can_frame buf[CAN_READ_BUF_LEN];
|
|
|
|
int bufcnt;
|
|
|
|
int bufptr;
|
|
|
|
|
|
|
|
int fd;
|
2020-09-03 22:43:22 +02:00
|
|
|
};
|
2018-01-27 17:59:27 +01:00
|
|
|
|
|
|
|
/* Check that QEMU and Linux kernel flags encoding and structure matches */
|
|
|
|
QEMU_BUILD_BUG_ON(QEMU_CAN_EFF_FLAG != CAN_EFF_FLAG);
|
|
|
|
QEMU_BUILD_BUG_ON(QEMU_CAN_RTR_FLAG != CAN_RTR_FLAG);
|
|
|
|
QEMU_BUILD_BUG_ON(QEMU_CAN_ERR_FLAG != CAN_ERR_FLAG);
|
|
|
|
QEMU_BUILD_BUG_ON(QEMU_CAN_INV_FILTER != CAN_INV_FILTER);
|
|
|
|
QEMU_BUILD_BUG_ON(offsetof(qemu_can_frame, data)
|
|
|
|
!= offsetof(struct can_frame, data));
|
|
|
|
|
|
|
|
static void can_host_socketcan_display_msg(struct qemu_can_frame *msg)
|
|
|
|
{
|
|
|
|
int i;
|
2019-11-18 22:15:26 +01:00
|
|
|
FILE *logfile = qemu_log_lock();
|
2018-01-27 17:59:27 +01:00
|
|
|
qemu_log("[cansocketcan]: %03X [%01d] %s %s",
|
|
|
|
msg->can_id & QEMU_CAN_EFF_MASK,
|
|
|
|
msg->can_dlc,
|
|
|
|
msg->can_id & QEMU_CAN_EFF_FLAG ? "EFF" : "SFF",
|
|
|
|
msg->can_id & QEMU_CAN_RTR_FLAG ? "RTR" : "DAT");
|
|
|
|
|
|
|
|
for (i = 0; i < msg->can_dlc; i++) {
|
|
|
|
qemu_log(" %02X", msg->data[i]);
|
|
|
|
}
|
|
|
|
qemu_log("\n");
|
|
|
|
qemu_log_flush();
|
2019-11-18 22:15:26 +01:00
|
|
|
qemu_log_unlock(logfile);
|
2018-01-27 17:59:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void can_host_socketcan_read(void *opaque)
|
|
|
|
{
|
|
|
|
CanHostSocketCAN *c = opaque;
|
|
|
|
CanHostState *ch = CAN_HOST(c);
|
|
|
|
|
|
|
|
/* CAN_READ_BUF_LEN for multiple messages syscall is possible for future */
|
|
|
|
c->bufcnt = read(c->fd, c->buf, sizeof(qemu_can_frame));
|
|
|
|
if (c->bufcnt < 0) {
|
|
|
|
warn_report("CAN bus host read failed (%s)", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-14 10:09:02 +02:00
|
|
|
if (!ch->bus_client.fd_mode) {
|
|
|
|
c->buf[0].flags = 0;
|
|
|
|
} else {
|
|
|
|
if (c->bufcnt > CAN_MTU) {
|
|
|
|
c->buf[0].flags |= QEMU_CAN_FRMF_TYPE_FD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 17:59:27 +01:00
|
|
|
can_bus_client_send(&ch->bus_client, c->buf, 1);
|
|
|
|
|
|
|
|
if (DEBUG_CAN) {
|
|
|
|
can_host_socketcan_display_msg(c->buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 18:56:50 +01:00
|
|
|
static bool can_host_socketcan_can_receive(CanBusClientState *client)
|
2018-01-27 17:59:27 +01:00
|
|
|
{
|
2020-03-05 18:56:50 +01:00
|
|
|
return true;
|
2018-01-27 17:59:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t can_host_socketcan_receive(CanBusClientState *client,
|
|
|
|
const qemu_can_frame *frames, size_t frames_cnt)
|
|
|
|
{
|
|
|
|
CanHostState *ch = container_of(client, CanHostState, bus_client);
|
|
|
|
CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(ch);
|
|
|
|
|
2020-09-14 10:09:02 +02:00
|
|
|
size_t len;
|
2018-01-27 17:59:27 +01:00
|
|
|
int res;
|
|
|
|
|
|
|
|
if (c->fd < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-09-14 10:09:02 +02:00
|
|
|
if (frames->flags & QEMU_CAN_FRMF_TYPE_FD) {
|
|
|
|
if (!ch->bus_client.fd_mode) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
len = CANFD_MTU;
|
|
|
|
} else {
|
|
|
|
len = CAN_MTU;
|
|
|
|
|
|
|
|
}
|
2018-01-27 17:59:27 +01:00
|
|
|
|
|
|
|
res = write(c->fd, frames, len);
|
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
warn_report("[cansocketcan]: write message to host returns zero");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res != len) {
|
|
|
|
if (res < 0) {
|
|
|
|
warn_report("[cansocketcan]: write to host failed (%s)",
|
|
|
|
strerror(errno));
|
|
|
|
} else {
|
|
|
|
warn_report("[cansocketcan]: write to host truncated");
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void can_host_socketcan_disconnect(CanHostState *ch)
|
|
|
|
{
|
|
|
|
CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(ch);
|
|
|
|
|
|
|
|
if (c->fd >= 0) {
|
|
|
|
qemu_set_fd_handler(c->fd, NULL, NULL, c);
|
|
|
|
close(c->fd);
|
|
|
|
c->fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(c->rfilter);
|
|
|
|
c->rfilter = NULL;
|
|
|
|
c->rfilter_num = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CanBusClientInfo can_host_socketcan_bus_client_info = {
|
|
|
|
.can_receive = can_host_socketcan_can_receive,
|
|
|
|
.receive = can_host_socketcan_receive,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void can_host_socketcan_connect(CanHostState *ch, Error **errp)
|
|
|
|
{
|
|
|
|
CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(ch);
|
|
|
|
int s; /* can raw socket */
|
2020-09-14 10:09:02 +02:00
|
|
|
int mtu;
|
|
|
|
int enable_canfd = 1;
|
2018-01-27 17:59:27 +01:00
|
|
|
struct sockaddr_can addr;
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
2020-10-08 22:27:12 +02:00
|
|
|
if (!c->ifname) {
|
|
|
|
error_setg(errp, "'if' property not set");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-27 17:59:27 +01:00
|
|
|
/* open socket */
|
|
|
|
s = qemu_socket(PF_CAN, SOCK_RAW, CAN_RAW);
|
|
|
|
if (s < 0) {
|
|
|
|
error_setg_errno(errp, errno, "failed to create CAN_RAW socket");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr.can_family = AF_CAN;
|
|
|
|
memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
|
|
|
|
strcpy(ifr.ifr_name, c->ifname);
|
2020-09-14 10:09:02 +02:00
|
|
|
/* check if the frame fits into the CAN netdevice */
|
2018-01-27 17:59:27 +01:00
|
|
|
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
|
|
|
|
error_setg_errno(errp, errno,
|
2020-09-14 10:09:02 +02:00
|
|
|
"SocketCAN host interface %s not available",
|
|
|
|
c->ifname);
|
2018-01-27 17:59:27 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
addr.can_ifindex = ifr.ifr_ifindex;
|
|
|
|
|
2020-09-14 10:09:02 +02:00
|
|
|
if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
|
|
|
|
error_setg_errno(errp, errno,
|
|
|
|
"SocketCAN host interface %s SIOCGIFMTU failed",
|
|
|
|
c->ifname);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
mtu = ifr.ifr_mtu;
|
|
|
|
|
|
|
|
if (mtu >= CANFD_MTU) {
|
|
|
|
/* interface is ok - try to switch the socket into CAN FD mode */
|
|
|
|
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES,
|
|
|
|
&enable_canfd, sizeof(enable_canfd))) {
|
|
|
|
warn_report("SocketCAN host interface %s enabling CAN FD failed",
|
|
|
|
c->ifname);
|
|
|
|
} else {
|
|
|
|
c->parent.bus_client.fd_mode = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 17:59:27 +01:00
|
|
|
c->err_mask = 0xffffffff; /* Receive error frame. */
|
|
|
|
setsockopt(s, SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
|
|
|
|
&c->err_mask, sizeof(c->err_mask));
|
|
|
|
|
|
|
|
c->rfilter_num = 1;
|
|
|
|
c->rfilter = g_new(struct qemu_can_filter, c->rfilter_num);
|
|
|
|
|
|
|
|
/* Receive all data frame. If |= CAN_INV_FILTER no data. */
|
|
|
|
c->rfilter[0].can_id = 0;
|
|
|
|
c->rfilter[0].can_mask = 0;
|
|
|
|
c->rfilter[0].can_mask &= ~CAN_ERR_FLAG;
|
|
|
|
|
|
|
|
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, c->rfilter,
|
|
|
|
c->rfilter_num * sizeof(struct qemu_can_filter));
|
|
|
|
|
|
|
|
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
|
|
|
error_setg_errno(errp, errno, "failed to bind to host interface %s",
|
|
|
|
c->ifname);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->fd = s;
|
|
|
|
ch->bus_client.info = &can_host_socketcan_bus_client_info;
|
|
|
|
qemu_set_fd_handler(c->fd, can_host_socketcan_read, NULL, c);
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
close(s);
|
|
|
|
g_free(c->rfilter);
|
|
|
|
c->rfilter = NULL;
|
|
|
|
c->rfilter_num = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *can_host_socketcan_get_if(Object *obj, Error **errp)
|
|
|
|
{
|
|
|
|
CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(obj);
|
|
|
|
|
|
|
|
return g_strdup(c->ifname);
|
|
|
|
}
|
|
|
|
|
2020-09-14 10:09:02 +02:00
|
|
|
static void can_host_socketcan_set_if(Object *obj, const char *value,
|
|
|
|
Error **errp)
|
2018-01-27 17:59:27 +01:00
|
|
|
{
|
|
|
|
CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(obj);
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
|
|
|
if (strlen(value) >= sizeof(ifr.ifr_name)) {
|
|
|
|
error_setg(errp, "CAN interface name longer than %zd characters",
|
|
|
|
sizeof(ifr.ifr_name) - 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->fd != -1) {
|
|
|
|
error_setg(errp, "CAN interface already connected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(c->ifname);
|
|
|
|
c->ifname = g_strdup(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void can_host_socketcan_instance_init(Object *obj)
|
|
|
|
{
|
|
|
|
CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(obj);
|
|
|
|
|
|
|
|
c->fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void can_host_socketcan_class_init(ObjectClass *klass,
|
|
|
|
void *class_data G_GNUC_UNUSED)
|
|
|
|
{
|
|
|
|
CanHostClass *chc = CAN_HOST_CLASS(klass);
|
|
|
|
|
|
|
|
object_class_property_add_str(klass, "if",
|
|
|
|
can_host_socketcan_get_if,
|
qom: Drop parameter @errp of object_property_add() & friends
The only way object_property_add() can fail is when a property with
the same name already exists. Since our property names are all
hardcoded, failure is a programming error, and the appropriate way to
handle it is passing &error_abort.
Same for its variants, except for object_property_add_child(), which
additionally fails when the child already has a parent. Parentage is
also under program control, so this is a programming error, too.
We have a bit over 500 callers. Almost half of them pass
&error_abort, slightly fewer ignore errors, one test case handles
errors, and the remaining few callers pass them to their own callers.
The previous few commits demonstrated once again that ignoring
programming errors is a bad idea.
Of the few ones that pass on errors, several violate the Error API.
The Error ** argument must be NULL, &error_abort, &error_fatal, or a
pointer to a variable containing NULL. Passing an argument of the
latter kind twice without clearing it in between is wrong: if the
first call sets an error, it no longer points to NULL for the second
call. ich9_pm_add_properties(), sparc32_ledma_realize(),
sparc32_dma_realize(), xilinx_axidma_realize(), xilinx_enet_realize()
are wrong that way.
When the one appropriate choice of argument is &error_abort, letting
users pick the argument is a bad idea.
Drop parameter @errp and assert the preconditions instead.
There's one exception to "duplicate property name is a programming
error": the way object_property_add() implements the magic (and
undocumented) "automatic arrayification". Don't drop @errp there.
Instead, rename object_property_add() to object_property_try_add(),
and add the obvious wrapper object_property_add().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-15-armbru@redhat.com>
[Two semantic rebase conflicts resolved]
2020-05-05 17:29:22 +02:00
|
|
|
can_host_socketcan_set_if);
|
2018-01-27 17:59:27 +01:00
|
|
|
chc->connect = can_host_socketcan_connect;
|
|
|
|
chc->disconnect = can_host_socketcan_disconnect;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo can_host_socketcan_info = {
|
|
|
|
.parent = TYPE_CAN_HOST,
|
|
|
|
.name = TYPE_CAN_HOST_SOCKETCAN,
|
|
|
|
.instance_size = sizeof(CanHostSocketCAN),
|
|
|
|
.instance_init = can_host_socketcan_instance_init,
|
|
|
|
.class_init = can_host_socketcan_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void can_host_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&can_host_socketcan_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(can_host_register_types);
|