blob: 10119d3ad590cc1ea0f0b46b017800c24b6edf8c [file] [log] [blame]
bellardf0cbd3e2004-04-22 00:10:48 +00001/*
2 * Copyright (c) 1995 Danny Gasparovski.
ths5fafdf22007-09-16 21:08:06 +00003 *
4 * Please read the file COPYRIGHT for the
bellardf0cbd3e2004-04-22 00:10:48 +00005 * terms and conditions of the copyright.
6 */
7
Peter Maydell7df74822016-01-29 17:49:59 +00008#include "qemu/osdep.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +02009#include "slirp.h"
10#include "qemu/main-loop.h"
bellardf0cbd3e2004-04-22 00:10:48 +000011
blueswir19634d902007-10-26 19:01:16 +000012static void sbappendsb(struct sbuf *sb, struct mbuf *m);
13
bellardf0cbd3e2004-04-22 00:10:48 +000014void
blueswir1511d2b12009-03-07 15:32:56 +000015sbfree(struct sbuf *sb)
bellardf0cbd3e2004-04-22 00:10:48 +000016{
17 free(sb->sb_data);
18}
19
20void
blueswir1511d2b12009-03-07 15:32:56 +000021sbdrop(struct sbuf *sb, int num)
bellardf0cbd3e2004-04-22 00:10:48 +000022{
Jan Kiszka86073012012-03-30 19:29:08 +020023 int limit = sb->sb_datalen / 2;
24
ths5fafdf22007-09-16 21:08:06 +000025 /*
bellardf0cbd3e2004-04-22 00:10:48 +000026 * We can only drop how much we have
ths5fafdf22007-09-16 21:08:06 +000027 * This should never succeed
bellardf0cbd3e2004-04-22 00:10:48 +000028 */
29 if(num > sb->sb_cc)
30 num = sb->sb_cc;
31 sb->sb_cc -= num;
32 sb->sb_rptr += num;
33 if(sb->sb_rptr >= sb->sb_data + sb->sb_datalen)
34 sb->sb_rptr -= sb->sb_datalen;
ths3b46e622007-09-17 08:09:54 +000035
Jan Kiszka86073012012-03-30 19:29:08 +020036 if (sb->sb_cc < limit && sb->sb_cc + num >= limit) {
37 qemu_notify_event();
38 }
bellardf0cbd3e2004-04-22 00:10:48 +000039}
40
41void
blueswir1511d2b12009-03-07 15:32:56 +000042sbreserve(struct sbuf *sb, int size)
bellardf0cbd3e2004-04-22 00:10:48 +000043{
44 if (sb->sb_data) {
45 /* Already alloced, realloc if necessary */
46 if (sb->sb_datalen != size) {
47 sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)realloc(sb->sb_data, size);
48 sb->sb_cc = 0;
49 if (sb->sb_wptr)
50 sb->sb_datalen = size;
51 else
52 sb->sb_datalen = 0;
53 }
54 } else {
55 sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)malloc(size);
56 sb->sb_cc = 0;
57 if (sb->sb_wptr)
58 sb->sb_datalen = size;
59 else
60 sb->sb_datalen = 0;
61 }
62}
63
64/*
65 * Try and write() to the socket, whatever doesn't get written
66 * append to the buffer... for a host with a fast net connection,
67 * this prevents an unnecessary copy of the data
68 * (the socket is non-blocking, so we won't hang)
69 */
70void
blueswir1511d2b12009-03-07 15:32:56 +000071sbappend(struct socket *so, struct mbuf *m)
bellardf0cbd3e2004-04-22 00:10:48 +000072{
73 int ret = 0;
ths5fafdf22007-09-16 21:08:06 +000074
bellardf0cbd3e2004-04-22 00:10:48 +000075 DEBUG_CALL("sbappend");
Stefan Weilecc804c2015-08-29 09:12:35 +020076 DEBUG_ARG("so = %p", so);
77 DEBUG_ARG("m = %p", m);
bellardf0cbd3e2004-04-22 00:10:48 +000078 DEBUG_ARG("m->m_len = %d", m->m_len);
ths5fafdf22007-09-16 21:08:06 +000079
bellardf0cbd3e2004-04-22 00:10:48 +000080 /* Shouldn't happen, but... e.g. foreign host closes connection */
81 if (m->m_len <= 0) {
82 m_free(m);
83 return;
84 }
ths5fafdf22007-09-16 21:08:06 +000085
bellardf0cbd3e2004-04-22 00:10:48 +000086 /*
87 * If there is urgent data, call sosendoob
88 * if not all was sent, sowrite will take care of the rest
89 * (The rest of this function is just an optimisation)
90 */
91 if (so->so_urgc) {
92 sbappendsb(&so->so_rcv, m);
93 m_free(m);
94 sosendoob(so);
95 return;
96 }
ths5fafdf22007-09-16 21:08:06 +000097
bellardf0cbd3e2004-04-22 00:10:48 +000098 /*
99 * We only write if there's nothing in the buffer,
100 * ottherwise it'll arrive out of order, and hence corrupt
101 */
102 if (!so->so_rcv.sb_cc)
aliguorie1c5a2b2009-01-08 19:18:21 +0000103 ret = slirp_send(so, m->m_data, m->m_len, 0);
ths5fafdf22007-09-16 21:08:06 +0000104
bellardf0cbd3e2004-04-22 00:10:48 +0000105 if (ret <= 0) {
ths5fafdf22007-09-16 21:08:06 +0000106 /*
bellardf0cbd3e2004-04-22 00:10:48 +0000107 * Nothing was written
108 * It's possible that the socket has closed, but
109 * we don't need to check because if it has closed,
110 * it will be detected in the normal way by soread()
111 */
112 sbappendsb(&so->so_rcv, m);
113 } else if (ret != m->m_len) {
114 /*
115 * Something was written, but not everything..
116 * sbappendsb the rest
117 */
118 m->m_len -= ret;
119 m->m_data += ret;
120 sbappendsb(&so->so_rcv, m);
121 } /* else */
122 /* Whatever happened, we free the mbuf */
123 m_free(m);
124}
125
126/*
127 * Copy the data from m into sb
128 * The caller is responsible to make sure there's enough room
129 */
blueswir19634d902007-10-26 19:01:16 +0000130static void
131sbappendsb(struct sbuf *sb, struct mbuf *m)
bellardf0cbd3e2004-04-22 00:10:48 +0000132{
133 int len, n, nn;
ths5fafdf22007-09-16 21:08:06 +0000134
bellardf0cbd3e2004-04-22 00:10:48 +0000135 len = m->m_len;
136
137 if (sb->sb_wptr < sb->sb_rptr) {
138 n = sb->sb_rptr - sb->sb_wptr;
139 if (n > len) n = len;
140 memcpy(sb->sb_wptr, m->m_data, n);
141 } else {
142 /* Do the right edge first */
143 n = sb->sb_data + sb->sb_datalen - sb->sb_wptr;
144 if (n > len) n = len;
145 memcpy(sb->sb_wptr, m->m_data, n);
146 len -= n;
147 if (len) {
148 /* Now the left edge */
149 nn = sb->sb_rptr - sb->sb_data;
150 if (nn > len) nn = len;
151 memcpy(sb->sb_data,m->m_data+n,nn);
152 n += nn;
153 }
154 }
155
156 sb->sb_cc += n;
157 sb->sb_wptr += n;
158 if (sb->sb_wptr >= sb->sb_data + sb->sb_datalen)
159 sb->sb_wptr -= sb->sb_datalen;
160}
161
162/*
163 * Copy data from sbuf to a normal, straight buffer
164 * Don't update the sbuf rptr, this will be
165 * done in sbdrop when the data is acked
166 */
167void
blueswir1511d2b12009-03-07 15:32:56 +0000168sbcopy(struct sbuf *sb, int off, int len, char *to)
bellardf0cbd3e2004-04-22 00:10:48 +0000169{
170 char *from;
ths5fafdf22007-09-16 21:08:06 +0000171
bellardf0cbd3e2004-04-22 00:10:48 +0000172 from = sb->sb_rptr + off;
173 if (from >= sb->sb_data + sb->sb_datalen)
174 from -= sb->sb_datalen;
175
176 if (from < sb->sb_wptr) {
177 if (len > sb->sb_cc) len = sb->sb_cc;
178 memcpy(to,from,len);
179 } else {
180 /* re-use off */
181 off = (sb->sb_data + sb->sb_datalen) - from;
182 if (off > len) off = len;
183 memcpy(to,from,off);
184 len -= off;
185 if (len)
186 memcpy(to+off,sb->sb_data,len);
187 }
188}