📄 uipc_sock2.c
字号:
* a data record, perhaps of zero length. * * Before using a new socket structure it is first necessary to reserve * buffer space to the socket, by calling sbreserve(). This should commit * some of the available buffer space in the system buffer pool for the * socket (currently, it does nothing but enforce limits). The space * should be released by calling sbrelease() when the socket is destroyed. */intsoreserve(so, sndcc, rcvcc) register struct socket *so; u_long sndcc, rcvcc;{ if (sbreserve(&so->so_snd, sndcc) == 0) goto bad; if (sbreserve(&so->so_rcv, rcvcc) == 0) goto bad2; if (so->so_rcv.sb_lowat == 0) so->so_rcv.sb_lowat = 1; if (so->so_snd.sb_lowat == 0) so->so_snd.sb_lowat = CL_SIZE_64; if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) so->so_snd.sb_lowat = so->so_snd.sb_hiwat; return (0);bad2: sbrelease(&so->so_snd);bad: return (ENOBUFS);}/* * Allot mbufs to a sockbuf. * Attempt to scale mbmax so that mbcnt doesn't become limiting * if buffering efficiency is near the normal case. */intsbreserve(sb, cc) struct sockbuf *sb; u_long cc;{ if (cc > sb_max * MCLBYTES / (MSIZE + MCLBYTES)) return (0); sb->sb_hiwat = cc; sb->sb_mbmax = min(cc * 5, sb_max); if (sb->sb_lowat > sb->sb_hiwat) sb->sb_lowat = sb->sb_hiwat; return (1);}/* * Free mbufs held by a socket, and reserved mbuf space. */voidsbrelease(sb) struct sockbuf *sb;{ sbflush(sb); sb->sb_hiwat = sb->sb_mbmax = 0;}/* * Routines to add and remove * data from an mbuf queue. * * The routines sbappend() or sbappendrecord() are normally called to * append new mbufs to a socket buffer, after checking that adequate * space is available, comparing the function sbspace() with the amount * of data to be added. sbappendrecord() differs from sbappend() in * that data supplied is treated as the beginning of a new record. * To place a sender's address, optional access rights, and data in a * socket receive buffer, sbappendaddr() should be used. To place * access rights and data in a socket receive buffer, sbappendrights() * should be used. In either case, the new data begins a new record. * Note that unlike sbappend() and sbappendrecord(), these routines check * for the caller that there will be enough space to store the data. * Each fails if there is not enough space, or if it cannot find mbufs * to store additional information in. * * Reliable protocols may use the socket send buffer to hold data * awaiting acknowledgement. Data is normally copied from a socket * send buffer in a protocol with m_copy for output to a peer, * and then removing the data from the socket buffer with sbdrop() * or sbdroprecord() when the data is acknowledged by the peer. *//* * Append mbuf chain m to the last record in the * socket buffer sb. The additional space associated * the mbuf chain is recorded in sb. Empty mbufs are * discarded and mbufs are compacted where possible. */voidsbappend(sb, m) struct sockbuf *sb; struct mbuf *m;{ register struct mbuf *n; if (m == 0) return; if (n = sb->sb_mb) { while (n->m_nextpkt) n = n->m_nextpkt; do { if (n->m_flags & M_EOR) { sbappendrecord(sb, m); /* XXXXXX!!!! */ return; } } while (n->m_next && (n = n->m_next)); } sbcompress(sb, m, n);}#ifdef SOCKBUF_DEBUGvoidsbcheck(sb) register struct sockbuf *sb;{ register struct mbuf *m; register int len = 0, mbcnt = 0; for (m = sb->sb_mb; m; m = m->m_next) { len += m->m_len; mbcnt += MSIZE; if (m->m_flags & M_EXT) mbcnt += m->m_extSize; if (m->m_nextpkt) panic("sbcheck nextpkt"); } if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { printf("cc %d != %d || mbcnt %d != %d\n", len, sb->sb_cc, mbcnt, sb->sb_mbcnt); panic("sbcheck"); }}#endif/* * As above, except the mbuf chain * begins a new record. */voidsbappendrecord(sb, m0) register struct sockbuf *sb; register struct mbuf *m0;{ register struct mbuf *m; if (m0 == 0) return; if (m = sb->sb_mb) while (m->m_nextpkt) m = m->m_nextpkt; /* * Put the first mbuf on the queue. * Note this permits zero length records. */ sballoc(sb, m0); if (m) m->m_nextpkt = m0; else sb->sb_mb = m0; m = m0->m_next; m0->m_next = 0; if (m && (m0->m_flags & M_EOR)) { m0->m_flags &= ~M_EOR; m->m_flags |= M_EOR; } sbcompress(sb, m, m0);}/* * As above except that OOB data * is inserted at the beginning of the sockbuf, * but after any other OOB data. */voidsbinsertoob(sb, m0) register struct sockbuf *sb; register struct mbuf *m0;{ register struct mbuf *m; register struct mbuf **mp; if (m0 == 0) return; for (mp = &sb->sb_mb; m = *mp; mp = &((*mp)->m_nextpkt)) { again: switch (m->m_type) { case MT_OOBDATA: continue; /* WANT next train */ case MT_CONTROL: if (m = m->m_next) goto again; /* inspect THIS train further */ } break; } /* * Put the first mbuf on the queue. * Note this permits zero length records. */ sballoc(sb, m0); m0->m_nextpkt = *mp; *mp = m0; m = m0->m_next; m0->m_next = 0; if (m && (m0->m_flags & M_EOR)) { m0->m_flags &= ~M_EOR; m->m_flags |= M_EOR; } sbcompress(sb, m, m0);}/* * Append address and data, and optionally, control (ancillary) data * to the receive queue of a socket. If present, * m0 must include a packet header with total length. * Returns 0 if no space in sockbuf or insufficient mbufs. */intsbappendaddr(sb, asa, m0, control) register struct sockbuf *sb; struct sockaddr *asa; struct mbuf *m0, *control;{ register struct mbuf *m, *n; int space = asa->sa_len;if (m0 && (m0->m_flags & M_PKTHDR) == 0)panic("sbappendaddr"); if (m0) space += m0->m_pkthdr.len; for (n = control; n; n = n->m_next) { space += n->m_len; if (n->m_next == 0) /* keep pointer to last control buf */ break; } if (space > sbspace(sb)) return (0); if (asa->sa_len > CL_SIZE_128) return (0); m = mBufClGet(M_DONTWAIT, MT_SONAME, CL_SIZE_128, TRUE); if (m == 0) return (0); m->m_len = asa->sa_len; bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len); if (n) n->m_next = m0; /* concatenate data to control */ else control = m0; m->m_next = control; for (n = m; n; n = n->m_next) sballoc(sb, n); if (n = sb->sb_mb) { while (n->m_nextpkt) n = n->m_nextpkt; n->m_nextpkt = m; } else sb->sb_mb = m; return (1);}intsbappendcontrol(sb, m0, control) struct sockbuf *sb; struct mbuf *m0, *control;{ register struct mbuf *m, *n; int space = 0; if (control == 0) panic("sbappendcontrol"); for (m = control; ; m = m->m_next) { space += m->m_len; if (m->m_next == 0) break; } n = m; /* save pointer to last control buffer */ for (m = m0; m; m = m->m_next) space += m->m_len; if (space > sbspace(sb)) return (0); n->m_next = m0; /* concatenate data to control */ for (m = control; m; m = m->m_next) sballoc(sb, m); if (n = sb->sb_mb) { while (n->m_nextpkt) n = n->m_nextpkt; n->m_nextpkt = control; } else sb->sb_mb = control; return (1);}/* * Compress mbuf chain m into the socket * buffer sb following mbuf n. If n * is null, the buffer is presumed empty. */voidsbcompress(sb, m, n) register struct sockbuf *sb; register struct mbuf *m, *n;{ register int eor = 0; register struct mbuf *o; while (m) { eor |= m->m_flags & M_EOR; if (m->m_len == 0 && (eor == 0 || (((o = m->m_next) || (o = n)) && o->m_type == m->m_type))) { m = m_free(m); continue; } /* * This test determines whether or not the data in <m> will * be copied to the cluster referenced by <n>. The conditions * which are required for compression are: * 1) <n> is non-NULL * 2) <n> is not the end of a record. M_EOR is set for * datagram protocols (UDP, raw IP) but not stream * protocols (TCP) * 3) length of data in <m> is less than CL_SIZE_MIN. This * condition optimizes buffer usage for small clusters * and optimizes speed for large clusters, by not * copying data in those cases. * 4) The data in <m> must fit in the remaining space in * the cluster referenced by <n> * 5) <m> and <n> must contain the same type of data */ if (n && (n->m_flags & M_EOR) == 0 && m->m_len < CL_SIZE_MIN && (n->m_data + n->m_len + m->m_len) < (n->m_extBuf + n->m_extSize) && n->m_type == m->m_type) { bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, (unsigned)m->m_len); n->m_len += m->m_len; sb->sb_cc += m->m_len; m = m_free(m); continue; } if (n) n->m_next = m; else sb->sb_mb = m; sballoc(sb, m); n = m; m->m_flags &= ~M_EOR; m = m->m_next; n->m_next = 0; } if (eor) { if (n) n->m_flags |= eor; else printf("semi-panic: sbcompress\n"); }}/* * Free all mbufs in a sockbuf. * Check that all resources are reclaimed. */voidsbflush(sb) register struct sockbuf *sb;{ if (sb->sb_flags & SB_LOCK) panic("sbflush"); while (sb->sb_mbcnt) sbdrop(sb, (int)sb->sb_cc); if (sb->sb_cc || sb->sb_mb) panic("sbflush 2");}/* * Drop data from (the front of) a sockbuf. */voidsbdrop(sb, len) register struct sockbuf *sb; register int len;{ register struct mbuf *m, *mn; struct mbuf *next; next = (m = sb->sb_mb) ? m->m_nextpkt : 0; while (len > 0) { if (m == 0) { if (next == 0) panic("sbdrop"); m = next; next = m->m_nextpkt; continue; } if (m->m_len > len) { m->m_len -= len; m->m_data += len; sb->sb_cc -= len; break; } len -= m->m_len; sbfree(sb, m); mn = m_free(m); m = mn; } while (m && m->m_len == 0) { sbfree(sb, m); mn = m_free(m); m = mn; } if (m) { sb->sb_mb = m; m->m_nextpkt = next; } else sb->sb_mb = next;}/* * Drop a record off the front of a sockbuf * and move the next record to the front. */voidsbdroprecord(sb) register struct sockbuf *sb;{ register struct mbuf *m, *mn; m = sb->sb_mb; if (m) { sb->sb_mb = m->m_nextpkt; do { sbfree(sb, m); mn = m_free(m); } while ((m = mn)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -