Annotation of src/sys/sys/socketvar.h, Revision 1.61
1.61 ! darrenr 1: /* $NetBSD: socketvar.h,v 1.60 2003/05/03 17:53:18 yamt Exp $ */
1.11 cgd 2:
1.1 cgd 3: /*-
1.8 mycroft 4: * Copyright (c) 1982, 1986, 1990, 1993
5: * The Regents of the University of California. All rights reserved.
1.1 cgd 6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: *
1.28 fvdl 35: * @(#)socketvar.h 8.3 (Berkeley) 2/19/95
1.1 cgd 36: */
37:
1.24 mikel 38: #ifndef _SYS_SOCKETVAR_H_
1.42 lukem 39: #define _SYS_SOCKETVAR_H_
1.24 mikel 40:
1.5 cgd 41: #include <sys/select.h> /* for struct selinfo */
1.27 thorpej 42: #include <sys/queue.h>
43:
1.38 simonb 44: #if !defined(_KERNEL) || defined(LKM)
1.29 matt 45: struct uio;
46: #endif
47:
1.27 thorpej 48: TAILQ_HEAD(soqhead, socket);
1.3 mycroft 49:
1.1 cgd 50: /*
1.58 matt 51: * Variables for socket buffering.
52: */
53: struct sockbuf {
54: u_long sb_cc; /* actual chars in buffer */
55: u_long sb_hiwat; /* max actual char count */
56: u_long sb_mbcnt; /* chars of mbufs used */
57: u_long sb_mbmax; /* max chars of mbufs to use */
58: long sb_lowat; /* low water mark */
59: struct mbuf *sb_mb; /* the mbuf chain */
60: struct mbuf *sb_mbtail; /* the last mbuf in the chain */
61: struct mbuf *sb_lastrecord; /* first mbuf of last record in
62: socket buffer */
63: struct mowner *sb_mowner; /* who owns data for this sockbuf */
64: struct selinfo sb_sel; /* process selecting read/write */
65: short sb_flags; /* flags, see below */
66: short sb_timeo; /* timeout for read/write */
67: };
68:
69: #ifndef SB_MAX
70: #define SB_MAX (256*1024) /* default for max chars in sockbuf */
71: #endif
72:
73: #define SB_LOCK 0x01 /* lock on data queue */
74: #define SB_WANT 0x02 /* someone is waiting to lock */
75: #define SB_WAIT 0x04 /* someone is waiting for data/space */
76: #define SB_SEL 0x08 /* someone is selecting */
77: #define SB_ASYNC 0x10 /* ASYNC I/O, need signals */
78: #define SB_UPCALL 0x20 /* someone wants an upcall */
79: #define SB_NOINTR 0x40 /* operations not interruptible */
80: /* XXXLUKEM: 0x80 left for FreeBSD's SB_AIO */
81: #define SB_KNOTE 0x100 /* kernel note attached */
82:
83: /*
1.1 cgd 84: * Kernel structure per socket.
85: * Contains send and receive buffer queues,
86: * handle on protocol and pointer to protocol
87: * private data and error information.
88: */
89: struct socket {
1.42 lukem 90: short so_type; /* generic type, see socket.h */
91: short so_options; /* from socket call, see socket.h */
92: short so_linger; /* time to linger while closing */
93: short so_state; /* internal state flags SS_*, below */
94: void *so_pcb; /* protocol control block */
95: struct protosw *so_proto; /* protocol handle */
1.1 cgd 96: /*
97: * Variables for connection queueing.
98: * Socket where accepts occur is so_head in all subsidiary sockets.
99: * If so_head is 0, socket is not related to an accept.
100: * For head socket so_q0 queues partially completed connections,
101: * while so_q is a queue of connections ready to be accepted.
102: * If a connection is aborted and it has so_head set, then
103: * it has to be pulled out of either so_q0 or so_q.
104: * We allow connections to queue up based on current queue lengths
105: * and limit on number of queued connections for this socket.
106: */
1.42 lukem 107: struct socket *so_head; /* back pointer to accept socket */
108: struct soqhead *so_onq; /* queue (q or q0) that we're on */
109: struct soqhead so_q0; /* queue of partial connections */
110: struct soqhead so_q; /* queue of incoming connections */
1.27 thorpej 111: TAILQ_ENTRY(socket) so_qe; /* our queue entry (q or q0) */
1.42 lukem 112: short so_q0len; /* partials on so_q0 */
113: short so_qlen; /* number of connections on so_q */
114: short so_qlimit; /* max number queued connections */
115: short so_timeo; /* connection timeout */
116: u_short so_error; /* error affecting connection */
117: pid_t so_pgid; /* pgid for signals */
118: u_long so_oobmark; /* chars to oob mark */
1.58 matt 119: struct sockbuf so_snd; /* send buffer */
120: struct sockbuf so_rcv; /* receive buffer */
1.1 cgd 121:
1.42 lukem 122: void *so_internal; /* Space for svr4 stream data */
123: void (*so_upcall) __P((struct socket *so, caddr_t arg,
124: int waitf));
125: caddr_t so_upcallarg; /* Arg for above */
126: int (*so_send) __P((struct socket *so, struct mbuf *addr,
127: struct uio *uio, struct mbuf *top,
128: struct mbuf *control, int flags));
129: int (*so_receive) __P((struct socket *so,
130: struct mbuf **paddr,
131: struct uio *uio, struct mbuf **mp0,
132: struct mbuf **controlp, int *flagsp));
1.58 matt 133: struct mowner *so_mowner; /* who owns mbufs for this socket */
1.42 lukem 134: uid_t so_uid; /* who opened the socket */
1.51 thorpej 135: struct mbuf *so_pendfree; /* loaned-page mbufs w/ frees pending */
1.1 cgd 136: };
137:
1.54 thorpej 138: #define SB_EMPTY_FIXUP(sb) \
1.52 thorpej 139: do { \
140: if ((sb)->sb_mb == NULL) { \
141: (sb)->sb_mbtail = NULL; \
142: (sb)->sb_lastrecord = NULL; \
143: } \
144: } while (/*CONSTCOND*/0)
145:
1.1 cgd 146: /*
147: * Socket state bits.
148: */
149: #define SS_NOFDREF 0x001 /* no file table ref any more */
150: #define SS_ISCONNECTED 0x002 /* socket connected to a peer */
151: #define SS_ISCONNECTING 0x004 /* in process of connecting to peer */
152: #define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */
153: #define SS_CANTSENDMORE 0x010 /* can't send more data to peer */
154: #define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */
155: #define SS_RCVATMARK 0x040 /* at mark on input */
1.33 mycroft 156: #define SS_ISDISCONNECTED 0x800 /* socket disconnected from peer */
1.1 cgd 157:
1.19 mycroft 158: #define SS_NBIO 0x080 /* non-blocking ops */
159: #define SS_ASYNC 0x100 /* async i/o notify */
160: #define SS_ISCONFIRMING 0x200 /* deciding to accept connection req */
1.42 lukem 161: #define SS_MORETOCOME 0x400 /*
162: * hint from sosend to lower layer;
163: * more data coming
164: */
1.46 manu 165: #define SS_ISAPIPE 0x800 /* socket is implementing a pipe */
1.1 cgd 166:
167:
168: /*
169: * Macros for sockets and socket buffering.
170: */
1.15 mycroft 171:
172: /*
173: * Do we need to notify the other side when I/O is possible?
174: */
1.42 lukem 175: #define sb_notify(sb) (((sb)->sb_flags & \
1.55 jdolecek 176: (SB_WAIT | SB_SEL | SB_ASYNC | SB_UPCALL | SB_KNOTE)) != 0)
1.1 cgd 177:
178: /*
179: * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
180: * This is problematical if the fields are unsigned, as the space might
181: * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
1.50 jdolecek 182: * overflow and return 0.
1.1 cgd 183: */
184: #define sbspace(sb) \
1.50 jdolecek 185: (lmin((sb)->sb_hiwat - (sb)->sb_cc, (sb)->sb_mbmax - (sb)->sb_mbcnt))
1.1 cgd 186:
187: /* do we have to send all at once on a socket? */
188: #define sosendallatonce(so) \
1.42 lukem 189: ((so)->so_proto->pr_flags & PR_ATOMIC)
1.1 cgd 190:
191: /* can we read something from so? */
192: #define soreadable(so) \
1.42 lukem 193: ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
194: ((so)->so_state & SS_CANTRCVMORE) || \
195: (so)->so_qlen || (so)->so_error)
1.1 cgd 196:
197: /* can we write something to so? */
1.56 wiz 198: #define sowritable(so) \
1.42 lukem 199: ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
200: (((so)->so_state&SS_ISCONNECTED) || \
201: ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
202: ((so)->so_state & SS_CANTSENDMORE) || \
203: (so)->so_error)
1.1 cgd 204:
205: /* adjust counters in sb reflecting allocation of m */
1.42 lukem 206: #define sballoc(sb, m) \
207: do { \
208: (sb)->sb_cc += (m)->m_len; \
209: (sb)->sb_mbcnt += MSIZE; \
210: if ((m)->m_flags & M_EXT) \
211: (sb)->sb_mbcnt += (m)->m_ext.ext_size; \
1.45 lukem 212: } while (/* CONSTCOND */ 0)
1.1 cgd 213:
214: /* adjust counters in sb reflecting freeing of m */
1.42 lukem 215: #define sbfree(sb, m) \
216: do { \
217: (sb)->sb_cc -= (m)->m_len; \
218: (sb)->sb_mbcnt -= MSIZE; \
219: if ((m)->m_flags & M_EXT) \
220: (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
1.45 lukem 221: } while (/* CONSTCOND */ 0)
1.1 cgd 222:
223: /*
224: * Set lock on sockbuf sb; sleep if lock is already held.
225: * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
226: * Returns error without lock if sleep is interrupted.
227: */
1.42 lukem 228: #define sblock(sb, wf) \
229: ((sb)->sb_flags & SB_LOCK ? \
230: (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
231: ((sb)->sb_flags |= SB_LOCK), 0)
1.1 cgd 232:
233: /* release lock on sockbuf sb */
1.42 lukem 234: #define sbunlock(sb) \
235: do { \
236: (sb)->sb_flags &= ~SB_LOCK; \
237: if ((sb)->sb_flags & SB_WANT) { \
238: (sb)->sb_flags &= ~SB_WANT; \
239: wakeup((caddr_t)&(sb)->sb_flags); \
240: } \
1.45 lukem 241: } while (/* CONSTCOND */ 0)
1.42 lukem 242:
243: #define sorwakeup(so) \
244: do { \
245: if (sb_notify(&(so)->so_rcv)) \
246: sowakeup((so), &(so)->so_rcv); \
1.45 lukem 247: } while (/* CONSTCOND */ 0)
1.42 lukem 248:
249: #define sowwakeup(so) \
250: do { \
251: if (sb_notify(&(so)->so_snd)) \
252: sowakeup((so), &(so)->so_snd); \
1.45 lukem 253: } while (/* CONSTCOND */ 0)
1.1 cgd 254:
1.14 jtc 255: #ifdef _KERNEL
1.42 lukem 256: extern u_long sb_max;
1.1 cgd 257: /* to catch callers missing new second argument to sonewconn: */
258: #define sonewconn(head, connstatus) sonewconn1((head), (connstatus))
259:
260: /* strings for sleep message: */
1.42 lukem 261: extern const char netio[], netcon[], netcls[];
1.1 cgd 262:
1.42 lukem 263: extern struct pool socket_pool;
1.31 thorpej 264:
1.17 christos 265: struct mbuf;
266: struct sockaddr;
267: struct proc;
268: struct msghdr;
269: struct stat;
1.55 jdolecek 270: struct knote;
1.17 christos 271:
1.1 cgd 272: /*
273: * File operations on sockets.
274: */
1.42 lukem 275: int soo_read(struct file *fp, off_t *offset, struct uio *uio,
276: struct ucred *cred, int flags);
277: int soo_write(struct file *fp, off_t *offset, struct uio *uio,
278: struct ucred *cred, int flags);
1.61 ! darrenr 279: int soo_fcntl(struct file *fp, u_int cmd, void *data, struct lwp *p);
! 280: int soo_ioctl(struct file *fp, u_long cmd, void *data, struct lwp *p);
! 281: int soo_poll(struct file *fp, int events, struct lwp *p);
1.55 jdolecek 282: int soo_kqfilter(struct file *fp, struct knote *kn);
1.61 ! darrenr 283: int soo_close(struct file *fp, struct lwp *p);
! 284: int soo_stat(struct file *fp, struct stat *ub, struct lwp *p);
1.42 lukem 285: int uipc_usrreq(struct socket *, int , struct mbuf *,
1.61 ! darrenr 286: struct mbuf *, struct mbuf *, struct lwp *);
1.42 lukem 287: int uipc_ctloutput(int, struct socket *, int, int, struct mbuf **);
288: void sbappend(struct sockbuf *sb, struct mbuf *m);
1.53 thorpej 289: void sbappendstream(struct sockbuf *sb, struct mbuf *m);
1.42 lukem 290: int sbappendaddr(struct sockbuf *sb, struct sockaddr *asa,
291: struct mbuf *m0, struct mbuf *control);
292: int sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
293: struct mbuf *control);
294: void sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
295: void sbcheck(struct sockbuf *sb);
296: void sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
1.23 thorpej 297: struct mbuf *
1.42 lukem 298: sbcreatecontrol(caddr_t p, int size, int type, int level);
299: void sbdrop(struct sockbuf *sb, int len);
300: void sbdroprecord(struct sockbuf *sb);
301: void sbflush(struct sockbuf *sb);
302: void sbinsertoob(struct sockbuf *sb, struct mbuf *m0);
303: void sbrelease(struct sockbuf *sb);
304: int sbreserve(struct sockbuf *sb, u_long cc);
305: int sbwait(struct sockbuf *sb);
306: int sb_lock(struct sockbuf *sb);
307: void soinit(void);
308: int soabort(struct socket *so);
309: int soaccept(struct socket *so, struct mbuf *nam);
1.61 ! darrenr 310: int sobind(struct socket *so, struct mbuf *nam, struct lwp *);
1.42 lukem 311: void socantrcvmore(struct socket *so);
312: void socantsendmore(struct socket *so);
313: int soclose(struct socket *so);
314: int soconnect(struct socket *so, struct mbuf *nam);
315: int soconnect2(struct socket *so1, struct socket *so2);
316: int socreate(int dom, struct socket **aso, int type, int proto);
317: int sodisconnect(struct socket *so);
318: void sofree(struct socket *so);
319: int sogetopt(struct socket *so, int level, int optname, struct mbuf **mp);
320: void sohasoutofband(struct socket *so);
321: void soisconnected(struct socket *so);
322: void soisconnecting(struct socket *so);
323: void soisdisconnected(struct socket *so);
324: void soisdisconnecting(struct socket *so);
325: int solisten(struct socket *so, int backlog);
1.7 mycroft 326: struct socket *
1.42 lukem 327: sonewconn1(struct socket *head, int connstatus);
328: void soqinsque(struct socket *head, struct socket *so, int q);
329: int soqremque(struct socket *so, int q);
330: int soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
331: struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
332: int soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
333: void sorflush(struct socket *so);
334: int sosend(struct socket *so, struct mbuf *addr, struct uio *uio,
335: struct mbuf *top, struct mbuf *control, int flags);
336: int sosetopt(struct socket *so, int level, int optname, struct mbuf *m0);
337: int soshutdown(struct socket *so, int how);
338: void sowakeup(struct socket *so, struct sockbuf *sb);
1.49 matt 339: int sockargs(struct mbuf **, const void *, size_t, int);
1.42 lukem 340:
1.61 ! darrenr 341: int sendit(struct lwp *, int, struct msghdr *, int, register_t *);
! 342: int recvit(struct lwp *, int, struct msghdr *, caddr_t, register_t *);
1.52 thorpej 343:
344: #ifdef SOCKBUF_DEBUG
345: void sblastrecordchk(struct sockbuf *, const char *);
346: #define SBLASTRECORDCHK(sb, where) sblastrecordchk((sb), (where))
347:
348: void sblastmbufchk(struct sockbuf *, const char *);
349: #define SBLASTMBUFCHK(sb, where) sblastmbufchk((sb), (where))
350: #else
351: #define SBLASTRECORDCHK(sb, where) /* nothing */
352: #define SBLASTMBUFCHK(sb, where) /* nothing */
353: #endif /* SOCKBUF_DEBUG */
1.60 yamt 354:
355: /* sosend loan */
356: vaddr_t sokvaalloc(vsize_t, struct socket *);
357: void sokvafree(vaddr_t, vsize_t);
358: void soloanfree(struct mbuf *, caddr_t, size_t, void *);
1.17 christos 359:
1.14 jtc 360: #endif /* _KERNEL */
1.24 mikel 361:
362: #endif /* !_SYS_SOCKETVAR_H_ */
CVSweb <webmaster@jp.NetBSD.org>