xref: /freebsd/usr.sbin/ppp/link.c (revision 11afcc8f9f96d657b8e6f7547c02c1957331fc96)
1 /*-
2  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *  $Id: link.c,v 1.2 1998/05/21 21:46:10 brian Exp $
27  *
28  */
29 
30 #include <sys/types.h>
31 
32 #include <stdio.h>
33 #include <string.h>
34 #include <termios.h>
35 
36 #include "mbuf.h"
37 #include "log.h"
38 #include "timer.h"
39 #include "lqr.h"
40 #include "hdlc.h"
41 #include "throughput.h"
42 #include "lcpproto.h"
43 #include "fsm.h"
44 #include "descriptor.h"
45 #include "lcp.h"
46 #include "ccp.h"
47 #include "link.h"
48 #include "prompt.h"
49 
50 void
51 link_AddInOctets(struct link *l, int n)
52 {
53   throughput_addin(&l->throughput, n);
54 }
55 
56 void
57 link_AddOutOctets(struct link *l, int n)
58 {
59   throughput_addout(&l->throughput, n);
60 }
61 
62 void
63 link_SequenceQueue(struct link *l)
64 {
65   log_Printf(LogDEBUG, "link_SequenceQueue\n");
66   while (l->Queue[PRI_NORMAL].qlen)
67     mbuf_Enqueue(l->Queue + PRI_LINK, mbuf_Dequeue(l->Queue + PRI_NORMAL));
68 }
69 
70 int
71 link_QueueLen(struct link *l)
72 {
73   int i, len;
74 
75   for (i = 0, len = 0; i < LINK_QUEUES; i++)
76     len += l->Queue[i].qlen;
77 
78   return len;
79 }
80 
81 int
82 link_QueueBytes(struct link *l)
83 {
84   int i, len, bytes;
85   struct mbuf *m;
86 
87   bytes = 0;
88   for (i = 0, len = 0; i < LINK_QUEUES; i++) {
89     len = l->Queue[i].qlen;
90     m = l->Queue[i].top;
91     while (len--) {
92       bytes += mbuf_Length(m);
93       m = m->pnext;
94     }
95   }
96 
97   return bytes;
98 }
99 
100 struct mbuf *
101 link_Dequeue(struct link *l)
102 {
103   int pri;
104   struct mbuf *bp;
105 
106   for (bp = (struct mbuf *)0, pri = LINK_QUEUES - 1; pri >= 0; pri--)
107     if (l->Queue[pri].qlen) {
108       bp = mbuf_Dequeue(l->Queue + pri);
109       log_Printf(LogDEBUG, "link_Dequeue: Dequeued from queue %d,"
110                 " containing %d more packets\n", pri, l->Queue[pri].qlen);
111       break;
112     }
113 
114   return bp;
115 }
116 
117 /*
118  * Write to the link. Actualy, requested packets are queued, and go out
119  * at some later time depending on the physical link implementation.
120  */
121 void
122 link_Write(struct link *l, int pri, const char *ptr, int count)
123 {
124   struct mbuf *bp;
125 
126   if(pri < 0 || pri >= LINK_QUEUES)
127     pri = 0;
128 
129   bp = mbuf_Alloc(count, MB_LINK);
130   memcpy(MBUF_CTOP(bp), ptr, count);
131 
132   mbuf_Enqueue(l->Queue + pri, bp);
133 }
134 
135 void
136 link_Output(struct link *l, int pri, struct mbuf *bp)
137 {
138   struct mbuf *wp;
139   int len;
140 
141   if(pri < 0 || pri >= LINK_QUEUES)
142     pri = 0;
143 
144   len = mbuf_Length(bp);
145   wp = mbuf_Alloc(len, MB_LINK);
146   mbuf_Read(bp, MBUF_CTOP(wp), len);
147   mbuf_Enqueue(l->Queue + pri, wp);
148 }
149 
150 static struct protostatheader {
151   u_short number;
152   const char *name;
153 } ProtocolStat[NPROTOSTAT] = {
154   { PROTO_IP, "IP" },
155   { PROTO_VJUNCOMP, "VJ_UNCOMP" },
156   { PROTO_VJCOMP, "VJ_COMP" },
157   { PROTO_COMPD, "COMPD" },
158   { PROTO_ICOMPD, "ICOMPD" },
159   { PROTO_LCP, "LCP" },
160   { PROTO_IPCP, "IPCP" },
161   { PROTO_CCP, "CCP" },
162   { PROTO_PAP, "PAP" },
163   { PROTO_LQR, "LQR" },
164   { PROTO_CHAP, "CHAP" },
165   { PROTO_MP, "MULTILINK" },
166   { 0, "Others" }
167 };
168 
169 void
170 link_ProtocolRecord(struct link *l, u_short proto, int type)
171 {
172   int i;
173 
174   for (i = 0; i < NPROTOSTAT; i++)
175     if (ProtocolStat[i].number == proto)
176       break;
177 
178   if (type == PROTO_IN)
179     l->proto_in[i]++;
180   else
181     l->proto_out[i]++;
182 }
183 
184 void
185 link_ReportProtocolStatus(struct link *l, struct prompt *prompt)
186 {
187   int i;
188 
189   prompt_Printf(prompt, "    Protocol     in        out      "
190                 "Protocol      in       out\n");
191   for (i = 0; i < NPROTOSTAT; i++) {
192     prompt_Printf(prompt, "   %-9s: %8lu, %8lu",
193 	    ProtocolStat[i].name, l->proto_in[i], l->proto_out[i]);
194     if ((i % 2) == 0)
195       prompt_Printf(prompt, "\n");
196   }
197   if (!(i % 2))
198     prompt_Printf(prompt, "\n");
199 }
200