from shannon
[unix-history] / usr / src / sys / kern / uipc_socket2.c
CommitLineData
c20f7455 1/* uipc_socket2.c 4.35 83/01/13 */
681ebb17
BJ
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"
681ebb17
BJ
12#include "../h/protosw.h"
13#include "../h/socket.h"
14#include "../h/socketvar.h"
681ebb17
BJ
15
16/*
17 * Primitive routines for operating on sockets and socket buffers
18 */
19
20/*
21 * Procedures to manipulate state flags of socket
2deddea9
BJ
22 * and do appropriate wakeups. Normal sequence from the
23 * active (originating) side is that soisconnecting() is
24 * called during processing of connect() call,
4c078bb2
BJ
25 * resulting in an eventual call to soisconnected() if/when the
26 * connection is established. When the connection is torn down
27 * soisdisconnecting() is called during processing of disconnect() call,
28 * and soisdisconnected() is called when the connection to the peer
29 * is totally severed. The semantics of these routines are such that
30 * connectionless protocols can call soisconnected() and soisdisconnected()
31 * only, bypassing the in-progress calls when setting up a ``connection''
32 * takes no time.
33 *
2deddea9
BJ
34 * From the passive side, a socket is created with SO_ACCEPTCONN
35 * creating two queues of sockets: so_q0 for connections in progress
36 * and so_q for connections already made and awaiting user acceptance.
37 * As a protocol is preparing incoming connections, it creates a socket
38 * structure queued on so_q0 by calling sonewconn(). When the connection
39 * is established, soisconnected() is called, and transfers the
40 * socket structure to so_q, making it available to accept().
41 *
42 * If a SO_ACCEPTCONN socket is closed with sockets on either
43 * so_q0 or so_q, these sockets are dropped.
44 *
45 * If and when higher level protocols are implemented in
4c078bb2
BJ
46 * the kernel, the wakeups done here will sometimes
47 * be implemented as software-interrupt process scheduling.
681ebb17 48 */
4c078bb2 49
681ebb17
BJ
50soisconnecting(so)
51 struct socket *so;
52{
53
54 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
55 so->so_state |= SS_ISCONNECTING;
56 wakeup((caddr_t)&so->so_timeo);
57}
58
59soisconnected(so)
60 struct socket *so;
61{
2deddea9 62 register struct socket *head = so->so_head;
681ebb17 63
2deddea9
BJ
64 if (head) {
65 if (soqremque(so, 0) == 0)
66 panic("soisconnected");
67 soqinsque(head, so, 1);
68 wakeup((caddr_t)&head->so_timeo);
69 }
681ebb17
BJ
70 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING);
71 so->so_state |= SS_ISCONNECTED;
72 wakeup((caddr_t)&so->so_timeo);
f957a49a
BJ
73 sorwakeup(so);
74 sowwakeup(so);
681ebb17
BJ
75}
76
77soisdisconnecting(so)
78 struct socket *so;
79{
80
72857acf 81 so->so_state &= ~SS_ISCONNECTING;
681ebb17
BJ
82 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
83 wakeup((caddr_t)&so->so_timeo);
4c078bb2 84 sowwakeup(so);
b454c3ea 85 sorwakeup(so);
681ebb17
BJ
86}
87
88soisdisconnected(so)
89 struct socket *so;
90{
91
92 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
93 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE);
94 wakeup((caddr_t)&so->so_timeo);
95 sowwakeup(so);
96 sorwakeup(so);
97}
98
2deddea9
BJ
99/*
100 * When an attempt at a new connection is noted on a socket
101 * which accepts connections, sonewconn is called. If the
102 * connection is possible (subject to space constraints, etc.)
103 * then we allocate a new structure, propoerly linked into the
104 * data structure of the original socket, and return this.
105 */
106struct socket *
107sonewconn(head)
108 register struct socket *head;
109{
110 register struct socket *so;
111 struct mbuf *m;
112
113 if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
114 goto bad;
cce93e4b 115 m = m_getclr(M_DONTWAIT, MT_SOCKET);
5fe6f9d1 116 if (m == NULL)
2deddea9
BJ
117 goto bad;
118 so = mtod(m, struct socket *);
119 so->so_type = head->so_type;
120 so->so_options = head->so_options &~ SO_ACCEPTCONN;
121 so->so_linger = head->so_linger;
f7428e88 122 so->so_state = head->so_state | SS_NOFDREF;
2deddea9
BJ
123 so->so_proto = head->so_proto;
124 so->so_timeo = head->so_timeo;
125 so->so_pgrp = head->so_pgrp;
126 soqinsque(head, so, 0);
5fe6f9d1 127 if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0,
c20f7455 128 (struct mbuf *)0)) {
2deddea9 129 (void) soqremque(so, 0);
30c36259 130 (void) m_free(m);
2deddea9
BJ
131 goto bad;
132 }
133 return (so);
134bad:
135 return ((struct socket *)0);
136}
137
138soqinsque(head, so, q)
139 register struct socket *head, *so;
140 int q;
141{
142
143 so->so_head = head;
144 if (q == 0) {
145 head->so_q0len++;
146 so->so_q0 = head->so_q0;
147 head->so_q0 = so;
148 } else {
149 head->so_qlen++;
150 so->so_q = head->so_q;
151 head->so_q = so;
152 }
153}
154
155soqremque(so, q)
156 register struct socket *so;
157 int q;
158{
159 register struct socket *head, *prev, *next;
160
161 head = so->so_head;
162 prev = head;
163 for (;;) {
164 next = q ? prev->so_q : prev->so_q0;
165 if (next == so)
166 break;
167 if (next == head)
168 return (0);
169 prev = next;
170 }
171 if (q == 0) {
172 prev->so_q0 = next->so_q0;
173 head->so_q0len--;
174 } else {
175 prev->so_q = next->so_q;
176 head->so_qlen--;
177 }
178 next->so_q0 = next->so_q = 0;
179 next->so_head = 0;
180 return (1);
181}
182
4c078bb2
BJ
183/*
184 * Socantsendmore indicates that no more data will be sent on the
185 * socket; it would normally be applied to a socket when the user
186 * informs the system that no more data is to be sent, by the protocol
187 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
188 * will be received, and will normally be applied to the socket by a
189 * protocol when it detects that the peer will send no more data.
190 * Data queued for reading in the socket may yet be read.
191 */
192
ae921915
BJ
193socantsendmore(so)
194 struct socket *so;
195{
196
197 so->so_state |= SS_CANTSENDMORE;
198 sowwakeup(so);
199}
200
201socantrcvmore(so)
202 struct socket *so;
203{
204
205 so->so_state |= SS_CANTRCVMORE;
206 sorwakeup(so);
207}
208
681ebb17 209/*
4c078bb2
BJ
210 * Socket select/wakeup routines.
211 */
212
213/*
214 * Interface routine to select() system
215 * call for sockets.
681ebb17 216 */
477b2112 217soselect(so, rw)
681ebb17 218 register struct socket *so;
477b2112 219 int rw;
681ebb17 220{
f957a49a 221 int s = splnet();
681ebb17 222
477b2112
BJ
223 switch (rw) {
224
225 case FREAD:
f957a49a
BJ
226 if (soreadable(so)) {
227 splx(s);
681ebb17 228 return (1);
f957a49a 229 }
681ebb17 230 sbselqueue(&so->so_rcv);
477b2112
BJ
231 break;
232
233 case FWRITE:
f957a49a
BJ
234 if (sowriteable(so)) {
235 splx(s);
681ebb17 236 return (1);
f957a49a 237 }
681ebb17 238 sbselqueue(&so->so_snd);
477b2112 239 break;
681ebb17 240 }
f957a49a 241 splx(s);
681ebb17
BJ
242 return (0);
243}
244
245/*
246 * Queue a process for a select on a socket buffer.
247 */
248sbselqueue(sb)
249 struct sockbuf *sb;
250{
251 register struct proc *p;
252
ae921915 253 if ((p = sb->sb_sel) && p->p_wchan == (caddr_t)&selwait)
681ebb17
BJ
254 sb->sb_flags |= SB_COLL;
255 else
256 sb->sb_sel = u.u_procp;
257}
258
ae921915
BJ
259/*
260 * Wait for data to arrive at/drain from a socket buffer.
261 */
262sbwait(sb)
263 struct sockbuf *sb;
264{
265
266 sb->sb_flags |= SB_WAIT;
267 sleep((caddr_t)&sb->sb_cc, PZERO+1);
268}
269
681ebb17
BJ
270/*
271 * Wakeup processes waiting on a socket buffer.
272 */
273sbwakeup(sb)
274 struct sockbuf *sb;
275{
276
277 if (sb->sb_sel) {
278 selwakeup(sb->sb_sel, sb->sb_flags & SB_COLL);
279 sb->sb_sel = 0;
280 sb->sb_flags &= ~SB_COLL;
281 }
282 if (sb->sb_flags & SB_WAIT) {
283 sb->sb_flags &= ~SB_WAIT;
388ca8bd 284 wakeup((caddr_t)&sb->sb_cc);
681ebb17
BJ
285 }
286}
287
4c078bb2
BJ
288/*
289 * Socket buffer (struct sockbuf) utility routines.
290 *
291 * Each socket contains two socket buffers: one for sending data and
292 * one for receiving data. Each buffer contains a queue of mbufs,
293 * information about the number of mbufs and amount of data in the
294 * queue, and other fields allowing select() statements and notification
295 * on data availability to be implemented.
296 *
297 * Before using a new socket structure it is first necessary to reserve
298 * buffer space to the socket, by calling sbreserve. This commits
299 * some of the available buffer space in the system buffer pool for the
300 * socket. The space should be released by calling sbrelease when the
301 * socket is destroyed.
302 *
303 * The routine sbappend() is normally called to append new mbufs
304 * to a socket buffer, after checking that adequate space is available
305 * comparing the function spspace() with the amount of data to be added.
306 * Data is normally removed from a socket buffer in a protocol by
307 * first calling m_copy on the socket buffer mbuf chain and sending this
308 * to a peer, and then removing the data from the socket buffer with
309 * sbdrop when the data is acknowledged by the peer (or immediately
b454c3ea 310 * in the case of unreliable protocols.)
4c078bb2
BJ
311 *
312 * Protocols which do not require connections place both source address
313 * and data information in socket buffer queues. The source addresses
314 * are stored in single mbufs after each data item, and are easily found
315 * as the data items are all marked with end of record markers. The
316 * sbappendaddr() routine stores a datum and associated address in
317 * a socket buffer. Note that, unlike sbappend(), this routine checks
318 * for the caller that there will be enough space to store the data.
319 * It fails if there is not enough space, or if it cannot find
320 * a mbuf to store the address in.
321 *
322 * The higher-level routines sosend and soreceive (in socket.c)
b454c3ea 323 * also add data to, and remove data from socket buffers repectively.
4c078bb2
BJ
324 */
325
0e18ec4a
BJ
326soreserve(so, sndcc, rcvcc)
327 struct socket *so;
328 int sndcc, rcvcc;
329{
330
331 if (sbreserve(&so->so_snd, sndcc) == 0)
332 goto bad;
333 if (sbreserve(&so->so_rcv, rcvcc) == 0)
334 goto bad2;
335 return (0);
336bad2:
337 sbrelease(&so->so_snd);
338bad:
339 return (ENOBUFS);
340}
341
681ebb17
BJ
342/*
343 * Allot mbufs to a sockbuf.
344 */
345sbreserve(sb, cc)
346 struct sockbuf *sb;
347{
348
de48daf3 349 /* someday maybe this routine will fail... */
d028a086 350 sb->sb_hiwat = cc;
5fe6f9d1 351 /* the 2 implies names can be no more than 1 mbuf each */
76a6e254 352 sb->sb_mbmax = cc*2;
ae921915 353 return (1);
681ebb17
BJ
354}
355
356/*
357 * Free mbufs held by a socket, and reserved mbuf space.
358 */
359sbrelease(sb)
360 struct sockbuf *sb;
361{
362
363 sbflush(sb);
d028a086 364 sb->sb_hiwat = sb->sb_mbmax = 0;
681ebb17
BJ
365}
366
367/*
368 * Routines to add (at the end) and remove (from the beginning)
369 * data from a mbuf queue.
370 */
371
372/*
373 * Append mbuf queue m to sockbuf sb.
374 */
375sbappend(sb, m)
376 register struct mbuf *m;
377 register struct sockbuf *sb;
378{
e495e1cc 379 register struct mbuf *n;
681ebb17 380
e495e1cc
BJ
381 n = sb->sb_mb;
382 if (n)
383 while (n->m_next)
384 n = n->m_next;
681ebb17 385 while (m) {
a73ab5ae 386 if (m->m_len == 0 && (int)m->m_act == 0) {
c64d826c 387 m = m_free(m);
a73ab5ae
BJ
388 continue;
389 }
681ebb17
BJ
390 if (n && n->m_off <= MMAXOFF && m->m_off <= MMAXOFF &&
391 (int)n->m_act == 0 && (int)m->m_act == 0 &&
76a6e254
BJ
392 (n->m_off + n->m_len + m->m_len) <= MMAXOFF) {
393 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
ae921915 394 (unsigned)m->m_len);
681ebb17
BJ
395 n->m_len += m->m_len;
396 sb->sb_cc += m->m_len;
397 m = m_free(m);
398 continue;
399 }
400 sballoc(sb, m);
e495e1cc
BJ
401 if (n == 0)
402 sb->sb_mb = m;
403 else
404 n->m_next = m;
681ebb17 405 n = m;
681ebb17 406 m = m->m_next;
e495e1cc 407 n->m_next = 0;
681ebb17
BJ
408 }
409}
410
4c078bb2
BJ
411/*
412 * Append data and address.
413 * Return 0 if no space in sockbuf or if
414 * can't get mbuf to stuff address in.
415 */
2b4b57cd
BJ
416sbappendaddr(sb, asa, m0)
417 struct sockbuf *sb;
418 struct sockaddr *asa;
419 struct mbuf *m0;
420{
421 struct sockaddr *msa;
422 register struct mbuf *m;
423 register int len = sizeof (struct sockaddr);
424
76a6e254
BJ
425 m = m0;
426 if (m == 0)
427 panic("sbappendaddr");
428 for (;;) {
2b4b57cd 429 len += m->m_len;
76a6e254
BJ
430 if (m->m_next == 0) {
431 m->m_act = (struct mbuf *)1;
432 break;
433 }
434 m = m->m_next;
435 }
509e40dd 436 if (len > sbspace(sb))
2b4b57cd 437 return (0);
cce93e4b 438 m = m_get(M_DONTWAIT, MT_SONAME);
509e40dd 439 if (m == 0)
2b4b57cd 440 return (0);
2b4b57cd
BJ
441 m->m_len = sizeof (struct sockaddr);
442 msa = mtod(m, struct sockaddr *);
443 *msa = *asa;
444 m->m_act = (struct mbuf *)1;
445 sbappend(sb, m);
2b4b57cd
BJ
446 sbappend(sb, m0);
447 return (1);
448}
449
5fe6f9d1 450#ifdef notdef
e435773e
BJ
451SBCHECK(sb, str)
452 struct sockbuf *sb;
453 char *str;
454{
455 register int cnt = sb->sb_cc;
456 register int mbcnt = sb->sb_mbcnt;
457 register struct mbuf *m;
458
459 for (m = sb->sb_mb; m; m = m->m_next) {
460 cnt -= m->m_len;
461 mbcnt -= MSIZE;
462 if (m->m_off > MMAXOFF)
463 mbcnt -= CLBYTES;
464 }
465 if (cnt || mbcnt) {
466 printf("cnt %d mbcnt %d\n", cnt, mbcnt);
467 panic(str);
468 }
469}
5fe6f9d1 470#endif
e435773e 471
681ebb17
BJ
472/*
473 * Free all mbufs on a sockbuf mbuf chain.
474 * Check that resource allocations return to 0.
475 */
476sbflush(sb)
477 struct sockbuf *sb;
478{
479
480 if (sb->sb_flags & SB_LOCK)
481 panic("sbflush");
a73ab5ae
BJ
482 if (sb->sb_cc)
483 sbdrop(sb, sb->sb_cc);
681ebb17
BJ
484 if (sb->sb_cc || sb->sb_mbcnt || sb->sb_mb)
485 panic("sbflush 2");
486}
487
488/*
489 * Drop data from (the front of) a sockbuf chain.
490 */
491sbdrop(sb, len)
492 register struct sockbuf *sb;
493 register int len;
494{
495 register struct mbuf *m = sb->sb_mb, *mn;
496
497 while (len > 0) {
498 if (m == 0)
499 panic("sbdrop");
b9f0d37f 500 if (m->m_len > len) {
681ebb17
BJ
501 m->m_len -= len;
502 m->m_off += len;
503 sb->sb_cc -= len;
504 break;
505 }
b9f0d37f
BJ
506 len -= m->m_len;
507 sbfree(sb, m);
508 MFREE(m, mn);
509 m = mn;
681ebb17
BJ
510 }
511 sb->sb_mb = m;
512}