xref: /csrg-svn/sys/kern/uipc_socket2.c (revision 5578)
1 /*	uipc_socket2.c	4.19	82/01/19	*/
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 	if (m_reserve((cc*2)/MSIZE) == 0)
233 		return (0);
234 	sb->sb_hiwat = cc;
235 	sb->sb_mbmax = cc*2;
236 	return (1);
237 }
238 
239 /*
240  * Free mbufs held by a socket, and reserved mbuf space.
241  */
242 sbrelease(sb)
243 	struct sockbuf *sb;
244 {
245 
246 	sbflush(sb);
247 	m_release(sb->sb_mbmax/MSIZE);
248 	sb->sb_hiwat = sb->sb_mbmax = 0;
249 }
250 
251 /*
252  * Routines to add (at the end) and remove (from the beginning)
253  * data from a mbuf queue.
254  */
255 
256 /*
257  * Append mbuf queue m to sockbuf sb.
258  */
259 sbappend(sb, m)
260 	register struct mbuf *m;
261 	register struct sockbuf *sb;
262 {
263 	register struct mbuf **np, *n;
264 
265 	np = &sb->sb_mb;
266 	n = 0;
267 	while (*np) {
268 		n = *np;
269 		np = &n->m_next;
270 	}
271 	while (m) {
272 		if (m->m_len == 0 && (int)m->m_act == 0) {
273 			m = m_free(m);
274 			continue;
275 		}
276 		if (n && n->m_off <= MMAXOFF && m->m_off <= MMAXOFF &&
277 		   (int)n->m_act == 0 && (int)m->m_act == 0 &&
278 		   (n->m_off + n->m_len + m->m_len) <= MMAXOFF) {
279 			bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
280 			    (unsigned)m->m_len);
281 			n->m_len += m->m_len;
282 			sb->sb_cc += m->m_len;
283 			m = m_free(m);
284 			continue;
285 		}
286 		sballoc(sb, m);
287 		*np = m;
288 		n = m;
289 		np = &n->m_next;
290 		m = m->m_next;
291 	}
292 }
293 
294 /*
295  * Append data and address.
296  * Return 0 if no space in sockbuf or if
297  * can't get mbuf to stuff address in.
298  */
299 sbappendaddr(sb, asa, m0)
300 	struct sockbuf *sb;
301 	struct sockaddr *asa;
302 	struct mbuf *m0;
303 {
304 	struct sockaddr *msa;
305 	register struct mbuf *m;
306 	register int len = sizeof (struct sockaddr);
307 
308 	m = m0;
309 	if (m == 0)
310 		panic("sbappendaddr");
311 	for (;;) {
312 		len += m->m_len;
313 		if (m->m_next == 0) {
314 			m->m_act = (struct mbuf *)1;
315 			break;
316 		}
317 		m = m->m_next;
318 	}
319 	if (len > sbspace(sb))
320 		return (0);
321 	m = m_get(0);
322 	if (m == 0)
323 		return (0);
324 	m->m_off = MMINOFF;
325 	m->m_len = sizeof (struct sockaddr);
326 	msa = mtod(m, struct sockaddr *);
327 	*msa = *asa;
328 	m->m_act = (struct mbuf *)1;
329 	sbappend(sb, m);
330 	sbappend(sb, m0);
331 	return (1);
332 }
333 
334 /*
335  * Free all mbufs on a sockbuf mbuf chain.
336  * Check that resource allocations return to 0.
337  */
338 sbflush(sb)
339 	struct sockbuf *sb;
340 {
341 
342 	if (sb->sb_flags & SB_LOCK)
343 		panic("sbflush");
344 	if (sb->sb_cc)
345 		sbdrop(sb, sb->sb_cc);
346 	if (sb->sb_cc || sb->sb_mbcnt || sb->sb_mb)
347 		panic("sbflush 2");
348 }
349 
350 /*
351  * Drop data from (the front of) a sockbuf chain.
352  */
353 sbdrop(sb, len)
354 	register struct sockbuf *sb;
355 	register int len;
356 {
357 	register struct mbuf *m = sb->sb_mb, *mn;
358 
359 	while (len > 0) {
360 		if (m == 0)
361 			panic("sbdrop");
362 		if (m->m_len > len) {
363 			m->m_len -= len;
364 			m->m_off += len;
365 			sb->sb_cc -= len;
366 			break;
367 		}
368 		len -= m->m_len;
369 		sbfree(sb, m);
370 		MFREE(m, mn);
371 		m = mn;
372 	}
373 	sb->sb_mb = m;
374 }
375 
376 /*
377 printm(m)
378 	struct mbuf *m;
379 {
380 
381 	printf("<");
382 	while (m) {
383 		printf("%d,", m->m_len);
384 		m = m->m_next;
385 	}
386 	printf(">");
387 	printf("\n");
388 }
389 */
390