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 * 2692b09558SBrian Somers * $Id: link.c,v 1.3 1998/06/27 23:48:49 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 3592b09558SBrian Somers #include "defs.h" 368c07a7b2SBrian Somers #include "mbuf.h" 378c07a7b2SBrian Somers #include "log.h" 388c07a7b2SBrian Somers #include "timer.h" 39879ed6faSBrian Somers #include "lqr.h" 4063258dccSBrian Somers #include "hdlc.h" 418c07a7b2SBrian Somers #include "throughput.h" 428c07a7b2SBrian Somers #include "lcpproto.h" 436d666775SBrian Somers #include "fsm.h" 4485b542cfSBrian Somers #include "descriptor.h" 453b0f8d2eSBrian Somers #include "lcp.h" 463b0f8d2eSBrian Somers #include "ccp.h" 473b0f8d2eSBrian Somers #include "link.h" 4885b542cfSBrian Somers #include "prompt.h" 498c07a7b2SBrian Somers 508c07a7b2SBrian Somers void 518c07a7b2SBrian Somers link_AddInOctets(struct link *l, int n) 528c07a7b2SBrian Somers { 538c07a7b2SBrian Somers throughput_addin(&l->throughput, n); 548c07a7b2SBrian Somers } 558c07a7b2SBrian Somers 568c07a7b2SBrian Somers void 578c07a7b2SBrian Somers link_AddOutOctets(struct link *l, int n) 588c07a7b2SBrian Somers { 598c07a7b2SBrian Somers throughput_addout(&l->throughput, n); 608c07a7b2SBrian Somers } 618c07a7b2SBrian Somers 628c07a7b2SBrian Somers void 638c07a7b2SBrian Somers link_SequenceQueue(struct link *l) 648c07a7b2SBrian Somers { 65dd7e2610SBrian Somers log_Printf(LogDEBUG, "link_SequenceQueue\n"); 668c07a7b2SBrian Somers while (l->Queue[PRI_NORMAL].qlen) 67dd7e2610SBrian Somers mbuf_Enqueue(l->Queue + PRI_LINK, mbuf_Dequeue(l->Queue + PRI_NORMAL)); 688c07a7b2SBrian Somers } 698c07a7b2SBrian Somers 708c07a7b2SBrian Somers int 718c07a7b2SBrian Somers link_QueueLen(struct link *l) 728c07a7b2SBrian Somers { 738c07a7b2SBrian Somers int i, len; 748c07a7b2SBrian Somers 758c07a7b2SBrian Somers for (i = 0, len = 0; i < LINK_QUEUES; i++) 768c07a7b2SBrian Somers len += l->Queue[i].qlen; 778c07a7b2SBrian Somers 788c07a7b2SBrian Somers return len; 798c07a7b2SBrian Somers } 808c07a7b2SBrian Somers 813b0f8d2eSBrian Somers int 823b0f8d2eSBrian Somers link_QueueBytes(struct link *l) 833b0f8d2eSBrian Somers { 843b0f8d2eSBrian Somers int i, len, bytes; 853b0f8d2eSBrian Somers struct mbuf *m; 863b0f8d2eSBrian Somers 873b0f8d2eSBrian Somers bytes = 0; 883b0f8d2eSBrian Somers for (i = 0, len = 0; i < LINK_QUEUES; i++) { 893b0f8d2eSBrian Somers len = l->Queue[i].qlen; 903b0f8d2eSBrian Somers m = l->Queue[i].top; 913b0f8d2eSBrian Somers while (len--) { 92dd7e2610SBrian Somers bytes += mbuf_Length(m); 933b0f8d2eSBrian Somers m = m->pnext; 943b0f8d2eSBrian Somers } 953b0f8d2eSBrian Somers } 963b0f8d2eSBrian Somers 973b0f8d2eSBrian Somers return bytes; 983b0f8d2eSBrian Somers } 993b0f8d2eSBrian Somers 1008c07a7b2SBrian Somers struct mbuf * 1018c07a7b2SBrian Somers link_Dequeue(struct link *l) 1028c07a7b2SBrian Somers { 1038c07a7b2SBrian Somers int pri; 1048c07a7b2SBrian Somers struct mbuf *bp; 1058c07a7b2SBrian Somers 1068c07a7b2SBrian Somers for (bp = (struct mbuf *)0, pri = LINK_QUEUES - 1; pri >= 0; pri--) 1078c07a7b2SBrian Somers if (l->Queue[pri].qlen) { 108dd7e2610SBrian Somers bp = mbuf_Dequeue(l->Queue + pri); 109dd7e2610SBrian Somers log_Printf(LogDEBUG, "link_Dequeue: Dequeued from queue %d," 1102289f246SBrian Somers " containing %d more packets\n", pri, l->Queue[pri].qlen); 1118c07a7b2SBrian Somers break; 1128c07a7b2SBrian Somers } 1138c07a7b2SBrian Somers 1148c07a7b2SBrian Somers return bp; 1158c07a7b2SBrian Somers } 1168c07a7b2SBrian Somers 1178c07a7b2SBrian Somers /* 1188c07a7b2SBrian Somers * Write to the link. Actualy, requested packets are queued, and go out 1198c07a7b2SBrian Somers * at some later time depending on the physical link implementation. 1208c07a7b2SBrian Somers */ 1218c07a7b2SBrian Somers void 1228c07a7b2SBrian Somers link_Write(struct link *l, int pri, const char *ptr, int count) 1238c07a7b2SBrian Somers { 1248c07a7b2SBrian Somers struct mbuf *bp; 1258c07a7b2SBrian Somers 1268c07a7b2SBrian Somers if(pri < 0 || pri >= LINK_QUEUES) 1278c07a7b2SBrian Somers pri = 0; 1288c07a7b2SBrian Somers 129dd7e2610SBrian Somers bp = mbuf_Alloc(count, MB_LINK); 1308c07a7b2SBrian Somers memcpy(MBUF_CTOP(bp), ptr, count); 1318c07a7b2SBrian Somers 132dd7e2610SBrian Somers mbuf_Enqueue(l->Queue + pri, bp); 1338c07a7b2SBrian Somers } 1348c07a7b2SBrian Somers 1358c07a7b2SBrian Somers void 1368c07a7b2SBrian Somers link_Output(struct link *l, int pri, struct mbuf *bp) 1378c07a7b2SBrian Somers { 1388c07a7b2SBrian Somers struct mbuf *wp; 1398c07a7b2SBrian Somers int len; 1408c07a7b2SBrian Somers 1418c07a7b2SBrian Somers if(pri < 0 || pri >= LINK_QUEUES) 1428c07a7b2SBrian Somers pri = 0; 1438c07a7b2SBrian Somers 144dd7e2610SBrian Somers len = mbuf_Length(bp); 145dd7e2610SBrian Somers wp = mbuf_Alloc(len, MB_LINK); 146dd7e2610SBrian Somers mbuf_Read(bp, MBUF_CTOP(wp), len); 147dd7e2610SBrian Somers mbuf_Enqueue(l->Queue + pri, wp); 1488c07a7b2SBrian Somers } 1498c07a7b2SBrian Somers 1508c07a7b2SBrian Somers static struct protostatheader { 1518c07a7b2SBrian Somers u_short number; 1528c07a7b2SBrian Somers const char *name; 1538c07a7b2SBrian Somers } ProtocolStat[NPROTOSTAT] = { 1548c07a7b2SBrian Somers { PROTO_IP, "IP" }, 1558c07a7b2SBrian Somers { PROTO_VJUNCOMP, "VJ_UNCOMP" }, 1568c07a7b2SBrian Somers { PROTO_VJCOMP, "VJ_COMP" }, 1578c07a7b2SBrian Somers { PROTO_COMPD, "COMPD" }, 158ed32233cSBrian Somers { PROTO_ICOMPD, "ICOMPD" }, 1598c07a7b2SBrian Somers { PROTO_LCP, "LCP" }, 1608c07a7b2SBrian Somers { PROTO_IPCP, "IPCP" }, 1618c07a7b2SBrian Somers { PROTO_CCP, "CCP" }, 1628c07a7b2SBrian Somers { PROTO_PAP, "PAP" }, 1638c07a7b2SBrian Somers { PROTO_LQR, "LQR" }, 1648c07a7b2SBrian Somers { PROTO_CHAP, "CHAP" }, 1653b0f8d2eSBrian Somers { PROTO_MP, "MULTILINK" }, 1668c07a7b2SBrian Somers { 0, "Others" } 1678c07a7b2SBrian Somers }; 1688c07a7b2SBrian Somers 1698c07a7b2SBrian Somers void 1708c07a7b2SBrian Somers link_ProtocolRecord(struct link *l, u_short proto, int type) 1718c07a7b2SBrian Somers { 1728c07a7b2SBrian Somers int i; 1738c07a7b2SBrian Somers 1748c07a7b2SBrian Somers for (i = 0; i < NPROTOSTAT; i++) 1758c07a7b2SBrian Somers if (ProtocolStat[i].number == proto) 1768c07a7b2SBrian Somers break; 1778c07a7b2SBrian Somers 1788c07a7b2SBrian Somers if (type == PROTO_IN) 1798c07a7b2SBrian Somers l->proto_in[i]++; 1808c07a7b2SBrian Somers else 1818c07a7b2SBrian Somers l->proto_out[i]++; 1828c07a7b2SBrian Somers } 1838c07a7b2SBrian Somers 1848c07a7b2SBrian Somers void 185b6217683SBrian Somers link_ReportProtocolStatus(struct link *l, struct prompt *prompt) 1868c07a7b2SBrian Somers { 1878c07a7b2SBrian Somers int i; 1888c07a7b2SBrian Somers 189b6217683SBrian Somers prompt_Printf(prompt, " Protocol in out " 19085b542cfSBrian Somers "Protocol in out\n"); 1918c07a7b2SBrian Somers for (i = 0; i < NPROTOSTAT; i++) { 192b6217683SBrian Somers prompt_Printf(prompt, " %-9s: %8lu, %8lu", 1938c07a7b2SBrian Somers ProtocolStat[i].name, l->proto_in[i], l->proto_out[i]); 1948c07a7b2SBrian Somers if ((i % 2) == 0) 195b6217683SBrian Somers prompt_Printf(prompt, "\n"); 1968c07a7b2SBrian Somers } 1973b0f8d2eSBrian Somers if (!(i % 2)) 198b6217683SBrian Somers prompt_Printf(prompt, "\n"); 1998c07a7b2SBrian Somers } 200