xref: /openbmc/qemu/net/can/can_socketcan.c (revision fc59d2d8)
1 /*
2  * CAN c support to connect to the Linux host SocketCAN interfaces
3  *
4  * Copyright (c) 2013-2014 Jin Yang
5  * Copyright (c) 2014-2018 Pavel Pisa
6  *
7  * Initial development supported by Google GSoC 2013 from RTEMS project slot
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25  * THE SOFTWARE.
26  */
27 
28 #include "qemu/osdep.h"
29 #include "qemu/log.h"
30 #include "qemu/main-loop.h"
31 #include "qemu/module.h"
32 #include "qapi/error.h"
33 #include "chardev/char.h"
34 #include "qemu/sockets.h"
35 #include "qemu/error-report.h"
36 #include "net/can_emu.h"
37 #include "net/can_host.h"
38 
39 #include <sys/ioctl.h>
40 #include <net/if.h>
41 #include <linux/can.h>
42 #include <linux/can/raw.h>
43 
44 #ifndef DEBUG_CAN
45 #define DEBUG_CAN 0
46 #endif /*DEBUG_CAN*/
47 
48 #define TYPE_CAN_HOST_SOCKETCAN "can-host-socketcan"
49 #define CAN_HOST_SOCKETCAN(obj) \
50      OBJECT_CHECK(CanHostSocketCAN, (obj), TYPE_CAN_HOST_SOCKETCAN)
51 
52 #define CAN_READ_BUF_LEN  5
53 typedef struct CanHostSocketCAN {
54     CanHostState       parent;
55     char               *ifname;
56 
57     qemu_can_filter    *rfilter;
58     int                rfilter_num;
59     can_err_mask_t     err_mask;
60 
61     qemu_can_frame     buf[CAN_READ_BUF_LEN];
62     int                bufcnt;
63     int                bufptr;
64 
65     int                fd;
66 } CanHostSocketCAN;
67 
68 /* Check that QEMU and Linux kernel flags encoding and structure matches */
69 QEMU_BUILD_BUG_ON(QEMU_CAN_EFF_FLAG != CAN_EFF_FLAG);
70 QEMU_BUILD_BUG_ON(QEMU_CAN_RTR_FLAG != CAN_RTR_FLAG);
71 QEMU_BUILD_BUG_ON(QEMU_CAN_ERR_FLAG != CAN_ERR_FLAG);
72 QEMU_BUILD_BUG_ON(QEMU_CAN_INV_FILTER != CAN_INV_FILTER);
73 QEMU_BUILD_BUG_ON(offsetof(qemu_can_frame, data)
74                   != offsetof(struct can_frame, data));
75 
76 static void can_host_socketcan_display_msg(struct qemu_can_frame *msg)
77 {
78     int i;
79     FILE *logfile = qemu_log_lock();
80     qemu_log("[cansocketcan]: %03X [%01d] %s %s",
81              msg->can_id & QEMU_CAN_EFF_MASK,
82              msg->can_dlc,
83              msg->can_id & QEMU_CAN_EFF_FLAG ? "EFF" : "SFF",
84              msg->can_id & QEMU_CAN_RTR_FLAG ? "RTR" : "DAT");
85 
86     for (i = 0; i < msg->can_dlc; i++) {
87         qemu_log(" %02X", msg->data[i]);
88     }
89     qemu_log("\n");
90     qemu_log_flush();
91     qemu_log_unlock(logfile);
92 }
93 
94 static void can_host_socketcan_read(void *opaque)
95 {
96     CanHostSocketCAN *c = opaque;
97     CanHostState *ch = CAN_HOST(c);
98 
99     /* CAN_READ_BUF_LEN for multiple messages syscall is possible for future */
100     c->bufcnt = read(c->fd, c->buf, sizeof(qemu_can_frame));
101     if (c->bufcnt < 0) {
102         warn_report("CAN bus host read failed (%s)", strerror(errno));
103         return;
104     }
105 
106     can_bus_client_send(&ch->bus_client, c->buf, 1);
107 
108     if (DEBUG_CAN) {
109         can_host_socketcan_display_msg(c->buf);
110     }
111 }
112 
113 static int can_host_socketcan_can_receive(CanBusClientState *client)
114 {
115     return 1;
116 }
117 
118 static ssize_t can_host_socketcan_receive(CanBusClientState *client,
119                             const qemu_can_frame *frames, size_t frames_cnt)
120 {
121     CanHostState *ch = container_of(client, CanHostState, bus_client);
122     CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(ch);
123 
124     size_t len = sizeof(qemu_can_frame);
125     int res;
126 
127     if (c->fd < 0) {
128         return -1;
129     }
130 
131     res = write(c->fd, frames, len);
132 
133     if (!res) {
134         warn_report("[cansocketcan]: write message to host returns zero");
135         return -1;
136     }
137 
138     if (res != len) {
139         if (res < 0) {
140             warn_report("[cansocketcan]: write to host failed (%s)",
141                         strerror(errno));
142         } else {
143             warn_report("[cansocketcan]: write to host truncated");
144         }
145         return -1;
146     }
147 
148     return 1;
149 }
150 
151 static void can_host_socketcan_disconnect(CanHostState *ch)
152 {
153     CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(ch);
154 
155     if (c->fd >= 0) {
156         qemu_set_fd_handler(c->fd, NULL, NULL, c);
157         close(c->fd);
158         c->fd = -1;
159     }
160 
161     g_free(c->rfilter);
162     c->rfilter = NULL;
163     c->rfilter_num = 0;
164 }
165 
166 static CanBusClientInfo can_host_socketcan_bus_client_info = {
167     .can_receive = can_host_socketcan_can_receive,
168     .receive = can_host_socketcan_receive,
169 };
170 
171 static void can_host_socketcan_connect(CanHostState *ch, Error **errp)
172 {
173     CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(ch);
174     int s; /* can raw socket */
175     struct sockaddr_can addr;
176     struct ifreq ifr;
177 
178     /* open socket */
179     s = qemu_socket(PF_CAN, SOCK_RAW, CAN_RAW);
180     if (s < 0) {
181         error_setg_errno(errp, errno, "failed to create CAN_RAW socket");
182         return;
183     }
184 
185     addr.can_family = AF_CAN;
186     memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
187     strcpy(ifr.ifr_name, c->ifname);
188     if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
189         error_setg_errno(errp, errno,
190                          "SocketCAN host interface %s not available", c->ifname);
191         goto fail;
192     }
193     addr.can_ifindex = ifr.ifr_ifindex;
194 
195     c->err_mask = 0xffffffff; /* Receive error frame. */
196     setsockopt(s, SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
197                    &c->err_mask, sizeof(c->err_mask));
198 
199     c->rfilter_num = 1;
200     c->rfilter = g_new(struct qemu_can_filter, c->rfilter_num);
201 
202     /* Receive all data frame. If |= CAN_INV_FILTER no data. */
203     c->rfilter[0].can_id = 0;
204     c->rfilter[0].can_mask = 0;
205     c->rfilter[0].can_mask &= ~CAN_ERR_FLAG;
206 
207     setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, c->rfilter,
208                c->rfilter_num * sizeof(struct qemu_can_filter));
209 
210     if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
211         error_setg_errno(errp, errno, "failed to bind to host interface %s",
212                          c->ifname);
213         goto fail;
214     }
215 
216     c->fd = s;
217     ch->bus_client.info = &can_host_socketcan_bus_client_info;
218     qemu_set_fd_handler(c->fd, can_host_socketcan_read, NULL, c);
219     return;
220 
221 fail:
222     close(s);
223     g_free(c->rfilter);
224     c->rfilter = NULL;
225     c->rfilter_num = 0;
226 }
227 
228 static char *can_host_socketcan_get_if(Object *obj, Error **errp)
229 {
230     CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(obj);
231 
232     return g_strdup(c->ifname);
233 }
234 
235 static void can_host_socketcan_set_if(Object *obj, const char *value, Error **errp)
236 {
237     CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(obj);
238     struct ifreq ifr;
239 
240     if (strlen(value) >= sizeof(ifr.ifr_name)) {
241         error_setg(errp, "CAN interface name longer than %zd characters",
242                    sizeof(ifr.ifr_name) - 1);
243         return;
244     }
245 
246     if (c->fd != -1) {
247         error_setg(errp, "CAN interface already connected");
248         return;
249     }
250 
251     g_free(c->ifname);
252     c->ifname = g_strdup(value);
253 }
254 
255 static void can_host_socketcan_instance_init(Object *obj)
256 {
257     CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(obj);
258 
259     c->fd = -1;
260 }
261 
262 static void can_host_socketcan_class_init(ObjectClass *klass,
263                                           void *class_data G_GNUC_UNUSED)
264 {
265     CanHostClass *chc = CAN_HOST_CLASS(klass);
266 
267     object_class_property_add_str(klass, "if",
268                                   can_host_socketcan_get_if,
269                                   can_host_socketcan_set_if,
270                                   &error_abort);
271     chc->connect = can_host_socketcan_connect;
272     chc->disconnect = can_host_socketcan_disconnect;
273 }
274 
275 static const TypeInfo can_host_socketcan_info = {
276     .parent = TYPE_CAN_HOST,
277     .name = TYPE_CAN_HOST_SOCKETCAN,
278     .instance_size = sizeof(CanHostSocketCAN),
279     .instance_init = can_host_socketcan_instance_init,
280     .class_init = can_host_socketcan_class_init,
281 };
282 
283 static void can_host_register_types(void)
284 {
285     type_register_static(&can_host_socketcan_info);
286 }
287 
288 type_init(can_host_register_types);
289