2 * PPP Line Quality Monitoring (LQM) Module
4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp)
6 * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc. The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 * o LQR based on RFC1333
26 * o Allow user to configure LQM method and interval.
29 #include <sys/param.h>
47 #include "throughput.h"
50 #include "descriptor.h"
66 #define SIGNATURE 0x594e4f54
69 SendEchoReq(struct lcp *lcp)
71 struct hdlc *hdlc = &link2physical(lcp->fsm.link)->hdlc;
74 echo.magic = htonl(lcp->want_magic);
75 echo.signature = htonl(SIGNATURE);
76 echo.sequence = htonl(hdlc->lqm.echo.seq_sent);
77 fsm_Output(&lcp->fsm, CODE_ECHOREQ, hdlc->lqm.echo.seq_sent++,
78 (u_char *)&echo, sizeof echo, MB_ECHOOUT);
82 lqr_RecvEcho(struct fsm *fp, struct mbuf *bp)
84 struct hdlc *hdlc = &link2physical(fp->link)->hdlc;
85 struct lcp *lcp = fsm2lcp(fp);
88 if (mbuf_Length(bp) == sizeof lqr) {
89 bp = mbuf_Read(bp, &lqr, sizeof lqr);
90 lqr.magic = ntohl(lqr.magic);
91 lqr.signature = ntohl(lqr.signature);
92 lqr.sequence = ntohl(lqr.sequence);
94 /* Tolerate echo replies with either magic number */
95 if (lqr.magic != 0 && lqr.magic != lcp->his_magic &&
96 lqr.magic != lcp->want_magic) {
97 log_Printf(LogWARN, "%s: lqr_RecvEcho: Bad magic: expected 0x%08x,"
98 " got 0x%08x\n", fp->link->name, lcp->his_magic, lqr.magic);
100 * XXX: We should send a terminate request. But poor implementations may
104 if (lqr.signature == SIGNATURE) {
105 /* careful not to update lqm.echo.seq_recv with older values */
106 if ((hdlc->lqm.echo.seq_recv > (u_int32_t)0 - 5 && lqr.sequence < 5) ||
107 (hdlc->lqm.echo.seq_recv <= (u_int32_t)0 - 5 &&
108 lqr.sequence > hdlc->lqm.echo.seq_recv))
109 hdlc->lqm.echo.seq_recv = lqr.sequence;
111 log_Printf(LogWARN, "lqr_RecvEcho: Got sig 0x%08lx, not 0x%08lx !\n",
112 (u_long)lqr.signature, (u_long)SIGNATURE);
114 log_Printf(LogWARN, "lqr_RecvEcho: Got packet size %d, expecting %ld !\n",
115 mbuf_Length(bp), (long)sizeof(struct echolqr));
120 lqr_ChangeOrder(struct lqrdata *src, struct lqrdata *dst)
125 sp = (u_int32_t *) src;
126 dp = (u_int32_t *) dst;
127 for (n = 0; n < sizeof(struct lqrdata) / sizeof(u_int32_t); n++, sp++, dp++)
132 SendLqrData(struct lcp *lcp)
137 extra = proto_WrapperOctets(lcp, PROTO_LQR) +
138 acf_WrapperOctets(lcp, PROTO_LQR);
139 bp = mbuf_Alloc(sizeof(struct lqrdata) + extra, MB_LQROUT);
142 link_PushPacket(lcp->fsm.link, bp, lcp->fsm.bundle,
143 LINK_QUEUES(lcp->fsm.link) - 1, PROTO_LQR);
147 SendLqrReport(void *v)
149 struct lcp *lcp = (struct lcp *)v;
150 struct physical *p = link2physical(lcp->fsm.link);
152 timer_Stop(&p->hdlc.lqm.timer);
154 if (p->hdlc.lqm.method & LQM_LQR) {
155 if (p->hdlc.lqm.lqr.resent > 5) {
156 /* XXX: Should implement LQM strategy */
157 log_Printf(LogPHASE, "%s: ** Too many LQR packets lost **\n",
158 lcp->fsm.link->name);
159 log_Printf(LogLQM, "%s: Too many LQR packets lost\n",
160 lcp->fsm.link->name);
161 p->hdlc.lqm.method = 0;
162 datalink_Down(p->dl, CLOSE_NORMAL);
165 p->hdlc.lqm.lqr.resent++;
167 } else if (p->hdlc.lqm.method & LQM_ECHO) {
168 if ((p->hdlc.lqm.echo.seq_sent > 5 &&
169 p->hdlc.lqm.echo.seq_sent - 5 > p->hdlc.lqm.echo.seq_recv) ||
170 (p->hdlc.lqm.echo.seq_sent <= 5 &&
171 p->hdlc.lqm.echo.seq_sent > p->hdlc.lqm.echo.seq_recv + 5)) {
172 log_Printf(LogPHASE, "%s: ** Too many ECHO LQR packets lost **\n",
173 lcp->fsm.link->name);
174 log_Printf(LogLQM, "%s: Too many ECHO LQR packets lost\n",
175 lcp->fsm.link->name);
176 p->hdlc.lqm.method = 0;
177 datalink_Down(p->dl, CLOSE_NORMAL);
181 if (p->hdlc.lqm.method && p->hdlc.lqm.timer.load)
182 timer_Start(&p->hdlc.lqm.timer);
186 lqr_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
188 struct physical *p = link2physical(l);
189 struct lcp *lcp = p->hdlc.lqm.owner;
193 log_Printf(LogERROR, "lqr_Input: Not a physical link - dropped\n");
198 p->hdlc.lqm.lqr.SaveInLQRs++;
200 len = mbuf_Length(bp);
201 if (len != sizeof(struct lqrdata))
202 log_Printf(LogWARN, "lqr_Input: Got packet size %d, expecting %ld !\n",
203 len, (long)sizeof(struct lqrdata));
204 else if (!IsAccepted(l->lcp.cfg.lqr) && !(p->hdlc.lqm.method & LQM_LQR)) {
205 bp = mbuf_Contiguous(proto_Prepend(bp, PROTO_LQR, 0, 0));
206 lcp_SendProtoRej(lcp, MBUF_CTOP(bp), bp->cnt);
211 bp = mbuf_Contiguous(bp);
212 lqr = (struct lqrdata *)MBUF_CTOP(bp);
213 if (ntohl(lqr->MagicNumber) != lcp->his_magic)
214 log_Printf(LogWARN, "lqr_Input: magic 0x%08lx is wrong,"
215 " expecting 0x%08lx\n",
216 (u_long)ntohl(lqr->MagicNumber), (u_long)lcp->his_magic);
219 * Remember our PeerInLQRs, then convert byte order and save
221 lastLQR = p->hdlc.lqm.lqr.peer.PeerInLQRs;
223 lqr_ChangeOrder(lqr, &p->hdlc.lqm.lqr.peer);
224 lqr_Dump(l->name, "Input", &p->hdlc.lqm.lqr.peer);
225 /* we have received an LQR from peer */
226 p->hdlc.lqm.lqr.resent = 0;
229 * Generate an LQR response if we're not running an LQR timer OR
230 * two successive LQR's PeerInLQRs are the same OR we're not going to
231 * send our next one before the peers max timeout.
233 if (p->hdlc.lqm.timer.load == 0 ||
234 !(p->hdlc.lqm.method & LQM_LQR) ||
235 (lastLQR && lastLQR == p->hdlc.lqm.lqr.peer.PeerInLQRs) ||
236 (p->hdlc.lqm.lqr.peer_timeout &&
237 p->hdlc.lqm.timer.rest * 100 / SECTICKS >
238 p->hdlc.lqm.lqr.peer_timeout))
247 * When LCP is reached to opened state, We'll start LQM activity.
251 lqr_Setup(struct lcp *lcp)
253 struct physical *physical = link2physical(lcp->fsm.link);
255 physical->hdlc.lqm.lqr.resent = 0;
256 physical->hdlc.lqm.echo.seq_sent = 0;
257 physical->hdlc.lqm.echo.seq_recv = 0;
258 memset(&physical->hdlc.lqm.lqr.peer, '\0',
259 sizeof physical->hdlc.lqm.lqr.peer);
261 physical->hdlc.lqm.method = LQM_ECHO;
262 if (IsEnabled(lcp->cfg.lqr) && !REJECTED(lcp, TY_QUALPROTO))
263 physical->hdlc.lqm.method |= LQM_LQR;
264 timer_Stop(&physical->hdlc.lqm.timer);
266 physical->hdlc.lqm.lqr.peer_timeout = lcp->his_lqrperiod;
267 if (lcp->his_lqrperiod)
268 log_Printf(LogLQM, "%s: Expecting LQR every %d.%02d secs\n",
269 physical->link.name, lcp->his_lqrperiod / 100,
270 lcp->his_lqrperiod % 100);
272 if (lcp->want_lqrperiod) {
273 log_Printf(LogLQM, "%s: Will send %s every %d.%02d secs\n",
275 physical->hdlc.lqm.method & LQM_LQR ? "LQR" : "ECHO LQR",
276 lcp->want_lqrperiod / 100, lcp->want_lqrperiod % 100);
277 physical->hdlc.lqm.timer.load = lcp->want_lqrperiod * SECTICKS / 100;
278 physical->hdlc.lqm.timer.func = SendLqrReport;
279 physical->hdlc.lqm.timer.name = "lqm";
280 physical->hdlc.lqm.timer.arg = lcp;
282 physical->hdlc.lqm.timer.load = 0;
283 if (!lcp->his_lqrperiod)
284 log_Printf(LogLQM, "%s: LQR/ECHO LQR not negotiated\n",
285 physical->link.name);
290 lqr_Start(struct lcp *lcp)
292 struct physical *p = link2physical(lcp->fsm.link);
295 if (p->hdlc.lqm.timer.load)
300 lqr_reStart(struct lcp *lcp)
302 struct physical *p = link2physical(lcp->fsm.link);
305 if (p->hdlc.lqm.timer.load)
306 timer_Start(&p->hdlc.lqm.timer);
310 lqr_StopTimer(struct physical *physical)
312 timer_Stop(&physical->hdlc.lqm.timer);
316 lqr_Stop(struct physical *physical, int method)
318 if (method == LQM_LQR)
319 log_Printf(LogLQM, "%s: Stop sending LQR, Use LCP ECHO instead.\n",
320 physical->link.name);
321 if (method == LQM_ECHO)
322 log_Printf(LogLQM, "%s: Stop sending LCP ECHO.\n",
323 physical->link.name);
324 physical->hdlc.lqm.method &= ~method;
325 if (physical->hdlc.lqm.method)
326 SendLqrReport(physical->hdlc.lqm.owner);
328 timer_Stop(&physical->hdlc.lqm.timer);
332 lqr_Dump(const char *link, const char *message, const struct lqrdata *lqr)
334 if (log_IsKept(LogLQM)) {
335 log_Printf(LogLQM, "%s: %s:\n", link, message);
336 log_Printf(LogLQM, " Magic: %08x LastOutLQRs: %08x\n",
337 lqr->MagicNumber, lqr->LastOutLQRs);
338 log_Printf(LogLQM, " LastOutPackets: %08x LastOutOctets: %08x\n",
339 lqr->LastOutPackets, lqr->LastOutOctets);
340 log_Printf(LogLQM, " PeerInLQRs: %08x PeerInPackets: %08x\n",
341 lqr->PeerInLQRs, lqr->PeerInPackets);
342 log_Printf(LogLQM, " PeerInDiscards: %08x PeerInErrors: %08x\n",
343 lqr->PeerInDiscards, lqr->PeerInErrors);
344 log_Printf(LogLQM, " PeerInOctets: %08x PeerOutLQRs: %08x\n",
345 lqr->PeerInOctets, lqr->PeerOutLQRs);
346 log_Printf(LogLQM, " PeerOutPackets: %08x PeerOutOctets: %08x\n",
347 lqr->PeerOutPackets, lqr->PeerOutOctets);
352 lqr_LayerPush(struct bundle *b, struct link *l, struct mbuf *bp,
353 int pri, u_short *proto)
355 struct physical *p = link2physical(l);
359 /* Oops - can't happen :-] */
367 * All octets which are included in the FCS calculation MUST be counted,
368 * including the packet header, the information field, and any padding.
369 * The FCS octets MUST also be counted, and one flag octet per frame
370 * MUST be counted. All other octets (such as additional flag
371 * sequences, and escape bits or octets) MUST NOT be counted.
373 * As we're stacked before the HDLC layer (otherwise HDLC wouldn't be
374 * able to calculate the FCS), we must not forget about these additional
375 * bytes when we're asynchronous.
377 * We're also expecting to be stacked *before* the proto and acf layers.
378 * If we were after these, it makes alignment more of a pain, and we
379 * don't do LQR without these layers.
382 bp = mbuf_Contiguous(bp);
383 len = mbuf_Length(bp);
385 if (!physical_IsSync(p))
386 p->hdlc.lqm.OutOctets += hdlc_WrapperOctets(&l->lcp, *proto);
387 p->hdlc.lqm.OutOctets += acf_WrapperOctets(&l->lcp, *proto) +
388 proto_WrapperOctets(&l->lcp, *proto) + len + 1;
389 p->hdlc.lqm.OutPackets++;
391 if (*proto == PROTO_LQR) {
392 /* Overwrite the entire packet (created in SendLqrData()) */
395 lqr.MagicNumber = p->link.lcp.want_magic;
396 lqr.LastOutLQRs = p->hdlc.lqm.lqr.peer.PeerOutLQRs;
397 lqr.LastOutPackets = p->hdlc.lqm.lqr.peer.PeerOutPackets;
398 lqr.LastOutOctets = p->hdlc.lqm.lqr.peer.PeerOutOctets;
399 lqr.PeerInLQRs = p->hdlc.lqm.lqr.SaveInLQRs;
400 lqr.PeerInPackets = p->hdlc.lqm.SaveInPackets;
401 lqr.PeerInDiscards = p->hdlc.lqm.SaveInDiscards;
402 lqr.PeerInErrors = p->hdlc.lqm.SaveInErrors;
403 lqr.PeerInOctets = p->hdlc.lqm.SaveInOctets;
404 lqr.PeerOutPackets = p->hdlc.lqm.OutPackets;
405 lqr.PeerOutOctets = p->hdlc.lqm.OutOctets;
406 if (p->hdlc.lqm.lqr.peer.LastOutLQRs == p->hdlc.lqm.lqr.OutLQRs) {
408 * only increment if it's the first time or we've got a reply
411 lqr.PeerOutLQRs = ++p->hdlc.lqm.lqr.OutLQRs;
412 lqr_Dump(l->name, "Output", &lqr);
414 lqr.PeerOutLQRs = p->hdlc.lqm.lqr.OutLQRs;
415 lqr_Dump(l->name, "Output (again)", &lqr);
417 lqr_ChangeOrder(&lqr, (struct lqrdata *)MBUF_CTOP(bp));
424 lqr_LayerPull(struct bundle *b, struct link *l, struct mbuf *bp, u_short *proto)
427 * We mark the packet as ours but don't do anything 'till it's dispatched
430 if (*proto == PROTO_LQR)
431 mbuf_SetType(bp, MB_LQRIN);
436 * Statistics for pulled packets are recorded either in hdlc_PullPacket()
437 * or sync_PullPacket()
440 struct layer lqrlayer = { LAYER_LQR, "lqr", lqr_LayerPush, lqr_LayerPull };