xref: /csrg-svn/sys/kern/uipc_socket2.c (revision 7181)
1 /*	uipc_socket2.c	4.23	82/06/14	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/dir.h"
6 #include "../h/user.h"
7 #include "../h/proc.h"
8 #include "../h/file.h"
9 #include "../h/inode.h"
10 #include "../h/buf.h"
11 #include "../h/mbuf.h"
12 #include "../h/protosw.h"
13 #include "../h/socket.h"
14 #include "../h/socketvar.h"
15 #include "../net/in.h"
16 #include "../net/in_systm.h"
17 
18 /*
19  * Primitive routines for operating on sockets and socket buffers
20  */
21 
22 /*
23  * Procedures to manipulate state flags of socket
24  * and do appropriate wakeups.  Normal sequence is that
25  * soisconnecting() is called during processing of connect() call,
26  * resulting in an eventual call to soisconnected() if/when the
27  * connection is established.  When the connection is torn down
28  * soisdisconnecting() is called during processing of disconnect() call,
29  * and soisdisconnected() is called when the connection to the peer
30  * is totally severed.  The semantics of these routines are such that
31  * connectionless protocols can call soisconnected() and soisdisconnected()
32  * only, bypassing the in-progress calls when setting up a ``connection''
33  * takes no time.
34  *
35  * When higher level protocols are implemented in
36  * the kernel, the wakeups done here will sometimes
37  * be implemented as software-interrupt process scheduling.
38  */
39 
40 soisconnecting(so)
41 	struct socket *so;
42 {
43 
44 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
45 	so->so_state |= SS_ISCONNECTING;
46 	wakeup((caddr_t)&so->so_timeo);
47 }
48 
49 soisconnected(so)
50 	struct socket *so;
51 {
52 
53 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING);
54 	so->so_state |= SS_ISCONNECTED;
55 	wakeup((caddr_t)&so->so_timeo);
56 	sorwakeup(so);
57 	sowwakeup(so);
58 }
59 
60 soisdisconnecting(so)
61 	struct socket *so;
62 {
63 
64 	so->so_state &= ~SS_ISCONNECTING;
65 	so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
66 	wakeup((caddr_t)&so->so_timeo);
67 	sowwakeup(so);
68 	sorwakeup(so);
69 }
70 
71 soisdisconnected(so)
72 	struct socket *so;
73 {
74 
75 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
76 	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE);
77 	wakeup((caddr_t)&so->so_timeo);
78 	sowwakeup(so);
79 	sorwakeup(so);
80 }
81 
82 /*
83  * Socantsendmore indicates that no more data will be sent on the
84  * socket; it would normally be applied to a socket when the user
85  * informs the system that no more data is to be sent, by the protocol
86  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
87  * will be received, and will normally be applied to the socket by a
88  * protocol when it detects that the peer will send no more data.
89  * Data queued for reading in the socket may yet be read.
90  */
91 
92 socantsendmore(so)
93 	struct socket *so;
94 {
95 
96 	so->so_state |= SS_CANTSENDMORE;
97 	sowwakeup(so);
98 }
99 
100 socantrcvmore(so)
101 	struct socket *so;
102 {
103 
104 	so->so_state |= SS_CANTRCVMORE;
105 	sorwakeup(so);
106 }
107 
108 /*
109  * Socket select/wakeup routines.
110  */
111 
112 /*
113  * Interface routine to select() system
114  * call for sockets.
115  */
116 soselect(so, rw)
117 	register struct socket *so;
118 	int rw;
119 {
120 	int s = splnet();
121 
122 	switch (rw) {
123 
124 	case FREAD:
125 		if (soreadable(so)) {
126 			splx(s);
127 			return (1);
128 		}
129 		sbselqueue(&so->so_rcv);
130 		break;
131 
132 	case FWRITE:
133 		if (sowriteable(so)) {
134 			splx(s);
135 			return (1);
136 		}
137 		sbselqueue(&so->so_snd);
138 		break;
139 	}
140 	splx(s);
141 	return (0);
142 }
143 
144 /*
145  * Queue a process for a select on a socket buffer.
146  */
147 sbselqueue(sb)
148 	struct sockbuf *sb;
149 {
150 	register struct proc *p;
151 
152 	if ((p = sb->sb_sel) && p->p_wchan == (caddr_t)&selwait)
153 		sb->sb_flags |= SB_COLL;
154 	else
155 		sb->sb_sel = u.u_procp;
156 }
157 
158 /*
159  * Wait for data to arrive at/drain from a socket buffer.
160  */
161 sbwait(sb)
162 	struct sockbuf *sb;
163 {
164 
165 	sb->sb_flags |= SB_WAIT;
166 	sleep((caddr_t)&sb->sb_cc, PZERO+1);
167 }
168 
169 /*
170  * Wakeup processes waiting on a socket buffer.
171  */
172 sbwakeup(sb)
173 	struct sockbuf *sb;
174 {
175 
176 	if (sb->sb_sel) {
177 		selwakeup(sb->sb_sel, sb->sb_flags & SB_COLL);
178 		sb->sb_sel = 0;
179 		sb->sb_flags &= ~SB_COLL;
180 	}
181 	if (sb->sb_flags & SB_WAIT) {
182 		sb->sb_flags &= ~SB_WAIT;
183 		wakeup((caddr_t)&sb->sb_cc);
184 	}
185 }
186 
187 /*
188  * Socket buffer (struct sockbuf) utility routines.
189  *
190  * Each socket contains two socket buffers: one for sending data and
191  * one for receiving data.  Each buffer contains a queue of mbufs,
192  * information about the number of mbufs and amount of data in the
193  * queue, and other fields allowing select() statements and notification
194  * on data availability to be implemented.
195  *
196  * Before using a new socket structure it is first necessary to reserve
197  * buffer space to the socket, by calling sbreserve.  This commits
198  * some of the available buffer space in the system buffer pool for the
199  * socket.  The space should be released by calling sbrelease when the
200  * socket is destroyed.
201  *
202  * The routine sbappend() is normally called to append new mbufs
203  * to a socket buffer, after checking that adequate space is available
204  * comparing the function spspace() with the amount of data to be added.
205  * Data is normally removed from a socket buffer in a protocol by
206  * first calling m_copy on the socket buffer mbuf chain and sending this
207  * to a peer, and then removing the data from the socket buffer with
208  * sbdrop when the data is acknowledged by the peer (or immediately
209  * in the case of unreliable protocols.)
210  *
211  * Protocols which do not require connections place both source address
212  * and data information in socket buffer queues.  The source addresses
213  * are stored in single mbufs after each data item, and are easily found
214  * as the data items are all marked with end of record markers.  The
215  * sbappendaddr() routine stores a datum and associated address in
216  * a socket buffer.  Note that, unlike sbappend(), this routine checks
217  * for the caller that there will be enough space to store the data.
218  * It fails if there is not enough space, or if it cannot find
219  * a mbuf to store the address in.
220  *
221  * The higher-level routines sosend and soreceive (in socket.c)
222  * also add data to, and remove data from socket buffers repectively.
223  */
224 
225 /*
226  * Allot mbufs to a sockbuf.
227  */
228 sbreserve(sb, cc)
229 	struct sockbuf *sb;
230 {
231 
232 	/* someday maybe this routine will fail... */
233 	sb->sb_hiwat = cc;
234 	sb->sb_mbmax = cc*2;
235 	return (1);
236 }
237 
238 /*
239  * Free mbufs held by a socket, and reserved mbuf space.
240  */
241 sbrelease(sb)
242 	struct sockbuf *sb;
243 {
244 
245 	sbflush(sb);
246 	sb->sb_hiwat = sb->sb_mbmax = 0;
247 }
248 
249 /*
250  * Routines to add (at the end) and remove (from the beginning)
251  * data from a mbuf queue.
252  */
253 
254 /*
255  * Append mbuf queue m to sockbuf sb.
256  */
257 sbappend(sb, m)
258 	register struct mbuf *m;
259 	register struct sockbuf *sb;
260 {
261 	register struct mbuf *n;
262 
263 	n = sb->sb_mb;
264 	if (n)
265 		while (n->m_next)
266 			n = n->m_next;
267 	while (m) {
268 		if (m->m_len == 0 && (int)m->m_act == 0) {
269 			m = m_free(m);
270 			continue;
271 		}
272 		if (n && n->m_off <= MMAXOFF && m->m_off <= MMAXOFF &&
273 		   (int)n->m_act == 0 && (int)m->m_act == 0 &&
274 		   (n->m_off + n->m_len + m->m_len) <= MMAXOFF) {
275 			bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
276 			    (unsigned)m->m_len);
277 			n->m_len += m->m_len;
278 			sb->sb_cc += m->m_len;
279 			m = m_free(m);
280 			continue;
281 		}
282 		sballoc(sb, m);
283 		if (n == 0)
284 			sb->sb_mb = m;
285 		else
286 			n->m_next = m;
287 		n = m;
288 		m = m->m_next;
289 		n->m_next = 0;
290 	}
291 }
292 
293 /*
294  * Append data and address.
295  * Return 0 if no space in sockbuf or if
296  * can't get mbuf to stuff address in.
297  */
298 sbappendaddr(sb, asa, m0)
299 	struct sockbuf *sb;
300 	struct sockaddr *asa;
301 	struct mbuf *m0;
302 {
303 	struct sockaddr *msa;
304 	register struct mbuf *m;
305 	register int len = sizeof (struct sockaddr);
306 
307 	m = m0;
308 	if (m == 0)
309 		panic("sbappendaddr");
310 	for (;;) {
311 		len += m->m_len;
312 		if (m->m_next == 0) {
313 			m->m_act = (struct mbuf *)1;
314 			break;
315 		}
316 		m = m->m_next;
317 	}
318 	if (len > sbspace(sb))
319 		return (0);
320 	m = m_get(M_DONTWAIT);
321 	if (m == 0)
322 		return (0);
323 	m->m_off = MMINOFF;
324 	m->m_len = sizeof (struct sockaddr);
325 	msa = mtod(m, struct sockaddr *);
326 	*msa = *asa;
327 	m->m_act = (struct mbuf *)1;
328 	sbappend(sb, m);
329 	sbappend(sb, m0);
330 	return (1);
331 }
332 
333 /*
334  * Free all mbufs on a sockbuf mbuf chain.
335  * Check that resource allocations return to 0.
336  */
337 sbflush(sb)
338 	struct sockbuf *sb;
339 {
340 
341 	if (sb->sb_flags & SB_LOCK)
342 		panic("sbflush");
343 	if (sb->sb_cc)
344 		sbdrop(sb, sb->sb_cc);
345 	if (sb->sb_cc || sb->sb_mbcnt || sb->sb_mb)
346 		panic("sbflush 2");
347 }
348 
349 /*
350  * Drop data from (the front of) a sockbuf chain.
351  */
352 sbdrop(sb, len)
353 	register struct sockbuf *sb;
354 	register int len;
355 {
356 	register struct mbuf *m = sb->sb_mb, *mn;
357 
358 	while (len > 0) {
359 		if (m == 0)
360 			panic("sbdrop");
361 		if (m->m_len > len) {
362 			m->m_len -= len;
363 			m->m_off += len;
364 			sb->sb_cc -= len;
365 			break;
366 		}
367 		len -= m->m_len;
368 		sbfree(sb, m);
369 		MFREE(m, mn);
370 		m = mn;
371 	}
372 	sb->sb_mb = m;
373 }
374