2 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4 * Internet Initiative Japan, Inc (IIJ)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
34 #include <netinet/in.h>
53 #include "throughput.h"
56 #include "descriptor.h"
72 #define SIGNATURE 0x594e4f54
75 SendEchoReq(struct lcp *lcp)
77 struct hdlc *hdlc = &link2physical(lcp->fsm.link)->hdlc;
80 echo.magic = htonl(lcp->want_magic);
81 echo.signature = htonl(SIGNATURE);
82 echo.sequence = htonl(hdlc->lqm.echo.seq_sent);
83 fsm_Output(&lcp->fsm, CODE_ECHOREQ, hdlc->lqm.echo.seq_sent++,
84 (u_char *)&echo, sizeof echo, MB_ECHOOUT);
88 lqr_RecvEcho(struct fsm *fp, struct mbuf *bp)
90 struct hdlc *hdlc = &link2physical(fp->link)->hdlc;
91 struct lcp *lcp = fsm2lcp(fp);
94 if (m_length(bp) >= sizeof lqr) {
95 m_freem(mbuf_Read(bp, &lqr, sizeof lqr));
97 lqr.magic = ntohl(lqr.magic);
98 lqr.signature = ntohl(lqr.signature);
99 lqr.sequence = ntohl(lqr.sequence);
101 /* Tolerate echo replies with either magic number */
102 if (lqr.magic != 0 && lqr.magic != lcp->his_magic &&
103 lqr.magic != lcp->want_magic) {
104 log_Printf(LogWARN, "%s: lqr_RecvEcho: Bad magic: expected 0x%08x,"
105 " got 0x%08x\n", fp->link->name, lcp->his_magic, lqr.magic);
107 * XXX: We should send a terminate request. But poor implementations may
111 if (lqr.signature == SIGNATURE) {
112 /* careful not to update lqm.echo.seq_recv with older values */
113 if ((hdlc->lqm.echo.seq_recv > (u_int32_t)0 - 5 && lqr.sequence < 5) ||
114 (hdlc->lqm.echo.seq_recv <= (u_int32_t)0 - 5 &&
115 lqr.sequence > hdlc->lqm.echo.seq_recv))
116 hdlc->lqm.echo.seq_recv = lqr.sequence;
118 log_Printf(LogWARN, "lqr_RecvEcho: Got sig 0x%08lx, not 0x%08lx !\n",
119 (u_long)lqr.signature, (u_long)SIGNATURE);
121 log_Printf(LogWARN, "lqr_RecvEcho: Got packet size %zd, expecting %ld !\n",
122 m_length(bp), (long)sizeof(struct echolqr));
127 lqr_ChangeOrder(struct lqrdata *src, struct lqrdata *dst)
132 sp = (u_int32_t *) src;
133 dp = (u_int32_t *) dst;
134 for (n = 0; n < sizeof(struct lqrdata) / sizeof(u_int32_t); n++, sp++, dp++)
139 SendLqrData(struct lcp *lcp)
144 extra = proto_WrapperOctets(lcp, PROTO_LQR) +
145 acf_WrapperOctets(lcp, PROTO_LQR);
146 bp = m_get(sizeof(struct lqrdata) + extra, MB_LQROUT);
148 bp->m_offset += extra;
151 * Send on the highest priority queue. We send garbage - the real data
152 * is written by lqr_LayerPush() where we know how to fill in all the
153 * fields. Note, lqr_LayerPush() ``knows'' that we're pushing onto the
154 * highest priority queue, and factors out packet & octet values from
157 link_PushPacket(lcp->fsm.link, bp, lcp->fsm.bundle,
158 LINK_QUEUES(lcp->fsm.link) - 1, PROTO_LQR);
162 SendLqrReport(void *v)
164 struct lcp *lcp = (struct lcp *)v;
165 struct physical *p = link2physical(lcp->fsm.link);
167 timer_Stop(&p->hdlc.lqm.timer);
169 if (p->hdlc.lqm.method & LQM_LQR) {
170 if (p->hdlc.lqm.lqr.resent > 5) {
171 /* XXX: Should implement LQM strategy */
172 log_Printf(LogPHASE, "%s: ** Too many LQR packets lost **\n",
173 lcp->fsm.link->name);
174 log_Printf(LogLQM, "%s: Too many LQR packets lost\n",
175 lcp->fsm.link->name);
176 p->hdlc.lqm.method = 0;
177 datalink_Down(p->dl, CLOSE_NORMAL);
180 p->hdlc.lqm.lqr.resent++;
182 } else if (p->hdlc.lqm.method & LQM_ECHO) {
183 if ((p->hdlc.lqm.echo.seq_sent > 5 &&
184 p->hdlc.lqm.echo.seq_sent - 5 > p->hdlc.lqm.echo.seq_recv) ||
185 (p->hdlc.lqm.echo.seq_sent <= 5 &&
186 p->hdlc.lqm.echo.seq_sent > p->hdlc.lqm.echo.seq_recv + 5)) {
187 log_Printf(LogPHASE, "%s: ** Too many LCP ECHO packets lost **\n",
188 lcp->fsm.link->name);
189 log_Printf(LogLQM, "%s: Too many LCP ECHO packets lost\n",
190 lcp->fsm.link->name);
191 p->hdlc.lqm.method = 0;
192 datalink_Down(p->dl, CLOSE_NORMAL);
196 if (p->hdlc.lqm.method && p->hdlc.lqm.timer.load)
197 timer_Start(&p->hdlc.lqm.timer);
201 lqr_Input(struct bundle *bundle __unused, struct link *l, struct mbuf *bp)
203 struct physical *p = link2physical(l);
204 struct lcp *lcp = p->hdlc.lqm.owner;
208 log_Printf(LogERROR, "lqr_Input: Not a physical link - dropped\n");
214 if (len != sizeof(struct lqrdata))
215 log_Printf(LogWARN, "lqr_Input: Got packet size %d, expecting %ld !\n",
216 len, (long)sizeof(struct lqrdata));
217 else if (!IsAccepted(l->lcp.cfg.lqr) && !(p->hdlc.lqm.method & LQM_LQR)) {
218 bp = m_pullup(proto_Prepend(bp, PROTO_LQR, 0, 0));
219 lcp_SendProtoRej(lcp, MBUF_CTOP(bp), bp->m_len);
224 lqr = (struct lqrdata *)MBUF_CTOP(bp);
225 if (ntohl(lqr->MagicNumber) != lcp->his_magic)
226 log_Printf(LogWARN, "lqr_Input: magic 0x%08lx is wrong,"
227 " expecting 0x%08lx\n",
228 (u_long)ntohl(lqr->MagicNumber), (u_long)lcp->his_magic);
230 struct lqrdata lastlqr;
232 memcpy(&lastlqr, &p->hdlc.lqm.lqr.peer, sizeof lastlqr);
233 lqr_ChangeOrder(lqr, &p->hdlc.lqm.lqr.peer);
234 lqr_Dump(l->name, "Input", &p->hdlc.lqm.lqr.peer);
235 /* we have received an LQR from our peer */
236 p->hdlc.lqm.lqr.resent = 0;
238 /* Snapshot our state when the LQR packet was received */
239 memcpy(&p->hdlc.lqm.lqr.prevSave, &p->hdlc.lqm.lqr.Save,
240 sizeof p->hdlc.lqm.lqr.prevSave);
241 p->hdlc.lqm.lqr.Save.InLQRs = ++p->hdlc.lqm.lqr.InLQRs;
242 p->hdlc.lqm.lqr.Save.InPackets = p->hdlc.lqm.ifInUniPackets;
243 p->hdlc.lqm.lqr.Save.InDiscards = p->hdlc.lqm.ifInDiscards;
244 p->hdlc.lqm.lqr.Save.InErrors = p->hdlc.lqm.ifInErrors;
245 p->hdlc.lqm.lqr.Save.InOctets = p->hdlc.lqm.lqr.InGoodOctets;
247 lqr_Analyse(&p->hdlc, &lastlqr, &p->hdlc.lqm.lqr.peer);
250 * Generate an LQR response if we're not running an LQR timer OR
251 * two successive LQR's PeerInLQRs are the same.
253 if (p->hdlc.lqm.timer.load == 0 || !(p->hdlc.lqm.method & LQM_LQR) ||
254 (lastlqr.PeerInLQRs &&
255 lastlqr.PeerInLQRs == p->hdlc.lqm.lqr.peer.PeerInLQRs))
264 * When LCP is reached to opened state, We'll start LQM activity.
267 lqr_Setup(struct lcp *lcp)
269 struct physical *physical = link2physical(lcp->fsm.link);
272 physical->hdlc.lqm.lqr.resent = 0;
273 physical->hdlc.lqm.echo.seq_sent = 0;
274 physical->hdlc.lqm.echo.seq_recv = 0;
275 memset(&physical->hdlc.lqm.lqr.peer, '\0',
276 sizeof physical->hdlc.lqm.lqr.peer);
278 physical->hdlc.lqm.method = lcp->cfg.echo ? LQM_ECHO : 0;
279 if (IsEnabled(lcp->cfg.lqr) && !REJECTED(lcp, TY_QUALPROTO))
280 physical->hdlc.lqm.method |= LQM_LQR;
281 timer_Stop(&physical->hdlc.lqm.timer);
283 physical->hdlc.lqm.lqr.peer_timeout = lcp->his_lqrperiod;
284 if (lcp->his_lqrperiod)
285 log_Printf(LogLQM, "%s: Expecting LQR every %d.%02d secs\n",
286 physical->link.name, lcp->his_lqrperiod / 100,
287 lcp->his_lqrperiod % 100);
289 period = lcp->want_lqrperiod ?
290 lcp->want_lqrperiod : lcp->cfg.lqrperiod * 100;
291 physical->hdlc.lqm.timer.func = SendLqrReport;
292 physical->hdlc.lqm.timer.name = "lqm";
293 physical->hdlc.lqm.timer.arg = lcp;
295 if (lcp->want_lqrperiod || physical->hdlc.lqm.method & LQM_ECHO) {
296 log_Printf(LogLQM, "%s: Will send %s every %d.%02d secs\n",
297 physical->link.name, lcp->want_lqrperiod ? "LQR" : "LCP ECHO",
298 period / 100, period % 100);
299 physical->hdlc.lqm.timer.load = period * SECTICKS / 100;
301 physical->hdlc.lqm.timer.load = 0;
302 if (!lcp->his_lqrperiod)
303 log_Printf(LogLQM, "%s: LQR/LCP ECHO not negotiated\n",
304 physical->link.name);
309 lqr_Start(struct lcp *lcp)
311 struct physical *p = link2physical(lcp->fsm.link);
314 if (p->hdlc.lqm.timer.load)
319 lqr_reStart(struct lcp *lcp)
321 struct physical *p = link2physical(lcp->fsm.link);
324 if (p->hdlc.lqm.timer.load)
325 timer_Start(&p->hdlc.lqm.timer);
329 lqr_StopTimer(struct physical *physical)
331 timer_Stop(&physical->hdlc.lqm.timer);
335 lqr_Stop(struct physical *physical, int method)
337 if (method == LQM_LQR)
338 log_Printf(LogLQM, "%s: Stop sending LQR, Use LCP ECHO instead.\n",
339 physical->link.name);
340 if (method == LQM_ECHO)
341 log_Printf(LogLQM, "%s: Stop sending LCP ECHO.\n",
342 physical->link.name);
343 physical->hdlc.lqm.method &= ~method;
344 if (physical->hdlc.lqm.method)
345 SendLqrReport(physical->hdlc.lqm.owner);
347 timer_Stop(&physical->hdlc.lqm.timer);
351 lqr_Dump(const char *link, const char *message, const struct lqrdata *lqr)
353 if (log_IsKept(LogLQM)) {
354 log_Printf(LogLQM, "%s: %s:\n", link, message);
355 log_Printf(LogLQM, " Magic: %08x LastOutLQRs: %08x\n",
356 lqr->MagicNumber, lqr->LastOutLQRs);
357 log_Printf(LogLQM, " LastOutPackets: %08x LastOutOctets: %08x\n",
358 lqr->LastOutPackets, lqr->LastOutOctets);
359 log_Printf(LogLQM, " PeerInLQRs: %08x PeerInPackets: %08x\n",
360 lqr->PeerInLQRs, lqr->PeerInPackets);
361 log_Printf(LogLQM, " PeerInDiscards: %08x PeerInErrors: %08x\n",
362 lqr->PeerInDiscards, lqr->PeerInErrors);
363 log_Printf(LogLQM, " PeerInOctets: %08x PeerOutLQRs: %08x\n",
364 lqr->PeerInOctets, lqr->PeerOutLQRs);
365 log_Printf(LogLQM, " PeerOutPackets: %08x PeerOutOctets: %08x\n",
366 lqr->PeerOutPackets, lqr->PeerOutOctets);
371 lqr_Analyse(const struct hdlc *hdlc, const struct lqrdata *oldlqr,
372 const struct lqrdata *newlqr)
374 u_int32_t LQRs, transitLQRs, pkts, octets, disc, err;
376 if (!newlqr->PeerInLQRs) /* No analysis possible yet! */
379 log_Printf(LogLQM, "Analysis:\n");
381 LQRs = (newlqr->LastOutLQRs - oldlqr->LastOutLQRs) -
382 (newlqr->PeerInLQRs - oldlqr->PeerInLQRs);
383 transitLQRs = hdlc->lqm.lqr.OutLQRs - newlqr->LastOutLQRs;
384 pkts = (newlqr->LastOutPackets - oldlqr->LastOutPackets) -
385 (newlqr->PeerInPackets - oldlqr->PeerInPackets);
386 octets = (newlqr->LastOutOctets - oldlqr->LastOutOctets) -
387 (newlqr->PeerInOctets - oldlqr->PeerInOctets);
388 log_Printf(LogLQM, " Outbound lossage: %d LQR%s (%d en route), %d packet%s,"
389 " %d octet%s\n", (int)LQRs, LQRs == 1 ? "" : "s", (int)transitLQRs,
390 (int)pkts, pkts == 1 ? "" : "s",
391 (int)octets, octets == 1 ? "" : "s");
393 pkts = (newlqr->PeerOutPackets - oldlqr->PeerOutPackets) -
394 (hdlc->lqm.lqr.Save.InPackets - hdlc->lqm.lqr.prevSave.InPackets);
395 octets = (newlqr->PeerOutOctets - oldlqr->PeerOutOctets) -
396 (hdlc->lqm.lqr.Save.InOctets - hdlc->lqm.lqr.prevSave.InOctets);
397 log_Printf(LogLQM, " Inbound lossage: %d packet%s, %d octet%s\n",
398 (int)pkts, pkts == 1 ? "" : "s",
399 (int)octets, octets == 1 ? "" : "s");
401 disc = newlqr->PeerInDiscards - oldlqr->PeerInDiscards;
402 err = newlqr->PeerInErrors - oldlqr->PeerInErrors;
404 log_Printf(LogLQM, " Likely due to both peer congestion"
405 " and physical errors\n");
407 log_Printf(LogLQM, " Likely due to peer congestion\n");
409 log_Printf(LogLQM, " Likely due to physical errors\n");
411 log_Printf(LogLQM, " Likely due to transport "
416 lqr_LayerPush(struct bundle *b __unused, struct link *l, struct mbuf *bp,
417 int pri __unused, u_short *proto)
419 struct physical *p = link2physical(l);
423 /* Oops - can't happen :-] */
434 * All octets which are included in the FCS calculation MUST be counted,
435 * including the packet header, the information field, and any padding.
436 * The FCS octets MUST also be counted, and one flag octet per frame
437 * MUST be counted. All other octets (such as additional flag
438 * sequences, and escape bits or octets) MUST NOT be counted.
440 * As we're stacked higher than the HDLC layer (otherwise HDLC wouldn't be
441 * able to calculate the FCS), we must not forget about these additional
442 * bytes when we're asynchronous.
444 * We're also expecting to be stacked *before* the likes of the proto and
445 * acf layers (to avoid alignment issues), so deal with this too.
448 p->hdlc.lqm.ifOutUniPackets++;
449 p->hdlc.lqm.ifOutOctets += len + 1; /* plus 1 flag octet! */
450 for (layer = 0; layer < l->nlayers; layer++)
451 switch (l->layer[layer]->type) {
453 p->hdlc.lqm.ifOutOctets += acf_WrapperOctets(&l->lcp, *proto);
456 /* Not included - see rfc1989 */
459 p->hdlc.lqm.ifOutOctets += hdlc_WrapperOctets();
465 p->hdlc.lqm.ifOutOctets += proto_WrapperOctets(&l->lcp, *proto);
468 /* Nothing to add on */
471 log_Printf(LogWARN, "Oops, don't know how to do octets for %s layer\n",
472 l->layer[layer]->name);
476 if (*proto == PROTO_LQR) {
477 /* Overwrite the entire packet (created in SendLqrData()) */
479 size_t pending_pkts, pending_octets;
481 p->hdlc.lqm.lqr.OutLQRs++;
484 * We need to compensate for the fact that we're pushing our data
485 * onto the highest priority queue by factoring out packet & octet
486 * values from other queues!
488 link_PendingLowPriorityData(l, &pending_pkts, &pending_octets);
490 memset(&lqr, '\0', sizeof lqr);
491 lqr.MagicNumber = p->link.lcp.want_magic;
492 lqr.LastOutLQRs = p->hdlc.lqm.lqr.peer.PeerOutLQRs;
493 lqr.LastOutPackets = p->hdlc.lqm.lqr.peer.PeerOutPackets;
494 lqr.LastOutOctets = p->hdlc.lqm.lqr.peer.PeerOutOctets;
495 lqr.PeerInLQRs = p->hdlc.lqm.lqr.Save.InLQRs;
496 lqr.PeerInPackets = p->hdlc.lqm.lqr.Save.InPackets;
497 lqr.PeerInDiscards = p->hdlc.lqm.lqr.Save.InDiscards;
498 lqr.PeerInErrors = p->hdlc.lqm.lqr.Save.InErrors;
499 lqr.PeerInOctets = p->hdlc.lqm.lqr.Save.InOctets;
500 lqr.PeerOutLQRs = p->hdlc.lqm.lqr.OutLQRs;
501 lqr.PeerOutPackets = p->hdlc.lqm.ifOutUniPackets - pending_pkts;
502 /* Don't forget our ``flag'' octets.... */
503 lqr.PeerOutOctets = p->hdlc.lqm.ifOutOctets - pending_octets - pending_pkts;
504 lqr_Dump(l->name, "Output", &lqr);
505 lqr_ChangeOrder(&lqr, (struct lqrdata *)MBUF_CTOP(bp));
512 lqr_LayerPull(struct bundle *b __unused, struct link *l __unused,
513 struct mbuf *bp, u_short *proto)
516 * This is the ``Rx'' process from rfc1989, although a part of it is
517 * actually performed by sync_LayerPull() & hdlc_LayerPull() so that
518 * our octet counts are correct.
521 if (*proto == PROTO_LQR)
522 m_settype(bp, MB_LQRIN);
527 * Statistics for pulled packets are recorded either in hdlc_PullPacket()
528 * or sync_PullPacket()
531 struct layer lqrlayer = { LAYER_LQR, "lqr", lqr_LayerPush, lqr_LayerPull };