link.c revision 38544
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.4 1998/08/07 18:42:49 brian Exp $
27 *
28 */
29
30#include <sys/types.h>
31
32#include <stdio.h>
33#include <termios.h>
34
35#include "defs.h"
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
50void
51link_AddInOctets(struct link *l, int n)
52{
53  throughput_addin(&l->throughput, n);
54}
55
56void
57link_AddOutOctets(struct link *l, int n)
58{
59  throughput_addout(&l->throughput, n);
60}
61
62void
63link_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
70void
71link_DeleteQueue(struct link *l)
72{
73  struct mqueue *queue;
74
75  for (queue = l->Queue; queue < l->Queue + LINK_QUEUES; queue++)
76    while (queue->top)
77      mbuf_Free(mbuf_Dequeue(queue));
78}
79
80int
81link_QueueLen(struct link *l)
82{
83  int i, len;
84
85  for (i = 0, len = 0; i < LINK_QUEUES; i++)
86    len += l->Queue[i].qlen;
87
88  return len;
89}
90
91int
92link_QueueBytes(struct link *l)
93{
94  int i, len, bytes;
95  struct mbuf *m;
96
97  bytes = 0;
98  for (i = 0, len = 0; i < LINK_QUEUES; i++) {
99    len = l->Queue[i].qlen;
100    m = l->Queue[i].top;
101    while (len--) {
102      bytes += mbuf_Length(m);
103      m = m->pnext;
104    }
105  }
106
107  return bytes;
108}
109
110struct mbuf *
111link_Dequeue(struct link *l)
112{
113  int pri;
114  struct mbuf *bp;
115
116  for (bp = (struct mbuf *)0, pri = LINK_QUEUES - 1; pri >= 0; pri--)
117    if (l->Queue[pri].qlen) {
118      bp = mbuf_Dequeue(l->Queue + pri);
119      log_Printf(LogDEBUG, "link_Dequeue: Dequeued from queue %d,"
120                " containing %d more packets\n", pri, l->Queue[pri].qlen);
121      break;
122    }
123
124  return bp;
125}
126
127/*
128 * Write to the link. Actualy, requested packets are queued, and go out
129 * at some later time depending on the physical link implementation.
130 */
131void
132link_Write(struct link *l, int pri, const char *ptr, int count)
133{
134  struct mbuf *bp;
135
136  if(pri < 0 || pri >= LINK_QUEUES)
137    pri = 0;
138
139  bp = mbuf_Alloc(count, MB_LINK);
140  memcpy(MBUF_CTOP(bp), ptr, count);
141
142  mbuf_Enqueue(l->Queue + pri, bp);
143}
144
145void
146link_Output(struct link *l, int pri, struct mbuf *bp)
147{
148  struct mbuf *wp;
149  int len;
150
151  if(pri < 0 || pri >= LINK_QUEUES)
152    pri = 0;
153
154  len = mbuf_Length(bp);
155  wp = mbuf_Alloc(len, MB_LINK);
156  mbuf_Read(bp, MBUF_CTOP(wp), len);
157  mbuf_Enqueue(l->Queue + pri, wp);
158}
159
160static struct protostatheader {
161  u_short number;
162  const char *name;
163} ProtocolStat[NPROTOSTAT] = {
164  { PROTO_IP, "IP" },
165  { PROTO_VJUNCOMP, "VJ_UNCOMP" },
166  { PROTO_VJCOMP, "VJ_COMP" },
167  { PROTO_COMPD, "COMPD" },
168  { PROTO_ICOMPD, "ICOMPD" },
169  { PROTO_LCP, "LCP" },
170  { PROTO_IPCP, "IPCP" },
171  { PROTO_CCP, "CCP" },
172  { PROTO_PAP, "PAP" },
173  { PROTO_LQR, "LQR" },
174  { PROTO_CHAP, "CHAP" },
175  { PROTO_MP, "MULTILINK" },
176  { 0, "Others" }
177};
178
179void
180link_ProtocolRecord(struct link *l, u_short proto, int type)
181{
182  int i;
183
184  for (i = 0; i < NPROTOSTAT; i++)
185    if (ProtocolStat[i].number == proto)
186      break;
187
188  if (type == PROTO_IN)
189    l->proto_in[i]++;
190  else
191    l->proto_out[i]++;
192}
193
194void
195link_ReportProtocolStatus(struct link *l, struct prompt *prompt)
196{
197  int i;
198
199  prompt_Printf(prompt, "    Protocol     in        out      "
200                "Protocol      in       out\n");
201  for (i = 0; i < NPROTOSTAT; i++) {
202    prompt_Printf(prompt, "   %-9s: %8lu, %8lu",
203	    ProtocolStat[i].name, l->proto_in[i], l->proto_out[i]);
204    if ((i % 2) == 0)
205      prompt_Printf(prompt, "\n");
206  }
207  if (!(i % 2))
208    prompt_Printf(prompt, "\n");
209}
210