1 /* vdagent-virtio-port.c virtio port communication code
3 Copyright 2010 Red Hat, Inc.
6 Hans de Goede <hdegoede@redhat.com>
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/select.h>
28 #include "vdagent-virtio-port.h"
30 struct vdagent_virtio_port_buf
{
35 struct vdagent_virtio_port_buf
*next
;
38 struct vdagent_virtio_port
{
42 /* Read stuff, single buffer, separate header and data buffer */
43 int chunk_header_read
;
45 int message_header_read
;
47 VDIChunkHeader chunk_header
;
48 VDAgentMessage message_header
;
49 uint8_t chunk_data
[VD_AGENT_MAX_DATA_SIZE
];
50 uint8_t *message_data
;
52 /* Writes are stored in a linked list of buffers, with both the header
53 + data for a single message in 1 buffer. */
54 struct vdagent_virtio_port_buf
*write_buf
;
57 vdagent_virtio_port_read_callback read_callback
;
58 vdagent_virtio_port_disconnect_callback disconnect_callback
;
61 static void vdagent_virtio_port_do_write(struct vdagent_virtio_port
**vportp
);
62 static void vdagent_virtio_port_do_read(struct vdagent_virtio_port
**vportp
);
64 struct vdagent_virtio_port
*vdagent_virtio_port_create(const char *portname
,
65 vdagent_virtio_port_read_callback read_callback
,
66 vdagent_virtio_port_disconnect_callback disconnect_callback
,
69 struct vdagent_virtio_port
*vport
;
71 vport
= calloc(1, sizeof(*vport
));
75 vport
->errfile
= errfile
;
76 vport
->fd
= open(portname
, O_RDWR
);
77 if (vport
->fd
== -1) {
78 fprintf(vport
->errfile
, "open %s: %s\n", portname
, strerror(errno
));
83 vport
->read_callback
= read_callback
;
84 vport
->disconnect_callback
= disconnect_callback
;
89 void vdagent_virtio_port_destroy(struct vdagent_virtio_port
**vportp
)
91 struct vdagent_virtio_port_buf
*wbuf
, *next_wbuf
;
92 struct vdagent_virtio_port
*vport
= *vportp
;
97 if (vport
->disconnect_callback
)
98 vport
->disconnect_callback(vport
);
100 wbuf
= vport
->write_buf
;
102 next_wbuf
= wbuf
->next
;
108 free(vport
->message_data
);
115 int vdagent_virtio_port_fill_fds(struct vdagent_virtio_port
*vport
,
116 fd_set
*readfds
, fd_set
*writefds
)
121 FD_SET(vport
->fd
, readfds
);
122 if (vport
->write_buf
)
123 FD_SET(vport
->fd
, writefds
);
125 return vport
->fd
+ 1;
128 void vdagent_virtio_port_handle_fds(struct vdagent_virtio_port
**vportp
,
129 fd_set
*readfds
, fd_set
*writefds
)
134 if (FD_ISSET((*vportp
)->fd
, readfds
))
135 vdagent_virtio_port_do_read(vportp
);
137 if (*vportp
&& FD_ISSET((*vportp
)->fd
, writefds
))
138 vdagent_virtio_port_do_write(vportp
);
141 int vdagent_virtio_port_write(
142 struct vdagent_virtio_port
*vport
,
144 uint32_t message_type
,
145 uint32_t message_opaque
,
149 struct vdagent_virtio_port_buf
*wbuf
, *new_wbuf
;
150 VDIChunkHeader chunk_header
;
151 VDAgentMessage message_header
;
153 new_wbuf
= malloc(sizeof(*new_wbuf
));
158 new_wbuf
->size
= sizeof(chunk_header
) + sizeof(message_header
) + data_size
;
159 new_wbuf
->next
= NULL
;
160 new_wbuf
->buf
= malloc(new_wbuf
->size
);
161 if (!new_wbuf
->buf
) {
166 chunk_header
.port
= port_nr
;
167 chunk_header
.size
= sizeof(message_header
) + data_size
;
168 message_header
.protocol
= VD_AGENT_PROTOCOL
;
169 message_header
.type
= message_type
;
170 message_header
.opaque
= message_opaque
;
171 message_header
.size
= data_size
;
173 memcpy(new_wbuf
->buf
, &chunk_header
, sizeof(chunk_header
));
174 memcpy(new_wbuf
->buf
+ sizeof(chunk_header
), &message_header
,
175 sizeof(message_header
));
176 memcpy(new_wbuf
->buf
+ sizeof(chunk_header
) + sizeof(message_header
),
179 if (!vport
->write_buf
) {
180 vport
->write_buf
= new_wbuf
;
184 /* maybe we should limit the write_buf stack depth ? */
185 wbuf
= vport
->write_buf
;
189 wbuf
->next
= new_wbuf
;
194 void vdagent_virtio_port_flush(struct vdagent_virtio_port
**vportp
)
196 while (*vportp
&& (*vportp
)->write_buf
)
197 vdagent_virtio_port_do_write(vportp
);
200 static void vdagent_virtio_port_do_chunk(struct vdagent_virtio_port
**vportp
)
202 int avail
, read
, pos
= 0;
203 struct vdagent_virtio_port
*vport
= *vportp
;
205 if (vport
->message_header_read
< sizeof(vport
->message_header
)) {
206 read
= sizeof(vport
->message_header
) - vport
->message_header_read
;
207 if (read
> vport
->chunk_header
.size
) {
208 read
= vport
->chunk_header
.size
;
210 memcpy((uint8_t *)&vport
->message_header
+ vport
->message_header_read
,
211 vport
->chunk_data
, read
);
212 vport
->message_header_read
+= read
;
213 if (vport
->message_header_read
== sizeof(vport
->message_header
) &&
214 vport
->message_header
.size
) {
215 vport
->message_data
= malloc(vport
->message_header
.size
);
216 if (!vport
->message_data
) {
217 fprintf(vport
->errfile
, "out of memory, disconnecting virtio\n");
218 vdagent_virtio_port_destroy(vportp
);
225 if (vport
->message_header_read
== sizeof(vport
->message_header
)) {
226 read
= vport
->message_header
.size
- vport
->message_data_pos
;
227 avail
= vport
->chunk_header
.size
- pos
;
230 fprintf(vport
->errfile
, "chunk larger then message, lost sync?\n");
231 vdagent_virtio_port_destroy(vportp
);
239 memcpy(vport
->message_data
+ vport
->message_data_pos
,
240 vport
->chunk_data
+ pos
, read
);
241 vport
->message_data_pos
+= read
;
244 if (vport
->message_data_pos
== vport
->message_header
.size
) {
245 if (vport
->read_callback
) {
246 int r
= vport
->read_callback(vport
, &vport
->chunk_header
,
247 &vport
->message_header
, vport
->message_data
);
249 vdagent_virtio_port_destroy(vportp
);
253 vport
->message_header_read
= 0;
254 vport
->message_data_pos
= 0;
255 free(vport
->message_data
);
256 vport
->message_data
= NULL
;
261 static void vdagent_virtio_port_do_read(struct vdagent_virtio_port
**vportp
)
266 struct vdagent_virtio_port
*vport
= *vportp
;
268 if (vport
->chunk_header_read
< sizeof(vport
->chunk_header
)) {
269 to_read
= sizeof(vport
->chunk_header
) - vport
->chunk_header_read
;
270 dest
= (uint8_t *)&vport
->chunk_header
+ vport
->chunk_header_read
;
272 to_read
= vport
->chunk_header
.size
- vport
->chunk_data_pos
;
273 dest
= vport
->chunk_data
+ vport
->chunk_data_pos
;
276 n
= read(vport
->fd
, dest
, to_read
);
280 fprintf(vport
->errfile
, "reading from vdagent virtio port: %s\n",
284 vdagent_virtio_port_destroy(vportp
);
288 if (vport
->chunk_header_read
< sizeof(vport
->chunk_header
)) {
289 vport
->chunk_header_read
+= n
;
290 if (vport
->chunk_header_read
== sizeof(vport
->chunk_header
)) {
291 if (vport
->chunk_header
.size
> VD_AGENT_MAX_DATA_SIZE
) {
292 fprintf(vport
->errfile
, "chunk size too large\n");
293 vdagent_virtio_port_destroy(vportp
);
298 vport
->chunk_data_pos
+= n
;
299 if (vport
->chunk_data_pos
== vport
->chunk_header
.size
) {
300 vdagent_virtio_port_do_chunk(vportp
);
301 vport
->chunk_header_read
= 0;
302 vport
->chunk_data_pos
= 0;
307 static void vdagent_virtio_port_do_write(struct vdagent_virtio_port
**vportp
)
311 struct vdagent_virtio_port
*vport
= *vportp
;
313 struct vdagent_virtio_port_buf
* wbuf
= vport
->write_buf
;
315 fprintf(vport
->errfile
,
316 "do_write called on a port without a write buf ?!\n");
320 to_write
= wbuf
->size
- wbuf
->pos
;
321 n
= write(vport
->fd
, wbuf
->buf
+ wbuf
->pos
, to_write
);
325 fprintf(vport
->errfile
, "writing to vdagent virtio port: %s\n",
327 vdagent_virtio_port_destroy(vportp
);
332 if (wbuf
->pos
== wbuf
->size
) {
333 vport
->write_buf
= wbuf
->next
;