xref: /freebsd/usr.sbin/ppp/link.c (revision 2764b86afdc99a30f4b1a4da2c04db8aa7aa785d)
18c07a7b2SBrian Somers /*-
28c07a7b2SBrian Somers  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
38c07a7b2SBrian Somers  * All rights reserved.
48c07a7b2SBrian Somers  *
58c07a7b2SBrian Somers  * Redistribution and use in source and binary forms, with or without
68c07a7b2SBrian Somers  * modification, are permitted provided that the following conditions
78c07a7b2SBrian Somers  * are met:
88c07a7b2SBrian Somers  * 1. Redistributions of source code must retain the above copyright
98c07a7b2SBrian Somers  *    notice, this list of conditions and the following disclaimer.
108c07a7b2SBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
118c07a7b2SBrian Somers  *    notice, this list of conditions and the following disclaimer in the
128c07a7b2SBrian Somers  *    documentation and/or other materials provided with the distribution.
138c07a7b2SBrian Somers  *
148c07a7b2SBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
158c07a7b2SBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
168c07a7b2SBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
178c07a7b2SBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
188c07a7b2SBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
198c07a7b2SBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
208c07a7b2SBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
218c07a7b2SBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
228c07a7b2SBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
238c07a7b2SBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
248c07a7b2SBrian Somers  * SUCH DAMAGE.
258c07a7b2SBrian Somers  *
262764b86aSBrian Somers  *  $Id: link.c,v 1.1.2.17 1998/04/06 09:12:31 brian Exp $
278c07a7b2SBrian Somers  *
288c07a7b2SBrian Somers  */
298c07a7b2SBrian Somers 
302764b86aSBrian Somers #include <sys/types.h>
318c07a7b2SBrian Somers 
328c07a7b2SBrian Somers #include <stdio.h>
3385b542cfSBrian Somers #include <termios.h>
348c07a7b2SBrian Somers 
358c07a7b2SBrian Somers #include "mbuf.h"
368c07a7b2SBrian Somers #include "log.h"
378c07a7b2SBrian Somers #include "timer.h"
38879ed6faSBrian Somers #include "lqr.h"
3963258dccSBrian Somers #include "hdlc.h"
408c07a7b2SBrian Somers #include "throughput.h"
418c07a7b2SBrian Somers #include "lcpproto.h"
426d666775SBrian Somers #include "fsm.h"
4385b542cfSBrian Somers #include "descriptor.h"
443b0f8d2eSBrian Somers #include "lcp.h"
453b0f8d2eSBrian Somers #include "ccp.h"
463b0f8d2eSBrian Somers #include "link.h"
4785b542cfSBrian Somers #include "prompt.h"
488c07a7b2SBrian Somers 
498c07a7b2SBrian Somers void
508c07a7b2SBrian Somers link_AddInOctets(struct link *l, int n)
518c07a7b2SBrian Somers {
528c07a7b2SBrian Somers   throughput_addin(&l->throughput, n);
538c07a7b2SBrian Somers }
548c07a7b2SBrian Somers 
558c07a7b2SBrian Somers void
568c07a7b2SBrian Somers link_AddOutOctets(struct link *l, int n)
578c07a7b2SBrian Somers {
588c07a7b2SBrian Somers   throughput_addout(&l->throughput, n);
598c07a7b2SBrian Somers }
608c07a7b2SBrian Somers 
618c07a7b2SBrian Somers void
628c07a7b2SBrian Somers link_SequenceQueue(struct link *l)
638c07a7b2SBrian Somers {
648c07a7b2SBrian Somers   LogPrintf(LogDEBUG, "link_SequenceQueue\n");
658c07a7b2SBrian Somers   while (l->Queue[PRI_NORMAL].qlen)
668c07a7b2SBrian Somers     Enqueue(l->Queue + PRI_LINK, Dequeue(l->Queue + PRI_NORMAL));
678c07a7b2SBrian Somers }
688c07a7b2SBrian Somers 
698c07a7b2SBrian Somers int
708c07a7b2SBrian Somers link_QueueLen(struct link *l)
718c07a7b2SBrian Somers {
728c07a7b2SBrian Somers   int i, len;
738c07a7b2SBrian Somers 
748c07a7b2SBrian Somers   for (i = 0, len = 0; i < LINK_QUEUES; i++)
758c07a7b2SBrian Somers     len += l->Queue[i].qlen;
768c07a7b2SBrian Somers 
778c07a7b2SBrian Somers   return len;
788c07a7b2SBrian Somers }
798c07a7b2SBrian Somers 
803b0f8d2eSBrian Somers int
813b0f8d2eSBrian Somers link_QueueBytes(struct link *l)
823b0f8d2eSBrian Somers {
833b0f8d2eSBrian Somers   int i, len, bytes;
843b0f8d2eSBrian Somers   struct mbuf *m;
853b0f8d2eSBrian Somers 
863b0f8d2eSBrian Somers   bytes = 0;
873b0f8d2eSBrian Somers   for (i = 0, len = 0; i < LINK_QUEUES; i++) {
883b0f8d2eSBrian Somers     len = l->Queue[i].qlen;
893b0f8d2eSBrian Somers     m = l->Queue[i].top;
903b0f8d2eSBrian Somers     while (len--) {
913b0f8d2eSBrian Somers       bytes += plength(m);
923b0f8d2eSBrian Somers       m = m->pnext;
933b0f8d2eSBrian Somers     }
943b0f8d2eSBrian Somers   }
953b0f8d2eSBrian Somers 
963b0f8d2eSBrian Somers   return bytes;
973b0f8d2eSBrian Somers }
983b0f8d2eSBrian Somers 
998c07a7b2SBrian Somers struct mbuf *
1008c07a7b2SBrian Somers link_Dequeue(struct link *l)
1018c07a7b2SBrian Somers {
1028c07a7b2SBrian Somers   int pri;
1038c07a7b2SBrian Somers   struct mbuf *bp;
1048c07a7b2SBrian Somers 
1058c07a7b2SBrian Somers   for (bp = (struct mbuf *)0, pri = LINK_QUEUES - 1; pri >= 0; pri--)
1068c07a7b2SBrian Somers     if (l->Queue[pri].qlen) {
1078c07a7b2SBrian Somers       bp = Dequeue(l->Queue + pri);
1082289f246SBrian Somers       LogPrintf(LogDEBUG, "link_Dequeue: Dequeued from queue %d,"
1092289f246SBrian Somers                 " containing %d more packets\n", pri, l->Queue[pri].qlen);
1108c07a7b2SBrian Somers       break;
1118c07a7b2SBrian Somers     }
1128c07a7b2SBrian Somers 
1138c07a7b2SBrian Somers   return bp;
1148c07a7b2SBrian Somers }
1158c07a7b2SBrian Somers 
1168c07a7b2SBrian Somers /*
1178c07a7b2SBrian Somers  * Write to the link. Actualy, requested packets are queued, and go out
1188c07a7b2SBrian Somers  * at some later time depending on the physical link implementation.
1198c07a7b2SBrian Somers  */
1208c07a7b2SBrian Somers void
1218c07a7b2SBrian Somers link_Write(struct link *l, int pri, const char *ptr, int count)
1228c07a7b2SBrian Somers {
1238c07a7b2SBrian Somers   struct mbuf *bp;
1248c07a7b2SBrian Somers 
1258c07a7b2SBrian Somers   if(pri < 0 || pri >= LINK_QUEUES)
1268c07a7b2SBrian Somers     pri = 0;
1278c07a7b2SBrian Somers 
1288c07a7b2SBrian Somers   bp = mballoc(count, MB_LINK);
1298c07a7b2SBrian Somers   memcpy(MBUF_CTOP(bp), ptr, count);
1308c07a7b2SBrian Somers 
1318c07a7b2SBrian Somers   Enqueue(l->Queue + pri, bp);
1328c07a7b2SBrian Somers }
1338c07a7b2SBrian Somers 
1348c07a7b2SBrian Somers void
1358c07a7b2SBrian Somers link_Output(struct link *l, int pri, struct mbuf *bp)
1368c07a7b2SBrian Somers {
1378c07a7b2SBrian Somers   struct mbuf *wp;
1388c07a7b2SBrian Somers   int len;
1398c07a7b2SBrian Somers 
1408c07a7b2SBrian Somers   if(pri < 0 || pri >= LINK_QUEUES)
1418c07a7b2SBrian Somers     pri = 0;
1428c07a7b2SBrian Somers 
1438c07a7b2SBrian Somers   len = plength(bp);
1448c07a7b2SBrian Somers   wp = mballoc(len, MB_LINK);
1458c07a7b2SBrian Somers   mbread(bp, MBUF_CTOP(wp), len);
1468c07a7b2SBrian Somers   Enqueue(l->Queue + pri, wp);
1478c07a7b2SBrian Somers }
1488c07a7b2SBrian Somers 
1498c07a7b2SBrian Somers static struct protostatheader {
1508c07a7b2SBrian Somers   u_short number;
1518c07a7b2SBrian Somers   const char *name;
1528c07a7b2SBrian Somers } ProtocolStat[NPROTOSTAT] = {
1538c07a7b2SBrian Somers   { PROTO_IP, "IP" },
1548c07a7b2SBrian Somers   { PROTO_VJUNCOMP, "VJ_UNCOMP" },
1558c07a7b2SBrian Somers   { PROTO_VJCOMP, "VJ_COMP" },
1568c07a7b2SBrian Somers   { PROTO_COMPD, "COMPD" },
1578c07a7b2SBrian Somers   { PROTO_LCP, "LCP" },
1588c07a7b2SBrian Somers   { PROTO_IPCP, "IPCP" },
1598c07a7b2SBrian Somers   { PROTO_CCP, "CCP" },
1608c07a7b2SBrian Somers   { PROTO_PAP, "PAP" },
1618c07a7b2SBrian Somers   { PROTO_LQR, "LQR" },
1628c07a7b2SBrian Somers   { PROTO_CHAP, "CHAP" },
1633b0f8d2eSBrian Somers   { PROTO_MP, "MULTILINK" },
1648c07a7b2SBrian Somers   { 0, "Others" }
1658c07a7b2SBrian Somers };
1668c07a7b2SBrian Somers 
1678c07a7b2SBrian Somers void
1688c07a7b2SBrian Somers link_ProtocolRecord(struct link *l, u_short proto, int type)
1698c07a7b2SBrian Somers {
1708c07a7b2SBrian Somers   int i;
1718c07a7b2SBrian Somers 
1728c07a7b2SBrian Somers   for (i = 0; i < NPROTOSTAT; i++)
1738c07a7b2SBrian Somers     if (ProtocolStat[i].number == proto)
1748c07a7b2SBrian Somers       break;
1758c07a7b2SBrian Somers 
1768c07a7b2SBrian Somers   if (type == PROTO_IN)
1778c07a7b2SBrian Somers     l->proto_in[i]++;
1788c07a7b2SBrian Somers   else
1798c07a7b2SBrian Somers     l->proto_out[i]++;
1808c07a7b2SBrian Somers }
1818c07a7b2SBrian Somers 
1828c07a7b2SBrian Somers void
183b6217683SBrian Somers link_ReportProtocolStatus(struct link *l, struct prompt *prompt)
1848c07a7b2SBrian Somers {
1858c07a7b2SBrian Somers   int i;
1868c07a7b2SBrian Somers 
187b6217683SBrian Somers   prompt_Printf(prompt, "    Protocol     in        out      "
18885b542cfSBrian Somers                 "Protocol      in       out\n");
1898c07a7b2SBrian Somers   for (i = 0; i < NPROTOSTAT; i++) {
190b6217683SBrian Somers     prompt_Printf(prompt, "   %-9s: %8lu, %8lu",
1918c07a7b2SBrian Somers 	    ProtocolStat[i].name, l->proto_in[i], l->proto_out[i]);
1928c07a7b2SBrian Somers     if ((i % 2) == 0)
193b6217683SBrian Somers       prompt_Printf(prompt, "\n");
1948c07a7b2SBrian Somers   }
1953b0f8d2eSBrian Somers   if (!(i % 2))
196b6217683SBrian Somers     prompt_Printf(prompt, "\n");
1978c07a7b2SBrian Somers }
198