xref: /openbmc/qemu/net/can/can_socketcan.c (revision 64f7aece)
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 #include "qom/object.h"
44 
45 #ifndef DEBUG_CAN
46 #define DEBUG_CAN 0
47 #endif /*DEBUG_CAN*/
48 
49 #define TYPE_CAN_HOST_SOCKETCAN "can-host-socketcan"
50 typedef struct CanHostSocketCAN CanHostSocketCAN;
51 DECLARE_INSTANCE_CHECKER(CanHostSocketCAN, CAN_HOST_SOCKETCAN,
52                          TYPE_CAN_HOST_SOCKETCAN)
53 
54 #define CAN_READ_BUF_LEN  5
55 struct CanHostSocketCAN {
56     CanHostState       parent;
57     char               *ifname;
58 
59     qemu_can_filter    *rfilter;
60     int                rfilter_num;
61     can_err_mask_t     err_mask;
62 
63     qemu_can_frame     buf[CAN_READ_BUF_LEN];
64     int                bufcnt;
65     int                bufptr;
66 
67     int                fd;
68 };
69 
70 /* Check that QEMU and Linux kernel flags encoding and structure matches */
71 QEMU_BUILD_BUG_ON(QEMU_CAN_EFF_FLAG != CAN_EFF_FLAG);
72 QEMU_BUILD_BUG_ON(QEMU_CAN_RTR_FLAG != CAN_RTR_FLAG);
73 QEMU_BUILD_BUG_ON(QEMU_CAN_ERR_FLAG != CAN_ERR_FLAG);
74 QEMU_BUILD_BUG_ON(QEMU_CAN_INV_FILTER != CAN_INV_FILTER);
75 QEMU_BUILD_BUG_ON(offsetof(qemu_can_frame, data)
76                   != offsetof(struct can_frame, data));
77 
78 static void can_host_socketcan_display_msg(struct qemu_can_frame *msg)
79 {
80     int i;
81     FILE *logfile = qemu_log_lock();
82     qemu_log("[cansocketcan]: %03X [%01d] %s %s",
83              msg->can_id & QEMU_CAN_EFF_MASK,
84              msg->can_dlc,
85              msg->can_id & QEMU_CAN_EFF_FLAG ? "EFF" : "SFF",
86              msg->can_id & QEMU_CAN_RTR_FLAG ? "RTR" : "DAT");
87 
88     for (i = 0; i < msg->can_dlc; i++) {
89         qemu_log(" %02X", msg->data[i]);
90     }
91     qemu_log("\n");
92     qemu_log_flush();
93     qemu_log_unlock(logfile);
94 }
95 
96 static void can_host_socketcan_read(void *opaque)
97 {
98     CanHostSocketCAN *c = opaque;
99     CanHostState *ch = CAN_HOST(c);
100 
101     /* CAN_READ_BUF_LEN for multiple messages syscall is possible for future */
102     c->bufcnt = read(c->fd, c->buf, sizeof(qemu_can_frame));
103     if (c->bufcnt < 0) {
104         warn_report("CAN bus host read failed (%s)", strerror(errno));
105         return;
106     }
107 
108     can_bus_client_send(&ch->bus_client, c->buf, 1);
109 
110     if (DEBUG_CAN) {
111         can_host_socketcan_display_msg(c->buf);
112     }
113 }
114 
115 static bool can_host_socketcan_can_receive(CanBusClientState *client)
116 {
117     return true;
118 }
119 
120 static ssize_t can_host_socketcan_receive(CanBusClientState *client,
121                             const qemu_can_frame *frames, size_t frames_cnt)
122 {
123     CanHostState *ch = container_of(client, CanHostState, bus_client);
124     CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(ch);
125 
126     size_t len = sizeof(qemu_can_frame);
127     int res;
128 
129     if (c->fd < 0) {
130         return -1;
131     }
132 
133     res = write(c->fd, frames, len);
134 
135     if (!res) {
136         warn_report("[cansocketcan]: write message to host returns zero");
137         return -1;
138     }
139 
140     if (res != len) {
141         if (res < 0) {
142             warn_report("[cansocketcan]: write to host failed (%s)",
143                         strerror(errno));
144         } else {
145             warn_report("[cansocketcan]: write to host truncated");
146         }
147         return -1;
148     }
149 
150     return 1;
151 }
152 
153 static void can_host_socketcan_disconnect(CanHostState *ch)
154 {
155     CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(ch);
156 
157     if (c->fd >= 0) {
158         qemu_set_fd_handler(c->fd, NULL, NULL, c);
159         close(c->fd);
160         c->fd = -1;
161     }
162 
163     g_free(c->rfilter);
164     c->rfilter = NULL;
165     c->rfilter_num = 0;
166 }
167 
168 static CanBusClientInfo can_host_socketcan_bus_client_info = {
169     .can_receive = can_host_socketcan_can_receive,
170     .receive = can_host_socketcan_receive,
171 };
172 
173 static void can_host_socketcan_connect(CanHostState *ch, Error **errp)
174 {
175     CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(ch);
176     int s; /* can raw socket */
177     struct sockaddr_can addr;
178     struct ifreq ifr;
179 
180     /* open socket */
181     s = qemu_socket(PF_CAN, SOCK_RAW, CAN_RAW);
182     if (s < 0) {
183         error_setg_errno(errp, errno, "failed to create CAN_RAW socket");
184         return;
185     }
186 
187     addr.can_family = AF_CAN;
188     memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
189     strcpy(ifr.ifr_name, c->ifname);
190     if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
191         error_setg_errno(errp, errno,
192                          "SocketCAN host interface %s not available", c->ifname);
193         goto fail;
194     }
195     addr.can_ifindex = ifr.ifr_ifindex;
196 
197     c->err_mask = 0xffffffff; /* Receive error frame. */
198     setsockopt(s, SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
199                    &c->err_mask, sizeof(c->err_mask));
200 
201     c->rfilter_num = 1;
202     c->rfilter = g_new(struct qemu_can_filter, c->rfilter_num);
203 
204     /* Receive all data frame. If |= CAN_INV_FILTER no data. */
205     c->rfilter[0].can_id = 0;
206     c->rfilter[0].can_mask = 0;
207     c->rfilter[0].can_mask &= ~CAN_ERR_FLAG;
208 
209     setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, c->rfilter,
210                c->rfilter_num * sizeof(struct qemu_can_filter));
211 
212     if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
213         error_setg_errno(errp, errno, "failed to bind to host interface %s",
214                          c->ifname);
215         goto fail;
216     }
217 
218     c->fd = s;
219     ch->bus_client.info = &can_host_socketcan_bus_client_info;
220     qemu_set_fd_handler(c->fd, can_host_socketcan_read, NULL, c);
221     return;
222 
223 fail:
224     close(s);
225     g_free(c->rfilter);
226     c->rfilter = NULL;
227     c->rfilter_num = 0;
228 }
229 
230 static char *can_host_socketcan_get_if(Object *obj, Error **errp)
231 {
232     CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(obj);
233 
234     return g_strdup(c->ifname);
235 }
236 
237 static void can_host_socketcan_set_if(Object *obj, const char *value, Error **errp)
238 {
239     CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(obj);
240     struct ifreq ifr;
241 
242     if (strlen(value) >= sizeof(ifr.ifr_name)) {
243         error_setg(errp, "CAN interface name longer than %zd characters",
244                    sizeof(ifr.ifr_name) - 1);
245         return;
246     }
247 
248     if (c->fd != -1) {
249         error_setg(errp, "CAN interface already connected");
250         return;
251     }
252 
253     g_free(c->ifname);
254     c->ifname = g_strdup(value);
255 }
256 
257 static void can_host_socketcan_instance_init(Object *obj)
258 {
259     CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(obj);
260 
261     c->fd = -1;
262 }
263 
264 static void can_host_socketcan_class_init(ObjectClass *klass,
265                                           void *class_data G_GNUC_UNUSED)
266 {
267     CanHostClass *chc = CAN_HOST_CLASS(klass);
268 
269     object_class_property_add_str(klass, "if",
270                                   can_host_socketcan_get_if,
271                                   can_host_socketcan_set_if);
272     chc->connect = can_host_socketcan_connect;
273     chc->disconnect = can_host_socketcan_disconnect;
274 }
275 
276 static const TypeInfo can_host_socketcan_info = {
277     .parent = TYPE_CAN_HOST,
278     .name = TYPE_CAN_HOST_SOCKETCAN,
279     .instance_size = sizeof(CanHostSocketCAN),
280     .instance_init = can_host_socketcan_instance_init,
281     .class_init = can_host_socketcan_class_init,
282 };
283 
284 static void can_host_register_types(void)
285 {
286     type_register_static(&can_host_socketcan_info);
287 }
288 
289 type_init(can_host_register_types);
290