X-Git-Url: https://git.subgeniuskitty.com/unix-history/.git/blobdiff_plain/b9033129dad1ac93233a13dca161eae84b91b6bc..95be0979f7f3d6a0ec21c6c2c2eeb80ed6354ffa:/usr/src/sys/net/if_sl.c diff --git a/usr/src/sys/net/if_sl.c b/usr/src/sys/net/if_sl.c index 472ba84579..9174150370 100644 --- a/usr/src/sys/net/if_sl.c +++ b/usr/src/sys/net/if_sl.c @@ -1,15 +1,20 @@ /* - * Copyright (c) 1987 Regents of the University of California. + * Copyright (c) 1987, 1989 Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms are permitted - * provided that this notice is preserved and that due credit is given - * to the University of California at Berkeley. The name of the University - * may not be used to endorse or promote products derived from this - * software without specific prior written permission. This software - * is provided ``as is'' without express or implied warranty. + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * @(#)if_sl.c 7.6 (Berkeley) %G% + * @(#)if_sl.c 7.16 (Berkeley) %G% */ /* @@ -30,22 +35,37 @@ * * Converted to 4.3BSD Beta by Chris Torek. * Other changes made at Berkeley, based in part on code by Kirk Smith. + * W. Jolitz added slip abort. + * + * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov). + * Added priority queuing for "interactive" traffic; hooks for TCP + * header compression; ICMP filtering (at 2400 baud, some cretin + * pinging you can use up all your bandwidth). Made low clist behavior + * more robust and slightly less likely to hang serial line. + * Sped up a bunch of things. + * + * Note that splimp() is used throughout to block both (tty) input + * interrupts and network activity; thus, splimp must be >= spltty. */ -/* $Header: if_sl.c,v 1.12 85/12/20 21:54:55 chris Exp $ */ +/* $Header: if_sl.c,v 1.7 89/05/31 02:24:52 van Exp $ */ /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */ #include "sl.h" #if NSL > 0 #include "param.h" +#include "dir.h" +#include "user.h" #include "mbuf.h" #include "buf.h" -#include "dkstat.h" +#include "dk.h" #include "socket.h" #include "ioctl.h" #include "file.h" #include "tty.h" +#include "kernel.h" +#include "conf.h" #include "errno.h" #include "if.h" @@ -58,33 +78,72 @@ #include "../netinet/ip.h" #endif -#include "../machine/mtpr.h" +#include "machine/mtpr.h" + +#include "slcompress.h" +#include "if_slvar.h" + +/* + * SLMTU is a hard limit on input packet size. To simplify the code + * and improve performance, we require that packets fit in an mbuf + * cluster, that there be enough extra room for the ifnet pointer that + * IP input requires and, if we get a compressed packet, there's + * enough extra room to expand the header into a max length tcp/ip + * header (128 bytes). So, SLMTU can be at most + * MCLBYTES - sizeof(struct ifnet *) - 128 + * + * To insure we get good interactive response, the MTU wants to be + * the smallest size that amortizes the header cost. (Remember + * that even with type-of-service queuing, we have to wait for any + * in-progress packet to finish. I.e., we wait, on the average, + * 1/2 * mtu / cps, where cps is the line speed in characters per + * second. E.g., 533ms wait for a 1024 byte MTU on a 9600 baud + * line. The average compressed header size is 6-8 bytes so any + * MTU > 90 bytes will give us 90% of the line bandwidth. A 100ms + * wait is tolerable (500ms is not), so want an MTU around 256. + * (Since TCP will send 212 byte segments (to allow for 40 byte + * headers), the typical packet size on the wire will be around 220 + * bytes). In 4.3tahoe+ systems, we can set an MTU in a route + * so we do that & leave the interface MTU relatively high (so we + * don't IP fragment when acting as a gateway to someone using a + * stupid MTU). + */ +#define SLMTU 576 +#define BUFOFFSET (128+sizeof(struct ifnet **)) +#define SLIP_HIWAT 1024 /* don't start a new packet if HIWAT on queue */ +#define CLISTRESERVE 1024 /* Can't let clists get too low */ +/* + * SLIP ABORT ESCAPE MECHANISM: + * (inspired by HAYES modem escape arrangement) + * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape } + * signals a "soft" exit from slip mode by usermode process + */ + +#define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/ +#define ABT_WAIT 1 /* in seconds - idle before an escape & after */ +#define ABT_RECYCLE (5*2+2) /* in seconds - time window processing abort */ + +#define ABT_SOFT 3 /* count of escapes */ /* - * N.B.: SLMTU is now a hard limit on input packet size. - * SLMTU must be <= CLBYTES - sizeof(struct ifnet *). + * The following disgusting hack gets around the problem that IP TOS + * can't be set in BSD/Sun OS yet. We want to put "interactive" + * traffic on a high priority queue. To decide if traffic is + * interactive, we check that a) it is TCP and b) one of it's ports + * if telnet, rlogin or ftp control. */ -#define SLMTU 1006 -#define SLIP_HIWAT 1000 /* don't start a new packet if HIWAT on queue */ -#define CLISTRESERVE 1000 /* Can't let clists get too low */ - -struct sl_softc { - struct ifnet sc_if; /* network-visible interface */ - short sc_flags; /* see below */ - short sc_ilen; /* length of input-packet-so-far */ - struct tty *sc_ttyp; /* pointer to tty structure */ - char *sc_mp; /* pointer to next available buf char */ - char *sc_buf; /* input buffer */ -} sl_softc[NSL]; - -/* flags */ -#define SC_ESCAPED 0x0001 /* saw a FRAME_ESCAPE */ -#define SC_OACTIVE 0x0002 /* output tty is active */ - -#define FRAME_END 0300 /* Frame End */ -#define FRAME_ESCAPE 0333 /* Frame Esc */ -#define TRANS_FRAME_END 0334 /* transposed frame end */ -#define TRANS_FRAME_ESCAPE 0335 /* transposed frame esc */ +static u_short interactive_ports[8] = { + 0, 513, 0, 0, + 0, 21, 0, 23, +}; +#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p)) + +struct sl_softc sl_softc[NSL]; + +#define FRAME_END 0xc0 /* Frame End */ +#define FRAME_ESCAPE 0xdb /* Frame Esc */ +#define TRANS_FRAME_END 0xdc /* transposed frame end */ +#define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */ #define t_sc T_LINEP @@ -105,11 +164,34 @@ slattach() sc->sc_if.if_flags = IFF_POINTOPOINT; sc->sc_if.if_ioctl = slioctl; sc->sc_if.if_output = sloutput; - sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN; + sc->sc_if.if_snd.ifq_maxlen = 50; + sc->sc_fastq.ifq_maxlen = 32; if_attach(&sc->sc_if); } } +static int +slinit(sc) + register struct sl_softc *sc; +{ + register caddr_t p; + + if (sc->sc_ep == (u_char *) 0) { + MCLALLOC(p, M_WAIT); + if (p) + sc->sc_ep = (u_char *)p + (BUFOFFSET + SLMTU); + else { + printf("sl%d: can't allocate buffer\n", sc - sl_softc); + sc->sc_if.if_flags &= ~IFF_UP; + return (0); + } + } + sc->sc_buf = sc->sc_ep - SLMTU; + sc->sc_mp = sc->sc_buf; + sl_compress_init(&sc->sc_comp); + return (1); +} + /* * Line specific open routine. * Attach the given tty to the first available sl unit. @@ -121,16 +203,16 @@ slopen(dev, tp) { register struct sl_softc *sc; register int nsl; + int error; + + if (error = suser(u.u_cred, &u.u_acflag)) + return (error); - if (!suser()) - return (EPERM); if (tp->t_line == SLIPDISC) return (EBUSY); - for (nsl = 0, sc = sl_softc; nsl < NSL; nsl++, sc++) + for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++) if (sc->sc_ttyp == NULL) { - sc->sc_flags = 0; - sc->sc_ilen = 0; if (slinit(sc) == 0) return (ENOBUFS); tp->t_sc = (caddr_t)sc; @@ -138,7 +220,6 @@ slopen(dev, tp) ttyflush(tp, FREAD | FWRITE); return (0); } - return (ENXIO); } @@ -154,14 +235,16 @@ slclose(tp) int s; ttywflush(tp); + s = splimp(); /* actually, max(spltty, splnet) */ tp->t_line = 0; - s = splimp(); /* paranoid; splnet probably ok */ sc = (struct sl_softc *)tp->t_sc; if (sc != NULL) { if_down(&sc->sc_if); sc->sc_ttyp = NULL; tp->t_sc = NULL; - MCLFREE((struct mbuf *)sc->sc_buf); + MCLFREE((struct mbuf *)(sc->sc_ep - (SLMTU + BUFOFFSET))); + sc->sc_ep = 0; + sc->sc_mp = 0; sc->sc_buf = 0; } splx(s); @@ -176,12 +259,31 @@ sltioctl(tp, cmd, data, flag) struct tty *tp; caddr_t data; { + struct sl_softc *sc = (struct sl_softc *)tp->t_sc; + int s; - if (cmd == TIOCGETD) { - *(int *)data = ((struct sl_softc *)tp->t_sc)->sc_if.if_unit; - return (0); + switch (cmd) { + case TIOCGETD: /* XXX */ + case SLIOGUNIT: + *(int *)data = sc->sc_if.if_unit; + break; + + case SLIOCGFLAGS: + *(int *)data = sc->sc_flags; + break; + + case SLIOCSFLAGS: +#define SC_MASK (SC_COMPRESS|SC_NOICMP) + s = splimp(); + sc->sc_flags = + (sc->sc_flags &~ SC_MASK) | ((*(int *)data) & SC_MASK); + splx(s); + break; + + default: + return (-1); } - return (-1); + return (0); } /* @@ -193,6 +295,8 @@ sloutput(ifp, m, dst) struct sockaddr *dst; { register struct sl_softc *sc; + register struct ip *ip; + register struct ifqueue *ifq; int s; /* @@ -215,16 +319,38 @@ sloutput(ifp, m, dst) m_freem(m); return (EHOSTUNREACH); } + ifq = &ifp->if_snd; + if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) { + register int p = ((int *)ip)[ip->ip_hl]; + + if (INTERACTIVE(p & 0xffff) || INTERACTIVE(p >> 16)) + ifq = &sc->sc_fastq; + + if (sc->sc_flags & SC_COMPRESS) { + /* if two copies of sl_compress_tcp are running + * for the same line, the compression state can + * get screwed up. We're assuming that sloutput + * was invoked at splnet so this isn't possible + * (this assumption is correct for 4.xbsd, x<=4). + * In a multi-threaded kernel, a lockout might + * be needed here. */ + p = sl_compress_tcp(m, ip, &sc->sc_comp); + *mtod(m, u_char *) |= p; + } + } else if (sc->sc_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) { + m_freem(m); + return (0); + } s = splimp(); - if (IF_QFULL(&ifp->if_snd)) { - IF_DROP(&ifp->if_snd); - splx(s); + if (IF_QFULL(ifq)) { + IF_DROP(ifq); m_freem(m); + splx(s); sc->sc_if.if_oerrors++; return (ENOBUFS); } - IF_ENQUEUE(&ifp->if_snd, m); - if ((sc->sc_flags & SC_OACTIVE) == 0) { + IF_ENQUEUE(ifq, m); + if (sc->sc_ttyp->t_outq.c_cc == 0) { splx(s); slstart(sc->sc_ttyp); } else @@ -242,9 +368,8 @@ slstart(tp) { register struct sl_softc *sc = (struct sl_softc *)tp->t_sc; register struct mbuf *m; - register int len; register u_char *cp; - int flush, nd, np, n, s; + int s; struct mbuf *m2; extern int cfreecount; @@ -254,85 +379,92 @@ slstart(tp) * We are being called in lieu of ttstart and must do what * it would. */ - if (tp->t_outq.c_cc > 0) - ttstart(tp); - if (tp->t_outq.c_cc > SLIP_HIWAT) - return; - + if (tp->t_outq.c_cc != 0) { + (*tp->t_oproc)(tp); + if (tp->t_outq.c_cc > SLIP_HIWAT) + return; + } /* * This happens briefly when the line shuts down. */ if (sc == NULL) return; - /* - * If system is getting low on clists - * and we have something running already, stop here. - */ - if (cfreecount < CLISTRESERVE + SLMTU && - sc->sc_flags & SC_OACTIVE) - return; - /* * Get a packet and send it to the interface. */ s = splimp(); - IF_DEQUEUE(&sc->sc_if.if_snd, m); - if (m == NULL) { - if (tp->t_outq.c_cc == 0) - sc->sc_flags &= ~SC_OACTIVE; - splx(s); + IF_DEQUEUE(&sc->sc_fastq, m); + if (m == NULL) + IF_DEQUEUE(&sc->sc_if.if_snd, m); + splx(s); + if (m == NULL) return; + /* + * If system is getting low on clists, just flush our + * output queue (if the stuff was important, it'll get + * retransmitted). + */ + if (cfreecount < CLISTRESERVE + SLMTU) { + m_freem(m); + sc->sc_if.if_collisions++; + continue; } - flush = !(sc->sc_flags & SC_OACTIVE); - sc->sc_flags |= SC_OACTIVE; - splx(s); /* * The extra FRAME_END will start up a new packet, and thus * will flush any accumulated garbage. We do this whenever * the line may have been idle for some time. */ - if (flush) + if (tp->t_outq.c_cc == 0) { + ++sc->sc_bytessent; (void) putc(FRAME_END, &tp->t_outq); + } while (m) { - cp = mtod(m, u_char *); - len = m->m_len; - while (len > 0) { + register u_char *ep; + + cp = mtod(m, u_char *); ep = cp + m->m_len; + while (cp < ep) { /* * Find out how many bytes in the string we can * handle without doing something special. */ - nd = locc(FRAME_ESCAPE, len, cp); - np = locc(FRAME_END, len, cp); - n = len - MAX(nd, np); - if (n) { + register u_char *bp = cp; + + while (cp < ep) { + switch (*cp++) { + case FRAME_ESCAPE: + case FRAME_END: + --cp; + goto out; + } + } + out: + if (cp > bp) { /* * Put n characters at once * into the tty output queue. */ - if (b_to_q((char *)cp, n, &tp->t_outq)) + if (b_to_q((char *)bp, cp - bp, &tp->t_outq)) break; - len -= n; - cp += n; + sc->sc_bytessent += cp - bp; } /* * If there are characters left in the mbuf, * the first one must be special.. * Put it out in a different form. */ - if (len) { + if (cp < ep) { if (putc(FRAME_ESCAPE, &tp->t_outq)) break; - if (putc(*cp == FRAME_ESCAPE ? + if (putc(*cp++ == FRAME_ESCAPE ? TRANS_FRAME_ESCAPE : TRANS_FRAME_END, &tp->t_outq)) { (void) unputc(&tp->t_outq); break; } - cp++; - len--; + sc->sc_bytessent += 2; } } MFREE(m, m2); @@ -350,97 +482,53 @@ slstart(tp) (void) unputc(&tp->t_outq); (void) putc(FRAME_END, &tp->t_outq); sc->sc_if.if_collisions++; - } else - sc->sc_if.if_opackets++; - } -} - -slinit(sc) - register struct sl_softc *sc; -{ - struct mbuf *p; - - if (sc->sc_buf == (char *) 0) { - int s = splimp(); - - MCLALLOC(p, 1); - splx(s); - if (p) { - sc->sc_buf = (char *)p; - sc->sc_mp = sc->sc_buf + sizeof(struct ifnet *); } else { - printf("sl%d: can't allocate buffer\n", sc - sl_softc); - sc->sc_if.if_flags &= ~IFF_UP; - return (0); + ++sc->sc_bytessent; + sc->sc_if.if_opackets++; } } - return (1); } /* - * Copy data buffer to mbuf chain; add ifnet pointer ifp. + * Copy data buffer to mbuf chain; add ifnet pointer. */ -struct mbuf * -sl_btom(sc, len, ifp) - struct sl_softc *sc; +static struct mbuf * +sl_btom(sc, len) + register struct sl_softc *sc; register int len; - struct ifnet *ifp; { - register caddr_t cp; - register struct mbuf *m, **mp; - register unsigned count; - struct mbuf *top = NULL; - - cp = sc->sc_buf + sizeof(struct ifnet *); - mp = ⊤ - while (len > 0) { - MGET(m, M_DONTWAIT, MT_DATA); - if ((*mp = m) == NULL) { - m_freem(top); + register u_char *cp; + register struct mbuf *m; + + MGETHDR(m, M_DONTWAIT, MT_DATA); + if (m == NULL) + return (NULL); + + /* + * If we have more than MLEN bytes, it's cheaper to + * queue the cluster we just filled & allocate a new one + * for the input buffer. Otherwise, fill the mbuf we + * allocated above. Note that code in the input routine + * guarantees that packet will fit in a cluster. + */ + cp = sc->sc_buf; + if (len >= MHLEN) { + MCLGET(m, M_DONTWAIT); + if ((m->m_flags & M_EXT) == 0) { + /* we couldn't get a cluster - if memory's this + * low, it's time to start dropping packets. */ + m_freem(m); return (NULL); } - if (ifp) - m->m_off += sizeof(ifp); - /* - * If we have at least NBPG bytes, - * allocate a new page. Swap the current buffer page - * with the new one. We depend on having a space - * left at the beginning of the buffer - * for the interface pointer. - */ - if (len >= NBPG) { - MCLGET(m); - if (m->m_len == CLBYTES) { - cp = mtod(m, char *); - m->m_off = (int)sc->sc_buf - (int)m; - sc->sc_buf = cp; - if (ifp) { - m->m_off += sizeof(ifp); - count = MIN(len, - CLBYTES - sizeof(struct ifnet *)); - } else - count = MIN(len, CLBYTES); - goto nocopy; - } - } - if (ifp) - count = MIN(len, MLEN - sizeof(ifp)); - else - count = MIN(len, MLEN); - bcopy(cp, mtod(m, caddr_t), count); -nocopy: - m->m_len = count; - if (ifp) { - m->m_off -= sizeof(ifp); - m->m_len += sizeof(ifp); - *mtod(m, struct ifnet **) = ifp; - ifp = NULL; - } - cp += count; - len -= count; - mp = &m->m_next; - } - return (top); + sc->sc_ep = mtod(m, u_char *) + (BUFOFFSET + SLMTU); + m->m_data = (caddr_t)cp; + } else + bcopy((caddr_t)cp, mtod(m, caddr_t), len); + + m->m_len = len; + m->m_pkthdr.len = len; + m->m_pkthdr.rcvif = &sc->sc_if; + return (m); } /* @@ -452,70 +540,105 @@ slinput(c, tp) { register struct sl_softc *sc; register struct mbuf *m; + register int len; int s; tk_nin++; sc = (struct sl_softc *)tp->t_sc; if (sc == NULL) return; + if (!(tp->t_state&TS_CARR_ON)) /* XXX */ + return; - c &= 0xff; - if (sc->sc_flags & SC_ESCAPED) { - sc->sc_flags &= ~SC_ESCAPED; - switch (c) { + ++sc->sc_bytesrcvd; + c &= 0xff; /* XXX */ + +#ifdef ABT_ESC + if (sc->sc_flags & SC_ABORT) { + /* if we see an abort after "idle" time, count it */ + if (c == ABT_ESC && time.tv_sec >= sc->sc_lasttime + ABT_WAIT) { + sc->sc_abortcount++; + /* record when the first abort escape arrived */ + if (sc->sc_abortcount == 1) + sc->sc_starttime = time.tv_sec; + } + /* + * if we have an abort, see that we have not run out of time, + * or that we have an "idle" time after the complete escape + * sequence + */ + if (sc->sc_abortcount) { + if (time.tv_sec >= sc->sc_starttime + ABT_RECYCLE) + sc->sc_abortcount = 0; + if (sc->sc_abortcount >= ABT_SOFT && + time.tv_sec >= sc->sc_lasttime + ABT_WAIT) { + slclose(tp); + return; + } + } + sc->sc_lasttime = time.tv_sec; + } +#endif - case TRANS_FRAME_ESCAPE: + switch (c) { + + case TRANS_FRAME_ESCAPE: + if (sc->sc_escape) c = FRAME_ESCAPE; - break; + break; - case TRANS_FRAME_END: + case TRANS_FRAME_END: + if (sc->sc_escape) c = FRAME_END; - break; + break; - default: - sc->sc_if.if_ierrors++; - sc->sc_mp = sc->sc_buf + sizeof(struct ifnet *); - sc->sc_ilen = 0; - return; - } - } else { - switch (c) { + case FRAME_ESCAPE: + sc->sc_escape = 1; + return; - case FRAME_END: - if (sc->sc_ilen == 0) /* ignore */ - return; - m = sl_btom(sc, sc->sc_ilen, &sc->sc_if); - if (m == NULL) { - sc->sc_if.if_ierrors++; - return; - } - sc->sc_mp = sc->sc_buf + sizeof(struct ifnet *); - sc->sc_ilen = 0; - sc->sc_if.if_ipackets++; - s = splimp(); - if (IF_QFULL(&ipintrq)) { - IF_DROP(&ipintrq); - sc->sc_if.if_ierrors++; - m_freem(m); - } else { - IF_ENQUEUE(&ipintrq, m); - schednetisr(NETISR_IP); - } - splx(s); - return; + case FRAME_END: + len = sc->sc_mp - sc->sc_buf; + if (len < 3) + /* less than min length packet - ignore */ + goto newpack; + + if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) { + if (c & 0x80) + c = TYPE_COMPRESSED_TCP; + else if (c == TYPE_UNCOMPRESSED_TCP) + *sc->sc_buf &= 0x4f; /* XXX */ + len = sl_uncompress_tcp(&sc->sc_buf, len, (u_int)c, + &sc->sc_comp); + if (len <= 0) + goto error; + } + m = sl_btom(sc, len); + if (m == NULL) + goto error; - case FRAME_ESCAPE: - sc->sc_flags |= SC_ESCAPED; - return; + sc->sc_if.if_ipackets++; + s = splimp(); + if (IF_QFULL(&ipintrq)) { + IF_DROP(&ipintrq); + sc->sc_if.if_ierrors++; + m_freem(m); + } else { + IF_ENQUEUE(&ipintrq, m); + schednetisr(NETISR_IP); } + splx(s); + goto newpack; } - if (++sc->sc_ilen > SLMTU) { - sc->sc_if.if_ierrors++; - sc->sc_mp = sc->sc_buf + sizeof(struct ifnet *); - sc->sc_ilen = 0; + if (sc->sc_mp < sc->sc_ep) { + *sc->sc_mp++ = c; + sc->sc_escape = 0; return; } - *sc->sc_mp++ = c; +error: + sc->sc_if.if_ierrors++; +newpack: + sc->sc_mp = sc->sc_buf = sc->sc_ep - SLMTU; + sc->sc_escape = 0; } /* @@ -532,14 +655,14 @@ slioctl(ifp, cmd, data) switch (cmd) { case SIOCSIFADDR: - if (ifa->ifa_addr.sa_family == AF_INET) + if (ifa->ifa_addr->sa_family == AF_INET) ifp->if_flags |= IFF_UP; else error = EAFNOSUPPORT; break; case SIOCSIFDSTADDR: - if (ifa->ifa_addr.sa_family != AF_INET) + if (ifa->ifa_addr->sa_family != AF_INET) error = EAFNOSUPPORT; break;