2 * Copyright (c) 2015 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
3 * Copyright (c) 2015 Leon Dang
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #ifndef WITHOUT_CAPSICUM
33 #include <sys/capsicum.h>
35 #include <sys/socket.h>
36 #include <sys/select.h>
38 #include <arpa/inet.h>
39 #include <machine/cpufunc.h>
40 #include <machine/specialreg.h>
41 #include <netinet/in.h>
47 #include <pthread_np.h>
61 #include "sockstream.h"
63 static int rfb_debug = 0;
64 #define DPRINTF(params) if (rfb_debug) printf params
65 #define WPRINTF(params) printf params
89 uint32_t *crc; /* WxH crc cells */
90 uint32_t *crc_tmp; /* buffer to store single crc row */
91 int crc_width, crc_height;
108 struct rfb_srvr_info {
111 struct rfb_pixfmt pixfmt;
115 struct rfb_pixfmt_msg {
118 struct rfb_pixfmt pixfmt;
121 #define RFB_ENCODING_RAW 0
122 #define RFB_ENCODING_ZLIB 6
123 #define RFB_ENCODING_RESIZE -223
125 #define RFB_MAX_WIDTH 2000
126 #define RFB_MAX_HEIGHT 1200
127 #define RFB_ZLIB_BUFSZ RFB_MAX_WIDTH*RFB_MAX_HEIGHT*4
129 /* percentage changes to screen before sending the entire screen */
130 #define RFB_SEND_ALL_THRESH 25
138 struct rfb_updt_msg {
161 struct rfb_srvr_updt_msg {
167 struct rfb_srvr_rect_hdr {
175 struct rfb_cuttext_msg {
183 rfb_send_server_init_msg(int cfd)
185 struct bhyvegc_image *gc_image;
186 struct rfb_srvr_info sinfo;
188 gc_image = console_get_image();
190 sinfo.width = htons(gc_image->width);
191 sinfo.height = htons(gc_image->height);
192 sinfo.pixfmt.bpp = 32;
193 sinfo.pixfmt.depth = 32;
194 sinfo.pixfmt.bigendian = 0;
195 sinfo.pixfmt.truecolor = 1;
196 sinfo.pixfmt.red_max = htons(255);
197 sinfo.pixfmt.green_max = htons(255);
198 sinfo.pixfmt.blue_max = htons(255);
199 sinfo.pixfmt.red_shift = 16;
200 sinfo.pixfmt.green_shift = 8;
201 sinfo.pixfmt.blue_shift = 0;
202 sinfo.namelen = htonl(strlen("bhyve"));
203 (void)stream_write(cfd, &sinfo, sizeof(sinfo));
204 (void)stream_write(cfd, "bhyve", strlen("bhyve"));
208 rfb_send_resize_update_msg(struct rfb_softc *rc, int cfd)
210 struct rfb_srvr_updt_msg supdt_msg;
211 struct rfb_srvr_rect_hdr srect_hdr;
213 /* Number of rectangles: 1 */
216 supdt_msg.numrects = htons(1);
217 stream_write(cfd, &supdt_msg, sizeof(struct rfb_srvr_updt_msg));
219 /* Rectangle header */
220 srect_hdr.x = htons(0);
221 srect_hdr.y = htons(0);
222 srect_hdr.width = htons(rc->width);
223 srect_hdr.height = htons(rc->height);
224 srect_hdr.encoding = htonl(RFB_ENCODING_RESIZE);
225 stream_write(cfd, &srect_hdr, sizeof(struct rfb_srvr_rect_hdr));
229 rfb_recv_set_pixfmt_msg(struct rfb_softc *rc, int cfd)
231 struct rfb_pixfmt_msg pixfmt_msg;
233 (void)stream_read(cfd, ((void *)&pixfmt_msg)+1, sizeof(pixfmt_msg)-1);
238 rfb_recv_set_encodings_msg(struct rfb_softc *rc, int cfd)
240 struct rfb_enc_msg enc_msg;
244 assert((sizeof(enc_msg) - 1) == 3);
245 (void)stream_read(cfd, ((void *)&enc_msg)+1, sizeof(enc_msg)-1);
247 for (i = 0; i < htons(enc_msg.numencs); i++) {
248 (void)stream_read(cfd, &encoding, sizeof(encoding));
249 switch (htonl(encoding)) {
250 case RFB_ENCODING_RAW:
251 rc->enc_raw_ok = true;
253 case RFB_ENCODING_ZLIB:
254 rc->enc_zlib_ok = true;
255 deflateInit(&rc->zstream, Z_BEST_SPEED);
257 case RFB_ENCODING_RESIZE:
258 rc->enc_resize_ok = true;
265 * Calculate CRC32 using SSE4.2; Intel or AMD Bulldozer+ CPUs only
267 static __inline uint32_t
268 fast_crc32(void *buf, int len, uint32_t crcval)
270 uint32_t q = len / sizeof(uint32_t);
271 uint32_t *p = (uint32_t *)buf;
275 ".byte 0xf2, 0xf, 0x38, 0xf1, 0xf1;"
277 :"0" (crcval), "c" (*p)
287 rfb_send_rect(struct rfb_softc *rc, int cfd, struct bhyvegc_image *gc,
288 int x, int y, int w, int h)
290 struct rfb_srvr_updt_msg supdt_msg;
291 struct rfb_srvr_rect_hdr srect_hdr;
293 ssize_t nwrite, total;
299 * Send a single rectangle of the given x, y, w h dimensions.
302 /* Number of rectangles: 1 */
305 supdt_msg.numrects = htons(1);
306 nwrite = stream_write(cfd, &supdt_msg,
307 sizeof(struct rfb_srvr_updt_msg));
312 /* Rectangle header */
313 srect_hdr.x = htons(x);
314 srect_hdr.y = htons(y);
315 srect_hdr.width = htons(w);
316 srect_hdr.height = htons(h);
319 w *= sizeof(uint32_t);
320 if (rc->enc_zlib_ok) {
322 rc->zstream.total_in = 0;
323 rc->zstream.total_out = 0;
324 for (p = &gc->data[y * gc->width + x]; y < h; y++) {
325 rc->zstream.next_in = (Bytef *)p;
326 rc->zstream.avail_in = w;
327 rc->zstream.next_out = (Bytef *)zbufp;
328 rc->zstream.avail_out = RFB_ZLIB_BUFSZ + 16 -
329 rc->zstream.total_out;
330 rc->zstream.data_type = Z_BINARY;
332 /* Compress with zlib */
333 err = deflate(&rc->zstream, Z_SYNC_FLUSH);
335 WPRINTF(("zlib[rect] deflate err: %d\n", err));
336 rc->enc_zlib_ok = false;
337 deflateEnd(&rc->zstream);
340 zbufp = rc->zbuf + rc->zstream.total_out;
343 srect_hdr.encoding = htonl(RFB_ENCODING_ZLIB);
344 nwrite = stream_write(cfd, &srect_hdr,
345 sizeof(struct rfb_srvr_rect_hdr));
349 zlen = htonl(rc->zstream.total_out);
350 nwrite = stream_write(cfd, &zlen, sizeof(uint32_t));
353 return (stream_write(cfd, rc->zbuf, rc->zstream.total_out));
360 for (p = &gc->data[y * gc->width + x]; y < h; y++) {
367 srect_hdr.encoding = htonl(RFB_ENCODING_RAW);
368 nwrite = stream_write(cfd, &srect_hdr,
369 sizeof(struct rfb_srvr_rect_hdr));
373 total = stream_write(cfd, rc->zbuf, total);
379 rfb_send_all(struct rfb_softc *rc, int cfd, struct bhyvegc_image *gc)
381 struct rfb_srvr_updt_msg supdt_msg;
382 struct rfb_srvr_rect_hdr srect_hdr;
388 * Send the whole thing
391 /* Number of rectangles: 1 */
394 supdt_msg.numrects = htons(1);
395 nwrite = stream_write(cfd, &supdt_msg,
396 sizeof(struct rfb_srvr_updt_msg));
400 /* Rectangle header */
403 srect_hdr.width = htons(gc->width);
404 srect_hdr.height = htons(gc->height);
405 if (rc->enc_zlib_ok) {
406 rc->zstream.next_in = (Bytef *)gc->data;
407 rc->zstream.avail_in = gc->width * gc->height *
409 rc->zstream.next_out = (Bytef *)rc->zbuf;
410 rc->zstream.avail_out = RFB_ZLIB_BUFSZ + 16;
411 rc->zstream.data_type = Z_BINARY;
413 rc->zstream.total_in = 0;
414 rc->zstream.total_out = 0;
416 /* Compress with zlib */
417 err = deflate(&rc->zstream, Z_SYNC_FLUSH);
419 WPRINTF(("zlib deflate err: %d\n", err));
420 rc->enc_zlib_ok = false;
421 deflateEnd(&rc->zstream);
425 srect_hdr.encoding = htonl(RFB_ENCODING_ZLIB);
426 nwrite = stream_write(cfd, &srect_hdr,
427 sizeof(struct rfb_srvr_rect_hdr));
431 zlen = htonl(rc->zstream.total_out);
432 nwrite = stream_write(cfd, &zlen, sizeof(uint32_t));
435 return (stream_write(cfd, rc->zbuf, rc->zstream.total_out));
439 srect_hdr.encoding = htonl(RFB_ENCODING_RAW);
440 nwrite = stream_write(cfd, &srect_hdr,
441 sizeof(struct rfb_srvr_rect_hdr));
445 nwrite = stream_write(cfd, gc->data,
446 gc->width * gc->height * sizeof(uint32_t));
451 #define PIX_PER_CELL 32
452 #define PIXCELL_SHIFT 5
453 #define PIXCELL_MASK 0x1F
456 rfb_send_screen(struct rfb_softc *rc, int cfd, int all)
458 struct bhyvegc_image *gc_image;
461 int celly, cellwidth;
465 int rem_x, rem_y; /* remainder for resolutions not x32 pixels ratio */
467 uint32_t *crc_p, *orig_crc;
471 gc_image = console_get_image();
473 pthread_mutex_lock(&rc->mtx);
475 pthread_mutex_unlock(&rc->mtx);
479 pthread_mutex_unlock(&rc->mtx);
484 retval = rfb_send_all(rc, cfd, gc_image);
489 * Calculate the checksum for each 32x32 cell. Send each that
490 * has changed since the last scan.
493 /* Resolution changed */
495 rc->crc_width = gc_image->width;
496 rc->crc_height = gc_image->height;
500 xcells = howmany(rc->crc_width, PIX_PER_CELL);
501 ycells = howmany(rc->crc_height, PIX_PER_CELL);
503 rem_x = w & PIXCELL_MASK;
505 rem_y = h & PIXCELL_MASK;
507 rem_y = PIX_PER_CELL;
512 * Go through all cells and calculate crc. If significant number
513 * of changes, then send entire screen.
514 * crc_tmp is dual purpose: to store the new crc and to flag as
515 * a cell that has changed.
517 crc_p = rc->crc_tmp - xcells;
518 orig_crc = rc->crc - xcells;
520 memset(rc->crc_tmp, 0, sizeof(uint32_t) * xcells * ycells);
521 for (y = 0; y < h; y++) {
522 if ((y & PIXCELL_MASK) == 0) {
527 for (x = 0; x < xcells; x++) {
529 crc_p[x] = fast_crc32(p,
530 PIX_PER_CELL * sizeof(uint32_t),
533 crc_p[x] = (uint32_t)crc32(crc_p[x],
535 PIX_PER_CELL * sizeof(uint32_t));
539 /* check for crc delta if last row in cell */
540 if ((y & PIXCELL_MASK) == PIXCELL_MASK || y == (h-1)) {
541 if (orig_crc[x] != crc_p[x]) {
542 orig_crc[x] = crc_p[x];
553 crc_p[x] = fast_crc32(p,
554 rem_x * sizeof(uint32_t),
557 crc_p[x] = (uint32_t)crc32(crc_p[x],
559 rem_x * sizeof(uint32_t));
562 if ((y & PIXCELL_MASK) == PIXCELL_MASK || y == (h-1)) {
563 if (orig_crc[x] != crc_p[x]) {
564 orig_crc[x] = crc_p[x];
574 /* If number of changes is > THRESH percent, send the whole screen */
575 if (((changes * 100) / (xcells * ycells)) >= RFB_SEND_ALL_THRESH) {
576 retval = rfb_send_all(rc, cfd, gc_image);
580 /* Go through all cells, and send only changed ones */
582 for (y = 0; y < h; y += PIX_PER_CELL) {
583 /* previous cell's row */
584 celly = (y >> PIXCELL_SHIFT);
586 /* Delta check crc to previous set */
587 for (x = 0; x < xcells; x++) {
591 if (x == (xcells - 1) && rem_x > 0)
594 cellwidth = PIX_PER_CELL;
595 nwrite = rfb_send_rect(rc, cfd,
598 celly * PIX_PER_CELL,
600 y + PIX_PER_CELL >= h ? rem_y : PIX_PER_CELL);
610 pthread_mutex_lock(&rc->mtx);
612 pthread_mutex_unlock(&rc->mtx);
619 rfb_recv_update_msg(struct rfb_softc *rc, int cfd, int discardonly)
621 struct rfb_updt_msg updt_msg;
622 struct bhyvegc_image *gc_image;
624 (void)stream_read(cfd, ((void *)&updt_msg) + 1 , sizeof(updt_msg) - 1);
627 gc_image = console_get_image();
629 updt_msg.x = htons(updt_msg.x);
630 updt_msg.y = htons(updt_msg.y);
631 updt_msg.width = htons(updt_msg.width);
632 updt_msg.height = htons(updt_msg.height);
634 if (updt_msg.width != gc_image->width ||
635 updt_msg.height != gc_image->height) {
636 rc->width = gc_image->width;
637 rc->height = gc_image->height;
638 if (rc->enc_resize_ok)
639 rfb_send_resize_update_msg(rc, cfd);
645 rfb_send_screen(rc, cfd, 1);
649 rfb_recv_key_msg(struct rfb_softc *rc, int cfd)
651 struct rfb_key_msg key_msg;
653 (void)stream_read(cfd, ((void *)&key_msg) + 1, sizeof(key_msg) - 1);
655 console_key_event(key_msg.down, htonl(key_msg.code));
659 rfb_recv_ptr_msg(struct rfb_softc *rc, int cfd)
661 struct rfb_ptr_msg ptr_msg;
663 (void)stream_read(cfd, ((void *)&ptr_msg) + 1, sizeof(ptr_msg) - 1);
665 console_ptr_event(ptr_msg.button, htons(ptr_msg.x), htons(ptr_msg.y));
669 rfb_recv_cuttext_msg(struct rfb_softc *rc, int cfd)
671 struct rfb_cuttext_msg ct_msg;
672 unsigned char buf[32];
675 len = stream_read(cfd, ((void *)&ct_msg) + 1, sizeof(ct_msg) - 1);
676 ct_msg.length = htonl(ct_msg.length);
677 while (ct_msg.length > 0) {
678 len = stream_read(cfd, buf, ct_msg.length > sizeof(buf) ?
679 sizeof(buf) : ct_msg.length);
680 ct_msg.length -= len;
685 timeval_delta(struct timeval *prev, struct timeval *now)
688 n1 = now->tv_sec * 1000000 + now->tv_usec;
689 n2 = prev->tv_sec * 1000000 + prev->tv_usec;
694 rfb_wr_thr(void *arg)
696 struct rfb_softc *rc;
699 struct timeval prev_tv;
709 while (rc->cfd >= 0) {
715 err = select(cfd+1, &rfds, NULL, NULL, &tv);
719 /* Determine if its time to push screen; ~24hz */
720 gettimeofday(&tv, NULL);
721 tdiff = timeval_delta(&prev_tv, &tv);
723 prev_tv.tv_sec = tv.tv_sec;
724 prev_tv.tv_usec = tv.tv_usec;
725 if (rfb_send_screen(rc, cfd, 0) <= 0) {
730 usleep(40000 - tdiff);
738 rfb_handle(struct rfb_softc *rc, int cfd)
740 const char *vbuf = "RFB 003.008\n";
741 unsigned char buf[80];
748 /* 1a. Send server version */
749 stream_write(cfd, vbuf, strlen(vbuf));
751 /* 1b. Read client version */
752 len = read(cfd, buf, sizeof(buf));
754 /* 2a. Send security type 'none' */
756 buf[1] = 1; /* none */
757 stream_write(cfd, buf, 2);
760 /* 2b. Read agreed security type */
761 len = stream_read(cfd, buf, 1);
763 /* 2c. Write back a status of 0 */
765 stream_write(cfd, &sres, 4);
767 /* 3a. Read client shared-flag byte */
768 len = stream_read(cfd, buf, 1);
770 /* 4a. Write server-init info */
771 rfb_send_server_init_msg(cfd);
774 rc->zbuf = malloc(RFB_ZLIB_BUFSZ + 16);
775 assert(rc->zbuf != NULL);
778 rfb_send_screen(rc, cfd, 1);
780 pthread_create(&tid, NULL, rfb_wr_thr, rc);
781 pthread_set_name_np(tid, "rfbout");
783 /* Now read in client requests. 1st byte identifies type */
785 len = read(cfd, buf, 1);
787 DPRINTF(("rfb client exiting\r\n"));
793 rfb_recv_set_pixfmt_msg(rc, cfd);
796 rfb_recv_set_encodings_msg(rc, cfd);
799 rfb_recv_update_msg(rc, cfd, 1);
802 rfb_recv_key_msg(rc, cfd);
805 rfb_recv_ptr_msg(rc, cfd);
808 rfb_recv_cuttext_msg(rc, cfd);
811 WPRINTF(("rfb unknown cli-code %d!\n", buf[0] & 0xff));
817 pthread_join(tid, NULL);
819 deflateEnd(&rc->zstream);
825 struct rfb_softc *rc;
833 sigaddset(&set, SIGPIPE);
834 if (pthread_sigmask(SIG_BLOCK, &set, NULL) != 0) {
835 perror("pthread_sigmask");
840 rc->enc_raw_ok = false;
841 rc->enc_zlib_ok = false;
842 rc->enc_resize_ok = false;
844 cfd = accept(rc->sfd, NULL, NULL);
846 pthread_mutex_lock(&rc->mtx);
847 pthread_cond_signal(&rc->cond);
848 pthread_mutex_unlock(&rc->mtx);
860 sse42_supported(void)
862 u_int cpu_registers[4], ecx;
864 do_cpuid(1, cpu_registers);
866 ecx = cpu_registers[2];
868 return ((ecx & CPUID2_SSE42) != 0);
872 rfb_init(char *hostname, int port, int wait)
874 struct rfb_softc *rc;
875 struct sockaddr_in sin;
877 #ifndef WITHOUT_CAPSICUM
881 rc = calloc(1, sizeof(struct rfb_softc));
883 rc->crc = calloc(howmany(RFB_MAX_WIDTH * RFB_MAX_HEIGHT, 32),
885 rc->crc_tmp = calloc(howmany(RFB_MAX_WIDTH * RFB_MAX_HEIGHT, 32),
887 rc->crc_width = RFB_MAX_WIDTH;
888 rc->crc_height = RFB_MAX_HEIGHT;
890 rc->sfd = socket(AF_INET, SOCK_STREAM, 0);
896 setsockopt(rc->sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
898 sin.sin_len = sizeof(sin);
899 sin.sin_family = AF_INET;
900 sin.sin_port = htons(port);
901 if (hostname && strlen(hostname) > 0)
902 inet_pton(AF_INET, hostname, &(sin.sin_addr));
904 sin.sin_addr.s_addr = htonl(INADDR_ANY);
906 if (bind(rc->sfd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
911 if (listen(rc->sfd, 1) < 0) {
916 #ifndef WITHOUT_CAPSICUM
917 cap_rights_init(&rights, CAP_ACCEPT, CAP_EVENT, CAP_READ, CAP_WRITE);
918 if (cap_rights_limit(rc->sfd, &rights) == -1 && errno != ENOSYS)
919 errx(EX_OSERR, "Unable to apply rights for sandbox");
922 rc->hw_crc = sse42_supported();
924 rc->conn_wait = wait;
926 pthread_mutex_init(&rc->mtx, NULL);
927 pthread_cond_init(&rc->cond, NULL);
930 pthread_create(&rc->tid, NULL, rfb_thr, rc);
931 pthread_set_name_np(rc->tid, "rfb");
934 DPRINTF(("Waiting for rfb client...\n"));
935 pthread_mutex_lock(&rc->mtx);
936 pthread_cond_wait(&rc->cond, &rc->mtx);
937 pthread_mutex_unlock(&rc->mtx);