162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * linux/net/sunrpc/timer.c 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Estimate RPC request round trip time. 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * Based on packet round-trip and variance estimator algorithms described 862306a36Sopenharmony_ci * in appendix A of "Congestion Avoidance and Control" by Van Jacobson 962306a36Sopenharmony_ci * and Michael J. Karels (ACM Computer Communication Review; Proceedings 1062306a36Sopenharmony_ci * of the Sigcomm '88 Symposium in Stanford, CA, August, 1988). 1162306a36Sopenharmony_ci * 1262306a36Sopenharmony_ci * This RTT estimator is used only for RPC over datagram protocols. 1362306a36Sopenharmony_ci * 1462306a36Sopenharmony_ci * Copyright (C) 2002 Trond Myklebust <trond.myklebust@fys.uio.no> 1562306a36Sopenharmony_ci */ 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci#include <asm/param.h> 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#include <linux/types.h> 2062306a36Sopenharmony_ci#include <linux/unistd.h> 2162306a36Sopenharmony_ci#include <linux/module.h> 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci#include <linux/sunrpc/clnt.h> 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci#define RPC_RTO_MAX (60*HZ) 2662306a36Sopenharmony_ci#define RPC_RTO_INIT (HZ/5) 2762306a36Sopenharmony_ci#define RPC_RTO_MIN (HZ/10) 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci/** 3062306a36Sopenharmony_ci * rpc_init_rtt - Initialize an RPC RTT estimator context 3162306a36Sopenharmony_ci * @rt: context to initialize 3262306a36Sopenharmony_ci * @timeo: initial timeout value, in jiffies 3362306a36Sopenharmony_ci * 3462306a36Sopenharmony_ci */ 3562306a36Sopenharmony_civoid rpc_init_rtt(struct rpc_rtt *rt, unsigned long timeo) 3662306a36Sopenharmony_ci{ 3762306a36Sopenharmony_ci unsigned long init = 0; 3862306a36Sopenharmony_ci unsigned int i; 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci rt->timeo = timeo; 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci if (timeo > RPC_RTO_INIT) 4362306a36Sopenharmony_ci init = (timeo - RPC_RTO_INIT) << 3; 4462306a36Sopenharmony_ci for (i = 0; i < 5; i++) { 4562306a36Sopenharmony_ci rt->srtt[i] = init; 4662306a36Sopenharmony_ci rt->sdrtt[i] = RPC_RTO_INIT; 4762306a36Sopenharmony_ci rt->ntimeouts[i] = 0; 4862306a36Sopenharmony_ci } 4962306a36Sopenharmony_ci} 5062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(rpc_init_rtt); 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci/** 5362306a36Sopenharmony_ci * rpc_update_rtt - Update an RPC RTT estimator context 5462306a36Sopenharmony_ci * @rt: context to update 5562306a36Sopenharmony_ci * @timer: timer array index (request type) 5662306a36Sopenharmony_ci * @m: recent actual RTT, in jiffies 5762306a36Sopenharmony_ci * 5862306a36Sopenharmony_ci * NB: When computing the smoothed RTT and standard deviation, 5962306a36Sopenharmony_ci * be careful not to produce negative intermediate results. 6062306a36Sopenharmony_ci */ 6162306a36Sopenharmony_civoid rpc_update_rtt(struct rpc_rtt *rt, unsigned int timer, long m) 6262306a36Sopenharmony_ci{ 6362306a36Sopenharmony_ci long *srtt, *sdrtt; 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci if (timer-- == 0) 6662306a36Sopenharmony_ci return; 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci /* jiffies wrapped; ignore this one */ 6962306a36Sopenharmony_ci if (m < 0) 7062306a36Sopenharmony_ci return; 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci if (m == 0) 7362306a36Sopenharmony_ci m = 1L; 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci srtt = (long *)&rt->srtt[timer]; 7662306a36Sopenharmony_ci m -= *srtt >> 3; 7762306a36Sopenharmony_ci *srtt += m; 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci if (m < 0) 8062306a36Sopenharmony_ci m = -m; 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci sdrtt = (long *)&rt->sdrtt[timer]; 8362306a36Sopenharmony_ci m -= *sdrtt >> 2; 8462306a36Sopenharmony_ci *sdrtt += m; 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci /* Set lower bound on the variance */ 8762306a36Sopenharmony_ci if (*sdrtt < RPC_RTO_MIN) 8862306a36Sopenharmony_ci *sdrtt = RPC_RTO_MIN; 8962306a36Sopenharmony_ci} 9062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(rpc_update_rtt); 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci/** 9362306a36Sopenharmony_ci * rpc_calc_rto - Provide an estimated timeout value 9462306a36Sopenharmony_ci * @rt: context to use for calculation 9562306a36Sopenharmony_ci * @timer: timer array index (request type) 9662306a36Sopenharmony_ci * 9762306a36Sopenharmony_ci * Estimate RTO for an NFS RPC sent via an unreliable datagram. Use 9862306a36Sopenharmony_ci * the mean and mean deviation of RTT for the appropriate type of RPC 9962306a36Sopenharmony_ci * for frequently issued RPCs, and a fixed default for the others. 10062306a36Sopenharmony_ci * 10162306a36Sopenharmony_ci * The justification for doing "other" this way is that these RPCs 10262306a36Sopenharmony_ci * happen so infrequently that timer estimation would probably be 10362306a36Sopenharmony_ci * stale. Also, since many of these RPCs are non-idempotent, a 10462306a36Sopenharmony_ci * conservative timeout is desired. 10562306a36Sopenharmony_ci * 10662306a36Sopenharmony_ci * getattr, lookup, 10762306a36Sopenharmony_ci * read, write, commit - A+4D 10862306a36Sopenharmony_ci * other - timeo 10962306a36Sopenharmony_ci */ 11062306a36Sopenharmony_ciunsigned long rpc_calc_rto(struct rpc_rtt *rt, unsigned int timer) 11162306a36Sopenharmony_ci{ 11262306a36Sopenharmony_ci unsigned long res; 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci if (timer-- == 0) 11562306a36Sopenharmony_ci return rt->timeo; 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci res = ((rt->srtt[timer] + 7) >> 3) + rt->sdrtt[timer]; 11862306a36Sopenharmony_ci if (res > RPC_RTO_MAX) 11962306a36Sopenharmony_ci res = RPC_RTO_MAX; 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci return res; 12262306a36Sopenharmony_ci} 12362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(rpc_calc_rto); 124