sch_cake.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /* COMMON Applications Kept Enhanced (CAKE) discipline
  3. *
  4. * Copyright (C) 2014-2018 Jonathan Morton <chromatix99@gmail.com>
  5. * Copyright (C) 2015-2018 Toke Høiland-Jørgensen <toke@toke.dk>
  6. * Copyright (C) 2014-2018 Dave Täht <dave.taht@gmail.com>
  7. * Copyright (C) 2015-2018 Sebastian Moeller <moeller0@gmx.de>
  8. * (C) 2015-2018 Kevin Darbyshire-Bryant <kevin@darbyshire-bryant.me.uk>
  9. * Copyright (C) 2017-2018 Ryan Mounce <ryan@mounce.com.au>
  10. *
  11. * The CAKE Principles:
  12. * (or, how to have your cake and eat it too)
  13. *
  14. * This is a combination of several shaping, AQM and FQ techniques into one
  15. * easy-to-use package:
  16. *
  17. * - An overall bandwidth shaper, to move the bottleneck away from dumb CPE
  18. * equipment and bloated MACs. This operates in deficit mode (as in sch_fq),
  19. * eliminating the need for any sort of burst parameter (eg. token bucket
  20. * depth). Burst support is limited to that necessary to overcome scheduling
  21. * latency.
  22. *
  23. * - A Diffserv-aware priority queue, giving more priority to certain classes,
  24. * up to a specified fraction of bandwidth. Above that bandwidth threshold,
  25. * the priority is reduced to avoid starving other tins.
  26. *
  27. * - Each priority tin has a separate Flow Queue system, to isolate traffic
  28. * flows from each other. This prevents a burst on one flow from increasing
  29. * the delay to another. Flows are distributed to queues using a
  30. * set-associative hash function.
  31. *
  32. * - Each queue is actively managed by Cobalt, which is a combination of the
  33. * Codel and Blue AQM algorithms. This serves flows fairly, and signals
  34. * congestion early via ECN (if available) and/or packet drops, to keep
  35. * latency low. The codel parameters are auto-tuned based on the bandwidth
  36. * setting, as is necessary at low bandwidths.
  37. *
  38. * The configuration parameters are kept deliberately simple for ease of use.
  39. * Everything has sane defaults. Complete generality of configuration is *not*
  40. * a goal.
  41. *
  42. * The priority queue operates according to a weighted DRR scheme, combined with
  43. * a bandwidth tracker which reuses the shaper logic to detect which side of the
  44. * bandwidth sharing threshold the tin is operating. This determines whether a
  45. * priority-based weight (high) or a bandwidth-based weight (low) is used for
  46. * that tin in the current pass.
  47. *
  48. * This qdisc was inspired by Eric Dumazet's fq_codel code, which he kindly
  49. * granted us permission to leverage.
  50. */
  51. #include <linux/module.h>
  52. #include <linux/types.h>
  53. #include <linux/kernel.h>
  54. #include <linux/jiffies.h>
  55. #include <linux/string.h>
  56. #include <linux/in.h>
  57. #include <linux/errno.h>
  58. #include <linux/init.h>
  59. #include <linux/skbuff.h>
  60. #include <linux/jhash.h>
  61. #include <linux/slab.h>
  62. #include <linux/vmalloc.h>
  63. #include <linux/reciprocal_div.h>
  64. #include <net/netlink.h>
  65. #include <linux/version.h>
  66. #include <linux/if_vlan.h>
  67. #include <net/pkt_sched.h>
  68. #include <net/pkt_cls.h>
  69. #include <net/tcp.h>
  70. #include <net/flow_dissector.h>
  71. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  72. #include <net/netfilter/nf_conntrack_core.h>
  73. #endif
  74. #define CAKE_SET_WAYS (8)
  75. #define CAKE_MAX_TINS (8)
  76. #define CAKE_QUEUES (1024)
  77. #define CAKE_FLOW_MASK 63
  78. #define CAKE_FLOW_NAT_FLAG 64
  79. /* struct cobalt_params - contains codel and blue parameters
  80. * @interval: codel initial drop rate
  81. * @target: maximum persistent sojourn time & blue update rate
  82. * @mtu_time: serialisation delay of maximum-size packet
  83. * @p_inc: increment of blue drop probability (0.32 fxp)
  84. * @p_dec: decrement of blue drop probability (0.32 fxp)
  85. */
  86. struct cobalt_params {
  87. u64 interval;
  88. u64 target;
  89. u64 mtu_time;
  90. u32 p_inc;
  91. u32 p_dec;
  92. };
  93. /* struct cobalt_vars - contains codel and blue variables
  94. * @count: codel dropping frequency
  95. * @rec_inv_sqrt: reciprocal value of sqrt(count) >> 1
  96. * @drop_next: time to drop next packet, or when we dropped last
  97. * @blue_timer: Blue time to next drop
  98. * @p_drop: BLUE drop probability (0.32 fxp)
  99. * @dropping: set if in dropping state
  100. * @ecn_marked: set if marked
  101. */
  102. struct cobalt_vars {
  103. u32 count;
  104. u32 rec_inv_sqrt;
  105. ktime_t drop_next;
  106. ktime_t blue_timer;
  107. u32 p_drop;
  108. bool dropping;
  109. bool ecn_marked;
  110. };
  111. enum {
  112. CAKE_SET_NONE = 0,
  113. CAKE_SET_SPARSE,
  114. CAKE_SET_SPARSE_WAIT, /* counted in SPARSE, actually in BULK */
  115. CAKE_SET_BULK,
  116. CAKE_SET_DECAYING
  117. };
  118. struct cake_flow {
  119. /* this stuff is all needed per-flow at dequeue time */
  120. struct sk_buff *head;
  121. struct sk_buff *tail;
  122. struct list_head flowchain;
  123. s32 deficit;
  124. u32 dropped;
  125. struct cobalt_vars cvars;
  126. u16 srchost; /* index into cake_host table */
  127. u16 dsthost;
  128. u8 set;
  129. }; /* please try to keep this structure <= 64 bytes */
  130. struct cake_host {
  131. u32 srchost_tag;
  132. u32 dsthost_tag;
  133. u16 srchost_refcnt;
  134. u16 dsthost_refcnt;
  135. };
  136. struct cake_heap_entry {
  137. u16 t:3, b:10;
  138. };
  139. struct cake_tin_data {
  140. struct cake_flow flows[CAKE_QUEUES];
  141. u32 backlogs[CAKE_QUEUES];
  142. u32 tags[CAKE_QUEUES]; /* for set association */
  143. u16 overflow_idx[CAKE_QUEUES];
  144. struct cake_host hosts[CAKE_QUEUES]; /* for triple isolation */
  145. u16 flow_quantum;
  146. struct cobalt_params cparams;
  147. u32 drop_overlimit;
  148. u16 bulk_flow_count;
  149. u16 sparse_flow_count;
  150. u16 decaying_flow_count;
  151. u16 unresponsive_flow_count;
  152. u32 max_skblen;
  153. struct list_head new_flows;
  154. struct list_head old_flows;
  155. struct list_head decaying_flows;
  156. /* time_next = time_this + ((len * rate_ns) >> rate_shft) */
  157. ktime_t time_next_packet;
  158. u64 tin_rate_ns;
  159. u64 tin_rate_bps;
  160. u16 tin_rate_shft;
  161. u16 tin_quantum_prio;
  162. u16 tin_quantum_band;
  163. s32 tin_deficit;
  164. u32 tin_backlog;
  165. u32 tin_dropped;
  166. u32 tin_ecn_mark;
  167. u32 packets;
  168. u64 bytes;
  169. u32 ack_drops;
  170. /* moving averages */
  171. u64 avge_delay;
  172. u64 peak_delay;
  173. u64 base_delay;
  174. /* hash function stats */
  175. u32 way_directs;
  176. u32 way_hits;
  177. u32 way_misses;
  178. u32 way_collisions;
  179. }; /* number of tins is small, so size of this struct doesn't matter much */
  180. struct cake_sched_data {
  181. struct tcf_proto __rcu *filter_list; /* optional external classifier */
  182. struct tcf_block *block;
  183. struct cake_tin_data *tins;
  184. struct cake_heap_entry overflow_heap[CAKE_QUEUES * CAKE_MAX_TINS];
  185. u16 overflow_timeout;
  186. u16 tin_cnt;
  187. u8 tin_mode;
  188. u8 flow_mode;
  189. u8 ack_filter;
  190. u8 atm_mode;
  191. /* time_next = time_this + ((len * rate_ns) >> rate_shft) */
  192. u16 rate_shft;
  193. ktime_t time_next_packet;
  194. ktime_t failsafe_next_packet;
  195. u64 rate_ns;
  196. u64 rate_bps;
  197. u16 rate_flags;
  198. s16 rate_overhead;
  199. u16 rate_mpu;
  200. u64 interval;
  201. u64 target;
  202. /* resource tracking */
  203. u32 buffer_used;
  204. u32 buffer_max_used;
  205. u32 buffer_limit;
  206. u32 buffer_config_limit;
  207. /* indices for dequeue */
  208. u16 cur_tin;
  209. u16 cur_flow;
  210. struct qdisc_watchdog watchdog;
  211. const u8 *tin_index;
  212. const u8 *tin_order;
  213. /* bandwidth capacity estimate */
  214. ktime_t last_packet_time;
  215. ktime_t avg_window_begin;
  216. u64 avg_packet_interval;
  217. u64 avg_window_bytes;
  218. u64 avg_peak_bandwidth;
  219. ktime_t last_reconfig_time;
  220. /* packet length stats */
  221. u32 avg_netoff;
  222. u16 max_netlen;
  223. u16 max_adjlen;
  224. u16 min_netlen;
  225. u16 min_adjlen;
  226. };
  227. enum {
  228. CAKE_FLAG_OVERHEAD = BIT(0),
  229. CAKE_FLAG_AUTORATE_INGRESS = BIT(1),
  230. CAKE_FLAG_INGRESS = BIT(2),
  231. CAKE_FLAG_WASH = BIT(3),
  232. CAKE_FLAG_SPLIT_GSO = BIT(4)
  233. };
  234. /* COBALT operates the Codel and BLUE algorithms in parallel, in order to
  235. * obtain the best features of each. Codel is excellent on flows which
  236. * respond to congestion signals in a TCP-like way. BLUE is more effective on
  237. * unresponsive flows.
  238. */
  239. struct cobalt_skb_cb {
  240. ktime_t enqueue_time;
  241. u32 adjusted_len;
  242. };
  243. static u64 us_to_ns(u64 us)
  244. {
  245. return us * NSEC_PER_USEC;
  246. }
  247. static struct cobalt_skb_cb *get_cobalt_cb(const struct sk_buff *skb)
  248. {
  249. qdisc_cb_private_validate(skb, sizeof(struct cobalt_skb_cb));
  250. return (struct cobalt_skb_cb *)qdisc_skb_cb(skb)->data;
  251. }
  252. static ktime_t cobalt_get_enqueue_time(const struct sk_buff *skb)
  253. {
  254. return get_cobalt_cb(skb)->enqueue_time;
  255. }
  256. static void cobalt_set_enqueue_time(struct sk_buff *skb,
  257. ktime_t now)
  258. {
  259. get_cobalt_cb(skb)->enqueue_time = now;
  260. }
  261. static u16 quantum_div[CAKE_QUEUES + 1] = {0};
  262. /* Diffserv lookup tables */
  263. static const u8 precedence[] = {
  264. 0, 0, 0, 0, 0, 0, 0, 0,
  265. 1, 1, 1, 1, 1, 1, 1, 1,
  266. 2, 2, 2, 2, 2, 2, 2, 2,
  267. 3, 3, 3, 3, 3, 3, 3, 3,
  268. 4, 4, 4, 4, 4, 4, 4, 4,
  269. 5, 5, 5, 5, 5, 5, 5, 5,
  270. 6, 6, 6, 6, 6, 6, 6, 6,
  271. 7, 7, 7, 7, 7, 7, 7, 7,
  272. };
  273. static const u8 diffserv8[] = {
  274. 2, 5, 1, 2, 4, 2, 2, 2,
  275. 0, 2, 1, 2, 1, 2, 1, 2,
  276. 5, 2, 4, 2, 4, 2, 4, 2,
  277. 3, 2, 3, 2, 3, 2, 3, 2,
  278. 6, 2, 3, 2, 3, 2, 3, 2,
  279. 6, 2, 2, 2, 6, 2, 6, 2,
  280. 7, 2, 2, 2, 2, 2, 2, 2,
  281. 7, 2, 2, 2, 2, 2, 2, 2,
  282. };
  283. static const u8 diffserv4[] = {
  284. 0, 2, 0, 0, 2, 0, 0, 0,
  285. 1, 0, 0, 0, 0, 0, 0, 0,
  286. 2, 0, 2, 0, 2, 0, 2, 0,
  287. 2, 0, 2, 0, 2, 0, 2, 0,
  288. 3, 0, 2, 0, 2, 0, 2, 0,
  289. 3, 0, 0, 0, 3, 0, 3, 0,
  290. 3, 0, 0, 0, 0, 0, 0, 0,
  291. 3, 0, 0, 0, 0, 0, 0, 0,
  292. };
  293. static const u8 diffserv3[] = {
  294. 0, 0, 0, 0, 2, 0, 0, 0,
  295. 1, 0, 0, 0, 0, 0, 0, 0,
  296. 0, 0, 0, 0, 0, 0, 0, 0,
  297. 0, 0, 0, 0, 0, 0, 0, 0,
  298. 0, 0, 0, 0, 0, 0, 0, 0,
  299. 0, 0, 0, 0, 2, 0, 2, 0,
  300. 2, 0, 0, 0, 0, 0, 0, 0,
  301. 2, 0, 0, 0, 0, 0, 0, 0,
  302. };
  303. static const u8 besteffort[] = {
  304. 0, 0, 0, 0, 0, 0, 0, 0,
  305. 0, 0, 0, 0, 0, 0, 0, 0,
  306. 0, 0, 0, 0, 0, 0, 0, 0,
  307. 0, 0, 0, 0, 0, 0, 0, 0,
  308. 0, 0, 0, 0, 0, 0, 0, 0,
  309. 0, 0, 0, 0, 0, 0, 0, 0,
  310. 0, 0, 0, 0, 0, 0, 0, 0,
  311. 0, 0, 0, 0, 0, 0, 0, 0,
  312. };
  313. /* tin priority order for stats dumping */
  314. static const u8 normal_order[] = {0, 1, 2, 3, 4, 5, 6, 7};
  315. static const u8 bulk_order[] = {1, 0, 2, 3};
  316. #define REC_INV_SQRT_CACHE (16)
  317. static u32 cobalt_rec_inv_sqrt_cache[REC_INV_SQRT_CACHE] = {0};
  318. /* http://en.wikipedia.org/wiki/Methods_of_computing_square_roots
  319. * new_invsqrt = (invsqrt / 2) * (3 - count * invsqrt^2)
  320. *
  321. * Here, invsqrt is a fixed point number (< 1.0), 32bit mantissa, aka Q0.32
  322. */
  323. static void cobalt_newton_step(struct cobalt_vars *vars)
  324. {
  325. u32 invsqrt, invsqrt2;
  326. u64 val;
  327. invsqrt = vars->rec_inv_sqrt;
  328. invsqrt2 = ((u64)invsqrt * invsqrt) >> 32;
  329. val = (3LL << 32) - ((u64)vars->count * invsqrt2);
  330. val >>= 2; /* avoid overflow in following multiply */
  331. val = (val * invsqrt) >> (32 - 2 + 1);
  332. vars->rec_inv_sqrt = val;
  333. }
  334. static void cobalt_invsqrt(struct cobalt_vars *vars)
  335. {
  336. if (vars->count < REC_INV_SQRT_CACHE)
  337. vars->rec_inv_sqrt = cobalt_rec_inv_sqrt_cache[vars->count];
  338. else
  339. cobalt_newton_step(vars);
  340. }
  341. /* There is a big difference in timing between the accurate values placed in
  342. * the cache and the approximations given by a single Newton step for small
  343. * count values, particularly when stepping from count 1 to 2 or vice versa.
  344. * Above 16, a single Newton step gives sufficient accuracy in either
  345. * direction, given the precision stored.
  346. *
  347. * The magnitude of the error when stepping up to count 2 is such as to give
  348. * the value that *should* have been produced at count 4.
  349. */
  350. static void cobalt_cache_init(void)
  351. {
  352. struct cobalt_vars v;
  353. memset(&v, 0, sizeof(v));
  354. v.rec_inv_sqrt = ~0U;
  355. cobalt_rec_inv_sqrt_cache[0] = v.rec_inv_sqrt;
  356. for (v.count = 1; v.count < REC_INV_SQRT_CACHE; v.count++) {
  357. cobalt_newton_step(&v);
  358. cobalt_newton_step(&v);
  359. cobalt_newton_step(&v);
  360. cobalt_newton_step(&v);
  361. cobalt_rec_inv_sqrt_cache[v.count] = v.rec_inv_sqrt;
  362. }
  363. }
  364. static void cobalt_vars_init(struct cobalt_vars *vars)
  365. {
  366. memset(vars, 0, sizeof(*vars));
  367. if (!cobalt_rec_inv_sqrt_cache[0]) {
  368. cobalt_cache_init();
  369. cobalt_rec_inv_sqrt_cache[0] = ~0;
  370. }
  371. }
  372. /* CoDel control_law is t + interval/sqrt(count)
  373. * We maintain in rec_inv_sqrt the reciprocal value of sqrt(count) to avoid
  374. * both sqrt() and divide operation.
  375. */
  376. static ktime_t cobalt_control(ktime_t t,
  377. u64 interval,
  378. u32 rec_inv_sqrt)
  379. {
  380. return ktime_add_ns(t, reciprocal_scale(interval,
  381. rec_inv_sqrt));
  382. }
  383. /* Call this when a packet had to be dropped due to queue overflow. Returns
  384. * true if the BLUE state was quiescent before but active after this call.
  385. */
  386. static bool cobalt_queue_full(struct cobalt_vars *vars,
  387. struct cobalt_params *p,
  388. ktime_t now)
  389. {
  390. bool up = false;
  391. if (ktime_to_ns(ktime_sub(now, vars->blue_timer)) > p->target) {
  392. up = !vars->p_drop;
  393. vars->p_drop += p->p_inc;
  394. if (vars->p_drop < p->p_inc)
  395. vars->p_drop = ~0;
  396. vars->blue_timer = now;
  397. }
  398. vars->dropping = true;
  399. vars->drop_next = now;
  400. if (!vars->count)
  401. vars->count = 1;
  402. return up;
  403. }
  404. /* Call this when the queue was serviced but turned out to be empty. Returns
  405. * true if the BLUE state was active before but quiescent after this call.
  406. */
  407. static bool cobalt_queue_empty(struct cobalt_vars *vars,
  408. struct cobalt_params *p,
  409. ktime_t now)
  410. {
  411. bool down = false;
  412. if (vars->p_drop &&
  413. ktime_to_ns(ktime_sub(now, vars->blue_timer)) > p->target) {
  414. if (vars->p_drop < p->p_dec)
  415. vars->p_drop = 0;
  416. else
  417. vars->p_drop -= p->p_dec;
  418. vars->blue_timer = now;
  419. down = !vars->p_drop;
  420. }
  421. vars->dropping = false;
  422. if (vars->count && ktime_to_ns(ktime_sub(now, vars->drop_next)) >= 0) {
  423. vars->count--;
  424. cobalt_invsqrt(vars);
  425. vars->drop_next = cobalt_control(vars->drop_next,
  426. p->interval,
  427. vars->rec_inv_sqrt);
  428. }
  429. return down;
  430. }
  431. /* Call this with a freshly dequeued packet for possible congestion marking.
  432. * Returns true as an instruction to drop the packet, false for delivery.
  433. */
  434. static bool cobalt_should_drop(struct cobalt_vars *vars,
  435. struct cobalt_params *p,
  436. ktime_t now,
  437. struct sk_buff *skb,
  438. u32 bulk_flows)
  439. {
  440. bool next_due, over_target, drop = false;
  441. ktime_t schedule;
  442. u64 sojourn;
  443. /* The 'schedule' variable records, in its sign, whether 'now' is before or
  444. * after 'drop_next'. This allows 'drop_next' to be updated before the next
  445. * scheduling decision is actually branched, without destroying that
  446. * information. Similarly, the first 'schedule' value calculated is preserved
  447. * in the boolean 'next_due'.
  448. *
  449. * As for 'drop_next', we take advantage of the fact that 'interval' is both
  450. * the delay between first exceeding 'target' and the first signalling event,
  451. * *and* the scaling factor for the signalling frequency. It's therefore very
  452. * natural to use a single mechanism for both purposes, and eliminates a
  453. * significant amount of reference Codel's spaghetti code. To help with this,
  454. * both the '0' and '1' entries in the invsqrt cache are 0xFFFFFFFF, as close
  455. * as possible to 1.0 in fixed-point.
  456. */
  457. sojourn = ktime_to_ns(ktime_sub(now, cobalt_get_enqueue_time(skb)));
  458. schedule = ktime_sub(now, vars->drop_next);
  459. over_target = sojourn > p->target &&
  460. sojourn > p->mtu_time * bulk_flows * 2 &&
  461. sojourn > p->mtu_time * 4;
  462. next_due = vars->count && ktime_to_ns(schedule) >= 0;
  463. vars->ecn_marked = false;
  464. if (over_target) {
  465. if (!vars->dropping) {
  466. vars->dropping = true;
  467. vars->drop_next = cobalt_control(now,
  468. p->interval,
  469. vars->rec_inv_sqrt);
  470. }
  471. if (!vars->count)
  472. vars->count = 1;
  473. } else if (vars->dropping) {
  474. vars->dropping = false;
  475. }
  476. if (next_due && vars->dropping) {
  477. /* Use ECN mark if possible, otherwise drop */
  478. drop = !(vars->ecn_marked = INET_ECN_set_ce(skb));
  479. vars->count++;
  480. if (!vars->count)
  481. vars->count--;
  482. cobalt_invsqrt(vars);
  483. vars->drop_next = cobalt_control(vars->drop_next,
  484. p->interval,
  485. vars->rec_inv_sqrt);
  486. schedule = ktime_sub(now, vars->drop_next);
  487. } else {
  488. while (next_due) {
  489. vars->count--;
  490. cobalt_invsqrt(vars);
  491. vars->drop_next = cobalt_control(vars->drop_next,
  492. p->interval,
  493. vars->rec_inv_sqrt);
  494. schedule = ktime_sub(now, vars->drop_next);
  495. next_due = vars->count && ktime_to_ns(schedule) >= 0;
  496. }
  497. }
  498. /* Simple BLUE implementation. Lack of ECN is deliberate. */
  499. if (vars->p_drop)
  500. drop |= (prandom_u32() < vars->p_drop);
  501. /* Overload the drop_next field as an activity timeout */
  502. if (!vars->count)
  503. vars->drop_next = ktime_add_ns(now, p->interval);
  504. else if (ktime_to_ns(schedule) > 0 && !drop)
  505. vars->drop_next = now;
  506. return drop;
  507. }
  508. static void cake_update_flowkeys(struct flow_keys *keys,
  509. const struct sk_buff *skb)
  510. {
  511. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  512. struct nf_conntrack_tuple tuple = {};
  513. bool rev = !skb->_nfct;
  514. if (tc_skb_protocol(skb) != htons(ETH_P_IP))
  515. return;
  516. if (!nf_ct_get_tuple_skb(&tuple, skb))
  517. return;
  518. keys->addrs.v4addrs.src = rev ? tuple.dst.u3.ip : tuple.src.u3.ip;
  519. keys->addrs.v4addrs.dst = rev ? tuple.src.u3.ip : tuple.dst.u3.ip;
  520. if (keys->ports.ports) {
  521. keys->ports.src = rev ? tuple.dst.u.all : tuple.src.u.all;
  522. keys->ports.dst = rev ? tuple.src.u.all : tuple.dst.u.all;
  523. }
  524. #endif
  525. }
  526. /* Cake has several subtle multiple bit settings. In these cases you
  527. * would be matching triple isolate mode as well.
  528. */
  529. static bool cake_dsrc(int flow_mode)
  530. {
  531. return (flow_mode & CAKE_FLOW_DUAL_SRC) == CAKE_FLOW_DUAL_SRC;
  532. }
  533. static bool cake_ddst(int flow_mode)
  534. {
  535. return (flow_mode & CAKE_FLOW_DUAL_DST) == CAKE_FLOW_DUAL_DST;
  536. }
  537. static u32 cake_hash(struct cake_tin_data *q, const struct sk_buff *skb,
  538. int flow_mode)
  539. {
  540. u32 flow_hash = 0, srchost_hash, dsthost_hash;
  541. u16 reduced_hash, srchost_idx, dsthost_idx;
  542. struct flow_keys keys, host_keys;
  543. if (unlikely(flow_mode == CAKE_FLOW_NONE))
  544. return 0;
  545. skb_flow_dissect_flow_keys(skb, &keys,
  546. FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
  547. if (flow_mode & CAKE_FLOW_NAT_FLAG)
  548. cake_update_flowkeys(&keys, skb);
  549. /* flow_hash_from_keys() sorts the addresses by value, so we have
  550. * to preserve their order in a separate data structure to treat
  551. * src and dst host addresses as independently selectable.
  552. */
  553. host_keys = keys;
  554. host_keys.ports.ports = 0;
  555. host_keys.basic.ip_proto = 0;
  556. host_keys.keyid.keyid = 0;
  557. host_keys.tags.flow_label = 0;
  558. switch (host_keys.control.addr_type) {
  559. case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
  560. host_keys.addrs.v4addrs.src = 0;
  561. dsthost_hash = flow_hash_from_keys(&host_keys);
  562. host_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
  563. host_keys.addrs.v4addrs.dst = 0;
  564. srchost_hash = flow_hash_from_keys(&host_keys);
  565. break;
  566. case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
  567. memset(&host_keys.addrs.v6addrs.src, 0,
  568. sizeof(host_keys.addrs.v6addrs.src));
  569. dsthost_hash = flow_hash_from_keys(&host_keys);
  570. host_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
  571. memset(&host_keys.addrs.v6addrs.dst, 0,
  572. sizeof(host_keys.addrs.v6addrs.dst));
  573. srchost_hash = flow_hash_from_keys(&host_keys);
  574. break;
  575. default:
  576. dsthost_hash = 0;
  577. srchost_hash = 0;
  578. }
  579. /* This *must* be after the above switch, since as a
  580. * side-effect it sorts the src and dst addresses.
  581. */
  582. if (flow_mode & CAKE_FLOW_FLOWS)
  583. flow_hash = flow_hash_from_keys(&keys);
  584. if (!(flow_mode & CAKE_FLOW_FLOWS)) {
  585. if (flow_mode & CAKE_FLOW_SRC_IP)
  586. flow_hash ^= srchost_hash;
  587. if (flow_mode & CAKE_FLOW_DST_IP)
  588. flow_hash ^= dsthost_hash;
  589. }
  590. reduced_hash = flow_hash % CAKE_QUEUES;
  591. /* set-associative hashing */
  592. /* fast path if no hash collision (direct lookup succeeds) */
  593. if (likely(q->tags[reduced_hash] == flow_hash &&
  594. q->flows[reduced_hash].set)) {
  595. q->way_directs++;
  596. } else {
  597. u32 inner_hash = reduced_hash % CAKE_SET_WAYS;
  598. u32 outer_hash = reduced_hash - inner_hash;
  599. bool allocate_src = false;
  600. bool allocate_dst = false;
  601. u32 i, k;
  602. /* check if any active queue in the set is reserved for
  603. * this flow.
  604. */
  605. for (i = 0, k = inner_hash; i < CAKE_SET_WAYS;
  606. i++, k = (k + 1) % CAKE_SET_WAYS) {
  607. if (q->tags[outer_hash + k] == flow_hash) {
  608. if (i)
  609. q->way_hits++;
  610. if (!q->flows[outer_hash + k].set) {
  611. /* need to increment host refcnts */
  612. allocate_src = cake_dsrc(flow_mode);
  613. allocate_dst = cake_ddst(flow_mode);
  614. }
  615. goto found;
  616. }
  617. }
  618. /* no queue is reserved for this flow, look for an
  619. * empty one.
  620. */
  621. for (i = 0; i < CAKE_SET_WAYS;
  622. i++, k = (k + 1) % CAKE_SET_WAYS) {
  623. if (!q->flows[outer_hash + k].set) {
  624. q->way_misses++;
  625. allocate_src = cake_dsrc(flow_mode);
  626. allocate_dst = cake_ddst(flow_mode);
  627. goto found;
  628. }
  629. }
  630. /* With no empty queues, default to the original
  631. * queue, accept the collision, update the host tags.
  632. */
  633. q->way_collisions++;
  634. q->hosts[q->flows[reduced_hash].srchost].srchost_refcnt--;
  635. q->hosts[q->flows[reduced_hash].dsthost].dsthost_refcnt--;
  636. allocate_src = cake_dsrc(flow_mode);
  637. allocate_dst = cake_ddst(flow_mode);
  638. found:
  639. /* reserve queue for future packets in same flow */
  640. reduced_hash = outer_hash + k;
  641. q->tags[reduced_hash] = flow_hash;
  642. if (allocate_src) {
  643. srchost_idx = srchost_hash % CAKE_QUEUES;
  644. inner_hash = srchost_idx % CAKE_SET_WAYS;
  645. outer_hash = srchost_idx - inner_hash;
  646. for (i = 0, k = inner_hash; i < CAKE_SET_WAYS;
  647. i++, k = (k + 1) % CAKE_SET_WAYS) {
  648. if (q->hosts[outer_hash + k].srchost_tag ==
  649. srchost_hash)
  650. goto found_src;
  651. }
  652. for (i = 0; i < CAKE_SET_WAYS;
  653. i++, k = (k + 1) % CAKE_SET_WAYS) {
  654. if (!q->hosts[outer_hash + k].srchost_refcnt)
  655. break;
  656. }
  657. q->hosts[outer_hash + k].srchost_tag = srchost_hash;
  658. found_src:
  659. srchost_idx = outer_hash + k;
  660. q->hosts[srchost_idx].srchost_refcnt++;
  661. q->flows[reduced_hash].srchost = srchost_idx;
  662. }
  663. if (allocate_dst) {
  664. dsthost_idx = dsthost_hash % CAKE_QUEUES;
  665. inner_hash = dsthost_idx % CAKE_SET_WAYS;
  666. outer_hash = dsthost_idx - inner_hash;
  667. for (i = 0, k = inner_hash; i < CAKE_SET_WAYS;
  668. i++, k = (k + 1) % CAKE_SET_WAYS) {
  669. if (q->hosts[outer_hash + k].dsthost_tag ==
  670. dsthost_hash)
  671. goto found_dst;
  672. }
  673. for (i = 0; i < CAKE_SET_WAYS;
  674. i++, k = (k + 1) % CAKE_SET_WAYS) {
  675. if (!q->hosts[outer_hash + k].dsthost_refcnt)
  676. break;
  677. }
  678. q->hosts[outer_hash + k].dsthost_tag = dsthost_hash;
  679. found_dst:
  680. dsthost_idx = outer_hash + k;
  681. q->hosts[dsthost_idx].dsthost_refcnt++;
  682. q->flows[reduced_hash].dsthost = dsthost_idx;
  683. }
  684. }
  685. return reduced_hash;
  686. }
  687. /* helper functions : might be changed when/if skb use a standard list_head */
  688. /* remove one skb from head of slot queue */
  689. static struct sk_buff *dequeue_head(struct cake_flow *flow)
  690. {
  691. struct sk_buff *skb = flow->head;
  692. if (skb) {
  693. flow->head = skb->next;
  694. skb->next = NULL;
  695. }
  696. return skb;
  697. }
  698. /* add skb to flow queue (tail add) */
  699. static void flow_queue_add(struct cake_flow *flow, struct sk_buff *skb)
  700. {
  701. if (!flow->head)
  702. flow->head = skb;
  703. else
  704. flow->tail->next = skb;
  705. flow->tail = skb;
  706. skb->next = NULL;
  707. }
  708. static struct iphdr *cake_get_iphdr(const struct sk_buff *skb,
  709. struct ipv6hdr *buf)
  710. {
  711. unsigned int offset = skb_network_offset(skb);
  712. struct iphdr *iph;
  713. iph = skb_header_pointer(skb, offset, sizeof(struct iphdr), buf);
  714. if (!iph)
  715. return NULL;
  716. if (iph->version == 4 && iph->protocol == IPPROTO_IPV6)
  717. return skb_header_pointer(skb, offset + iph->ihl * 4,
  718. sizeof(struct ipv6hdr), buf);
  719. else if (iph->version == 4)
  720. return iph;
  721. else if (iph->version == 6)
  722. return skb_header_pointer(skb, offset, sizeof(struct ipv6hdr),
  723. buf);
  724. return NULL;
  725. }
  726. static struct tcphdr *cake_get_tcphdr(const struct sk_buff *skb,
  727. void *buf, unsigned int bufsize)
  728. {
  729. unsigned int offset = skb_network_offset(skb);
  730. const struct ipv6hdr *ipv6h;
  731. const struct tcphdr *tcph;
  732. const struct iphdr *iph;
  733. struct ipv6hdr _ipv6h;
  734. struct tcphdr _tcph;
  735. ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
  736. if (!ipv6h)
  737. return NULL;
  738. if (ipv6h->version == 4) {
  739. iph = (struct iphdr *)ipv6h;
  740. offset += iph->ihl * 4;
  741. /* special-case 6in4 tunnelling, as that is a common way to get
  742. * v6 connectivity in the home
  743. */
  744. if (iph->protocol == IPPROTO_IPV6) {
  745. ipv6h = skb_header_pointer(skb, offset,
  746. sizeof(_ipv6h), &_ipv6h);
  747. if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
  748. return NULL;
  749. offset += sizeof(struct ipv6hdr);
  750. } else if (iph->protocol != IPPROTO_TCP) {
  751. return NULL;
  752. }
  753. } else if (ipv6h->version == 6) {
  754. if (ipv6h->nexthdr != IPPROTO_TCP)
  755. return NULL;
  756. offset += sizeof(struct ipv6hdr);
  757. } else {
  758. return NULL;
  759. }
  760. tcph = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
  761. if (!tcph)
  762. return NULL;
  763. return skb_header_pointer(skb, offset,
  764. min(__tcp_hdrlen(tcph), bufsize), buf);
  765. }
  766. static const void *cake_get_tcpopt(const struct tcphdr *tcph,
  767. int code, int *oplen)
  768. {
  769. /* inspired by tcp_parse_options in tcp_input.c */
  770. int length = __tcp_hdrlen(tcph) - sizeof(struct tcphdr);
  771. const u8 *ptr = (const u8 *)(tcph + 1);
  772. while (length > 0) {
  773. int opcode = *ptr++;
  774. int opsize;
  775. if (opcode == TCPOPT_EOL)
  776. break;
  777. if (opcode == TCPOPT_NOP) {
  778. length--;
  779. continue;
  780. }
  781. opsize = *ptr++;
  782. if (opsize < 2 || opsize > length)
  783. break;
  784. if (opcode == code) {
  785. *oplen = opsize;
  786. return ptr;
  787. }
  788. ptr += opsize - 2;
  789. length -= opsize;
  790. }
  791. return NULL;
  792. }
  793. /* Compare two SACK sequences. A sequence is considered greater if it SACKs more
  794. * bytes than the other. In the case where both sequences ACKs bytes that the
  795. * other doesn't, A is considered greater. DSACKs in A also makes A be
  796. * considered greater.
  797. *
  798. * @return -1, 0 or 1 as normal compare functions
  799. */
  800. static int cake_tcph_sack_compare(const struct tcphdr *tcph_a,
  801. const struct tcphdr *tcph_b)
  802. {
  803. const struct tcp_sack_block_wire *sack_a, *sack_b;
  804. u32 ack_seq_a = ntohl(tcph_a->ack_seq);
  805. u32 bytes_a = 0, bytes_b = 0;
  806. int oplen_a, oplen_b;
  807. bool first = true;
  808. sack_a = cake_get_tcpopt(tcph_a, TCPOPT_SACK, &oplen_a);
  809. sack_b = cake_get_tcpopt(tcph_b, TCPOPT_SACK, &oplen_b);
  810. /* pointers point to option contents */
  811. oplen_a -= TCPOLEN_SACK_BASE;
  812. oplen_b -= TCPOLEN_SACK_BASE;
  813. if (sack_a && oplen_a >= sizeof(*sack_a) &&
  814. (!sack_b || oplen_b < sizeof(*sack_b)))
  815. return -1;
  816. else if (sack_b && oplen_b >= sizeof(*sack_b) &&
  817. (!sack_a || oplen_a < sizeof(*sack_a)))
  818. return 1;
  819. else if ((!sack_a || oplen_a < sizeof(*sack_a)) &&
  820. (!sack_b || oplen_b < sizeof(*sack_b)))
  821. return 0;
  822. while (oplen_a >= sizeof(*sack_a)) {
  823. const struct tcp_sack_block_wire *sack_tmp = sack_b;
  824. u32 start_a = get_unaligned_be32(&sack_a->start_seq);
  825. u32 end_a = get_unaligned_be32(&sack_a->end_seq);
  826. int oplen_tmp = oplen_b;
  827. bool found = false;
  828. /* DSACK; always considered greater to prevent dropping */
  829. if (before(start_a, ack_seq_a))
  830. return -1;
  831. bytes_a += end_a - start_a;
  832. while (oplen_tmp >= sizeof(*sack_tmp)) {
  833. u32 start_b = get_unaligned_be32(&sack_tmp->start_seq);
  834. u32 end_b = get_unaligned_be32(&sack_tmp->end_seq);
  835. /* first time through we count the total size */
  836. if (first)
  837. bytes_b += end_b - start_b;
  838. if (!after(start_b, start_a) && !before(end_b, end_a)) {
  839. found = true;
  840. if (!first)
  841. break;
  842. }
  843. oplen_tmp -= sizeof(*sack_tmp);
  844. sack_tmp++;
  845. }
  846. if (!found)
  847. return -1;
  848. oplen_a -= sizeof(*sack_a);
  849. sack_a++;
  850. first = false;
  851. }
  852. /* If we made it this far, all ranges SACKed by A are covered by B, so
  853. * either the SACKs are equal, or B SACKs more bytes.
  854. */
  855. return bytes_b > bytes_a ? 1 : 0;
  856. }
  857. static void cake_tcph_get_tstamp(const struct tcphdr *tcph,
  858. u32 *tsval, u32 *tsecr)
  859. {
  860. const u8 *ptr;
  861. int opsize;
  862. ptr = cake_get_tcpopt(tcph, TCPOPT_TIMESTAMP, &opsize);
  863. if (ptr && opsize == TCPOLEN_TIMESTAMP) {
  864. *tsval = get_unaligned_be32(ptr);
  865. *tsecr = get_unaligned_be32(ptr + 4);
  866. }
  867. }
  868. static bool cake_tcph_may_drop(const struct tcphdr *tcph,
  869. u32 tstamp_new, u32 tsecr_new)
  870. {
  871. /* inspired by tcp_parse_options in tcp_input.c */
  872. int length = __tcp_hdrlen(tcph) - sizeof(struct tcphdr);
  873. const u8 *ptr = (const u8 *)(tcph + 1);
  874. u32 tstamp, tsecr;
  875. /* 3 reserved flags must be unset to avoid future breakage
  876. * ACK must be set
  877. * ECE/CWR are handled separately
  878. * All other flags URG/PSH/RST/SYN/FIN must be unset
  879. * 0x0FFF0000 = all TCP flags (confirm ACK=1, others zero)
  880. * 0x00C00000 = CWR/ECE (handled separately)
  881. * 0x0F3F0000 = 0x0FFF0000 & ~0x00C00000
  882. */
  883. if (((tcp_flag_word(tcph) &
  884. cpu_to_be32(0x0F3F0000)) != TCP_FLAG_ACK))
  885. return false;
  886. while (length > 0) {
  887. int opcode = *ptr++;
  888. int opsize;
  889. if (opcode == TCPOPT_EOL)
  890. break;
  891. if (opcode == TCPOPT_NOP) {
  892. length--;
  893. continue;
  894. }
  895. opsize = *ptr++;
  896. if (opsize < 2 || opsize > length)
  897. break;
  898. switch (opcode) {
  899. case TCPOPT_MD5SIG: /* doesn't influence state */
  900. break;
  901. case TCPOPT_SACK: /* stricter checking performed later */
  902. if (opsize % 8 != 2)
  903. return false;
  904. break;
  905. case TCPOPT_TIMESTAMP:
  906. /* only drop timestamps lower than new */
  907. if (opsize != TCPOLEN_TIMESTAMP)
  908. return false;
  909. tstamp = get_unaligned_be32(ptr);
  910. tsecr = get_unaligned_be32(ptr + 4);
  911. if (after(tstamp, tstamp_new) ||
  912. after(tsecr, tsecr_new))
  913. return false;
  914. break;
  915. case TCPOPT_MSS: /* these should only be set on SYN */
  916. case TCPOPT_WINDOW:
  917. case TCPOPT_SACK_PERM:
  918. case TCPOPT_FASTOPEN:
  919. case TCPOPT_EXP:
  920. default: /* don't drop if any unknown options are present */
  921. return false;
  922. }
  923. ptr += opsize - 2;
  924. length -= opsize;
  925. }
  926. return true;
  927. }
  928. static struct sk_buff *cake_ack_filter(struct cake_sched_data *q,
  929. struct cake_flow *flow)
  930. {
  931. bool aggressive = q->ack_filter == CAKE_ACK_AGGRESSIVE;
  932. struct sk_buff *elig_ack = NULL, *elig_ack_prev = NULL;
  933. struct sk_buff *skb_check, *skb_prev = NULL;
  934. const struct ipv6hdr *ipv6h, *ipv6h_check;
  935. unsigned char _tcph[64], _tcph_check[64];
  936. const struct tcphdr *tcph, *tcph_check;
  937. const struct iphdr *iph, *iph_check;
  938. struct ipv6hdr _iph, _iph_check;
  939. const struct sk_buff *skb;
  940. int seglen, num_found = 0;
  941. u32 tstamp = 0, tsecr = 0;
  942. __be32 elig_flags = 0;
  943. int sack_comp;
  944. /* no other possible ACKs to filter */
  945. if (flow->head == flow->tail)
  946. return NULL;
  947. skb = flow->tail;
  948. tcph = cake_get_tcphdr(skb, _tcph, sizeof(_tcph));
  949. iph = cake_get_iphdr(skb, &_iph);
  950. if (!tcph)
  951. return NULL;
  952. cake_tcph_get_tstamp(tcph, &tstamp, &tsecr);
  953. /* the 'triggering' packet need only have the ACK flag set.
  954. * also check that SYN is not set, as there won't be any previous ACKs.
  955. */
  956. if ((tcp_flag_word(tcph) &
  957. (TCP_FLAG_ACK | TCP_FLAG_SYN)) != TCP_FLAG_ACK)
  958. return NULL;
  959. /* the 'triggering' ACK is at the tail of the queue, we have already
  960. * returned if it is the only packet in the flow. loop through the rest
  961. * of the queue looking for pure ACKs with the same 5-tuple as the
  962. * triggering one.
  963. */
  964. for (skb_check = flow->head;
  965. skb_check && skb_check != skb;
  966. skb_prev = skb_check, skb_check = skb_check->next) {
  967. iph_check = cake_get_iphdr(skb_check, &_iph_check);
  968. tcph_check = cake_get_tcphdr(skb_check, &_tcph_check,
  969. sizeof(_tcph_check));
  970. /* only TCP packets with matching 5-tuple are eligible, and only
  971. * drop safe headers
  972. */
  973. if (!tcph_check || iph->version != iph_check->version ||
  974. tcph_check->source != tcph->source ||
  975. tcph_check->dest != tcph->dest)
  976. continue;
  977. if (iph_check->version == 4) {
  978. if (iph_check->saddr != iph->saddr ||
  979. iph_check->daddr != iph->daddr)
  980. continue;
  981. seglen = ntohs(iph_check->tot_len) -
  982. (4 * iph_check->ihl);
  983. } else if (iph_check->version == 6) {
  984. ipv6h = (struct ipv6hdr *)iph;
  985. ipv6h_check = (struct ipv6hdr *)iph_check;
  986. if (ipv6_addr_cmp(&ipv6h_check->saddr, &ipv6h->saddr) ||
  987. ipv6_addr_cmp(&ipv6h_check->daddr, &ipv6h->daddr))
  988. continue;
  989. seglen = ntohs(ipv6h_check->payload_len);
  990. } else {
  991. WARN_ON(1); /* shouldn't happen */
  992. continue;
  993. }
  994. /* If the ECE/CWR flags changed from the previous eligible
  995. * packet in the same flow, we should no longer be dropping that
  996. * previous packet as this would lose information.
  997. */
  998. if (elig_ack && (tcp_flag_word(tcph_check) &
  999. (TCP_FLAG_ECE | TCP_FLAG_CWR)) != elig_flags) {
  1000. elig_ack = NULL;
  1001. elig_ack_prev = NULL;
  1002. num_found--;
  1003. }
  1004. /* Check TCP options and flags, don't drop ACKs with segment
  1005. * data, and don't drop ACKs with a higher cumulative ACK
  1006. * counter than the triggering packet. Check ACK seqno here to
  1007. * avoid parsing SACK options of packets we are going to exclude
  1008. * anyway.
  1009. */
  1010. if (!cake_tcph_may_drop(tcph_check, tstamp, tsecr) ||
  1011. (seglen - __tcp_hdrlen(tcph_check)) != 0 ||
  1012. after(ntohl(tcph_check->ack_seq), ntohl(tcph->ack_seq)))
  1013. continue;
  1014. /* Check SACK options. The triggering packet must SACK more data
  1015. * than the ACK under consideration, or SACK the same range but
  1016. * have a larger cumulative ACK counter. The latter is a
  1017. * pathological case, but is contained in the following check
  1018. * anyway, just to be safe.
  1019. */
  1020. sack_comp = cake_tcph_sack_compare(tcph_check, tcph);
  1021. if (sack_comp < 0 ||
  1022. (ntohl(tcph_check->ack_seq) == ntohl(tcph->ack_seq) &&
  1023. sack_comp == 0))
  1024. continue;
  1025. /* At this point we have found an eligible pure ACK to drop; if
  1026. * we are in aggressive mode, we are done. Otherwise, keep
  1027. * searching unless this is the second eligible ACK we
  1028. * found.
  1029. *
  1030. * Since we want to drop ACK closest to the head of the queue,
  1031. * save the first eligible ACK we find, even if we need to loop
  1032. * again.
  1033. */
  1034. if (!elig_ack) {
  1035. elig_ack = skb_check;
  1036. elig_ack_prev = skb_prev;
  1037. elig_flags = (tcp_flag_word(tcph_check)
  1038. & (TCP_FLAG_ECE | TCP_FLAG_CWR));
  1039. }
  1040. if (num_found++ > 0)
  1041. goto found;
  1042. }
  1043. /* We made it through the queue without finding two eligible ACKs . If
  1044. * we found a single eligible ACK we can drop it in aggressive mode if
  1045. * we can guarantee that this does not interfere with ECN flag
  1046. * information. We ensure this by dropping it only if the enqueued
  1047. * packet is consecutive with the eligible ACK, and their flags match.
  1048. */
  1049. if (elig_ack && aggressive && elig_ack->next == skb &&
  1050. (elig_flags == (tcp_flag_word(tcph) &
  1051. (TCP_FLAG_ECE | TCP_FLAG_CWR))))
  1052. goto found;
  1053. return NULL;
  1054. found:
  1055. if (elig_ack_prev)
  1056. elig_ack_prev->next = elig_ack->next;
  1057. else
  1058. flow->head = elig_ack->next;
  1059. elig_ack->next = NULL;
  1060. return elig_ack;
  1061. }
  1062. static u64 cake_ewma(u64 avg, u64 sample, u32 shift)
  1063. {
  1064. avg -= avg >> shift;
  1065. avg += sample >> shift;
  1066. return avg;
  1067. }
  1068. static u32 cake_calc_overhead(struct cake_sched_data *q, u32 len, u32 off)
  1069. {
  1070. if (q->rate_flags & CAKE_FLAG_OVERHEAD)
  1071. len -= off;
  1072. if (q->max_netlen < len)
  1073. q->max_netlen = len;
  1074. if (q->min_netlen > len)
  1075. q->min_netlen = len;
  1076. len += q->rate_overhead;
  1077. if (len < q->rate_mpu)
  1078. len = q->rate_mpu;
  1079. if (q->atm_mode == CAKE_ATM_ATM) {
  1080. len += 47;
  1081. len /= 48;
  1082. len *= 53;
  1083. } else if (q->atm_mode == CAKE_ATM_PTM) {
  1084. /* Add one byte per 64 bytes or part thereof.
  1085. * This is conservative and easier to calculate than the
  1086. * precise value.
  1087. */
  1088. len += (len + 63) / 64;
  1089. }
  1090. if (q->max_adjlen < len)
  1091. q->max_adjlen = len;
  1092. if (q->min_adjlen > len)
  1093. q->min_adjlen = len;
  1094. return len;
  1095. }
  1096. static u32 cake_overhead(struct cake_sched_data *q, const struct sk_buff *skb)
  1097. {
  1098. const struct skb_shared_info *shinfo = skb_shinfo(skb);
  1099. unsigned int hdr_len, last_len = 0;
  1100. u32 off = skb_network_offset(skb);
  1101. u32 len = qdisc_pkt_len(skb);
  1102. u16 segs = 1;
  1103. q->avg_netoff = cake_ewma(q->avg_netoff, off << 16, 8);
  1104. if (!shinfo->gso_size)
  1105. return cake_calc_overhead(q, len, off);
  1106. /* borrowed from qdisc_pkt_len_init() */
  1107. hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
  1108. /* + transport layer */
  1109. if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 |
  1110. SKB_GSO_TCPV6))) {
  1111. const struct tcphdr *th;
  1112. struct tcphdr _tcphdr;
  1113. th = skb_header_pointer(skb, skb_transport_offset(skb),
  1114. sizeof(_tcphdr), &_tcphdr);
  1115. if (likely(th))
  1116. hdr_len += __tcp_hdrlen(th);
  1117. } else {
  1118. struct udphdr _udphdr;
  1119. if (skb_header_pointer(skb, skb_transport_offset(skb),
  1120. sizeof(_udphdr), &_udphdr))
  1121. hdr_len += sizeof(struct udphdr);
  1122. }
  1123. if (unlikely(shinfo->gso_type & SKB_GSO_DODGY))
  1124. segs = DIV_ROUND_UP(skb->len - hdr_len,
  1125. shinfo->gso_size);
  1126. else
  1127. segs = shinfo->gso_segs;
  1128. len = shinfo->gso_size + hdr_len;
  1129. last_len = skb->len - shinfo->gso_size * (segs - 1);
  1130. return (cake_calc_overhead(q, len, off) * (segs - 1) +
  1131. cake_calc_overhead(q, last_len, off));
  1132. }
  1133. static void cake_heap_swap(struct cake_sched_data *q, u16 i, u16 j)
  1134. {
  1135. struct cake_heap_entry ii = q->overflow_heap[i];
  1136. struct cake_heap_entry jj = q->overflow_heap[j];
  1137. q->overflow_heap[i] = jj;
  1138. q->overflow_heap[j] = ii;
  1139. q->tins[ii.t].overflow_idx[ii.b] = j;
  1140. q->tins[jj.t].overflow_idx[jj.b] = i;
  1141. }
  1142. static u32 cake_heap_get_backlog(const struct cake_sched_data *q, u16 i)
  1143. {
  1144. struct cake_heap_entry ii = q->overflow_heap[i];
  1145. return q->tins[ii.t].backlogs[ii.b];
  1146. }
  1147. static void cake_heapify(struct cake_sched_data *q, u16 i)
  1148. {
  1149. static const u32 a = CAKE_MAX_TINS * CAKE_QUEUES;
  1150. u32 mb = cake_heap_get_backlog(q, i);
  1151. u32 m = i;
  1152. while (m < a) {
  1153. u32 l = m + m + 1;
  1154. u32 r = l + 1;
  1155. if (l < a) {
  1156. u32 lb = cake_heap_get_backlog(q, l);
  1157. if (lb > mb) {
  1158. m = l;
  1159. mb = lb;
  1160. }
  1161. }
  1162. if (r < a) {
  1163. u32 rb = cake_heap_get_backlog(q, r);
  1164. if (rb > mb) {
  1165. m = r;
  1166. mb = rb;
  1167. }
  1168. }
  1169. if (m != i) {
  1170. cake_heap_swap(q, i, m);
  1171. i = m;
  1172. } else {
  1173. break;
  1174. }
  1175. }
  1176. }
  1177. static void cake_heapify_up(struct cake_sched_data *q, u16 i)
  1178. {
  1179. while (i > 0 && i < CAKE_MAX_TINS * CAKE_QUEUES) {
  1180. u16 p = (i - 1) >> 1;
  1181. u32 ib = cake_heap_get_backlog(q, i);
  1182. u32 pb = cake_heap_get_backlog(q, p);
  1183. if (ib > pb) {
  1184. cake_heap_swap(q, i, p);
  1185. i = p;
  1186. } else {
  1187. break;
  1188. }
  1189. }
  1190. }
  1191. static int cake_advance_shaper(struct cake_sched_data *q,
  1192. struct cake_tin_data *b,
  1193. struct sk_buff *skb,
  1194. ktime_t now, bool drop)
  1195. {
  1196. u32 len = get_cobalt_cb(skb)->adjusted_len;
  1197. /* charge packet bandwidth to this tin
  1198. * and to the global shaper.
  1199. */
  1200. if (q->rate_ns) {
  1201. u64 tin_dur = (len * b->tin_rate_ns) >> b->tin_rate_shft;
  1202. u64 global_dur = (len * q->rate_ns) >> q->rate_shft;
  1203. u64 failsafe_dur = global_dur + (global_dur >> 1);
  1204. if (ktime_before(b->time_next_packet, now))
  1205. b->time_next_packet = ktime_add_ns(b->time_next_packet,
  1206. tin_dur);
  1207. else if (ktime_before(b->time_next_packet,
  1208. ktime_add_ns(now, tin_dur)))
  1209. b->time_next_packet = ktime_add_ns(now, tin_dur);
  1210. q->time_next_packet = ktime_add_ns(q->time_next_packet,
  1211. global_dur);
  1212. if (!drop)
  1213. q->failsafe_next_packet = \
  1214. ktime_add_ns(q->failsafe_next_packet,
  1215. failsafe_dur);
  1216. }
  1217. return len;
  1218. }
  1219. static unsigned int cake_drop(struct Qdisc *sch, struct sk_buff **to_free)
  1220. {
  1221. struct cake_sched_data *q = qdisc_priv(sch);
  1222. ktime_t now = ktime_get();
  1223. u32 idx = 0, tin = 0, len;
  1224. struct cake_heap_entry qq;
  1225. struct cake_tin_data *b;
  1226. struct cake_flow *flow;
  1227. struct sk_buff *skb;
  1228. if (!q->overflow_timeout) {
  1229. int i;
  1230. /* Build fresh max-heap */
  1231. for (i = CAKE_MAX_TINS * CAKE_QUEUES / 2; i >= 0; i--)
  1232. cake_heapify(q, i);
  1233. }
  1234. q->overflow_timeout = 65535;
  1235. /* select longest queue for pruning */
  1236. qq = q->overflow_heap[0];
  1237. tin = qq.t;
  1238. idx = qq.b;
  1239. b = &q->tins[tin];
  1240. flow = &b->flows[idx];
  1241. skb = dequeue_head(flow);
  1242. if (unlikely(!skb)) {
  1243. /* heap has gone wrong, rebuild it next time */
  1244. q->overflow_timeout = 0;
  1245. return idx + (tin << 16);
  1246. }
  1247. if (cobalt_queue_full(&flow->cvars, &b->cparams, now))
  1248. b->unresponsive_flow_count++;
  1249. len = qdisc_pkt_len(skb);
  1250. q->buffer_used -= skb->truesize;
  1251. b->backlogs[idx] -= len;
  1252. b->tin_backlog -= len;
  1253. sch->qstats.backlog -= len;
  1254. qdisc_tree_reduce_backlog(sch, 1, len);
  1255. flow->dropped++;
  1256. b->tin_dropped++;
  1257. sch->qstats.drops++;
  1258. if (q->rate_flags & CAKE_FLAG_INGRESS)
  1259. cake_advance_shaper(q, b, skb, now, true);
  1260. __qdisc_drop(skb, to_free);
  1261. sch->q.qlen--;
  1262. cake_heapify(q, 0);
  1263. return idx + (tin << 16);
  1264. }
  1265. static void cake_wash_diffserv(struct sk_buff *skb)
  1266. {
  1267. switch (skb->protocol) {
  1268. case htons(ETH_P_IP):
  1269. ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, 0);
  1270. break;
  1271. case htons(ETH_P_IPV6):
  1272. ipv6_change_dsfield(ipv6_hdr(skb), INET_ECN_MASK, 0);
  1273. break;
  1274. default:
  1275. break;
  1276. }
  1277. }
  1278. static u8 cake_handle_diffserv(struct sk_buff *skb, u16 wash)
  1279. {
  1280. u8 dscp;
  1281. switch (skb->protocol) {
  1282. case htons(ETH_P_IP):
  1283. dscp = ipv4_get_dsfield(ip_hdr(skb)) >> 2;
  1284. if (wash && dscp)
  1285. ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, 0);
  1286. return dscp;
  1287. case htons(ETH_P_IPV6):
  1288. dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2;
  1289. if (wash && dscp)
  1290. ipv6_change_dsfield(ipv6_hdr(skb), INET_ECN_MASK, 0);
  1291. return dscp;
  1292. case htons(ETH_P_ARP):
  1293. return 0x38; /* CS7 - Net Control */
  1294. default:
  1295. /* If there is no Diffserv field, treat as best-effort */
  1296. return 0;
  1297. }
  1298. }
  1299. static struct cake_tin_data *cake_select_tin(struct Qdisc *sch,
  1300. struct sk_buff *skb)
  1301. {
  1302. struct cake_sched_data *q = qdisc_priv(sch);
  1303. u32 tin;
  1304. if (TC_H_MAJ(skb->priority) == sch->handle &&
  1305. TC_H_MIN(skb->priority) > 0 &&
  1306. TC_H_MIN(skb->priority) <= q->tin_cnt) {
  1307. tin = q->tin_order[TC_H_MIN(skb->priority) - 1];
  1308. if (q->rate_flags & CAKE_FLAG_WASH)
  1309. cake_wash_diffserv(skb);
  1310. } else if (q->tin_mode != CAKE_DIFFSERV_BESTEFFORT) {
  1311. /* extract the Diffserv Precedence field, if it exists */
  1312. /* and clear DSCP bits if washing */
  1313. tin = q->tin_index[cake_handle_diffserv(skb,
  1314. q->rate_flags & CAKE_FLAG_WASH)];
  1315. if (unlikely(tin >= q->tin_cnt))
  1316. tin = 0;
  1317. } else {
  1318. tin = 0;
  1319. if (q->rate_flags & CAKE_FLAG_WASH)
  1320. cake_wash_diffserv(skb);
  1321. }
  1322. return &q->tins[tin];
  1323. }
  1324. static u32 cake_classify(struct Qdisc *sch, struct cake_tin_data **t,
  1325. struct sk_buff *skb, int flow_mode, int *qerr)
  1326. {
  1327. struct cake_sched_data *q = qdisc_priv(sch);
  1328. struct tcf_proto *filter;
  1329. struct tcf_result res;
  1330. u32 flow = 0;
  1331. int result;
  1332. filter = rcu_dereference_bh(q->filter_list);
  1333. if (!filter)
  1334. goto hash;
  1335. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  1336. result = tcf_classify(skb, filter, &res, false);
  1337. if (result >= 0) {
  1338. #ifdef CONFIG_NET_CLS_ACT
  1339. switch (result) {
  1340. case TC_ACT_STOLEN:
  1341. case TC_ACT_QUEUED:
  1342. case TC_ACT_TRAP:
  1343. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  1344. /* fall through */
  1345. case TC_ACT_SHOT:
  1346. return 0;
  1347. }
  1348. #endif
  1349. if (TC_H_MIN(res.classid) <= CAKE_QUEUES)
  1350. flow = TC_H_MIN(res.classid);
  1351. }
  1352. hash:
  1353. *t = cake_select_tin(sch, skb);
  1354. return flow ?: cake_hash(*t, skb, flow_mode) + 1;
  1355. }
  1356. static void cake_reconfigure(struct Qdisc *sch);
  1357. static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  1358. struct sk_buff **to_free)
  1359. {
  1360. struct cake_sched_data *q = qdisc_priv(sch);
  1361. int len = qdisc_pkt_len(skb);
  1362. int uninitialized_var(ret);
  1363. struct sk_buff *ack = NULL;
  1364. ktime_t now = ktime_get();
  1365. struct cake_tin_data *b;
  1366. struct cake_flow *flow;
  1367. u32 idx;
  1368. /* choose flow to insert into */
  1369. idx = cake_classify(sch, &b, skb, q->flow_mode, &ret);
  1370. if (idx == 0) {
  1371. if (ret & __NET_XMIT_BYPASS)
  1372. qdisc_qstats_drop(sch);
  1373. __qdisc_drop(skb, to_free);
  1374. return ret;
  1375. }
  1376. idx--;
  1377. flow = &b->flows[idx];
  1378. /* ensure shaper state isn't stale */
  1379. if (!b->tin_backlog) {
  1380. if (ktime_before(b->time_next_packet, now))
  1381. b->time_next_packet = now;
  1382. if (!sch->q.qlen) {
  1383. if (ktime_before(q->time_next_packet, now)) {
  1384. q->failsafe_next_packet = now;
  1385. q->time_next_packet = now;
  1386. } else if (ktime_after(q->time_next_packet, now) &&
  1387. ktime_after(q->failsafe_next_packet, now)) {
  1388. u64 next = \
  1389. min(ktime_to_ns(q->time_next_packet),
  1390. ktime_to_ns(
  1391. q->failsafe_next_packet));
  1392. sch->qstats.overlimits++;
  1393. qdisc_watchdog_schedule_ns(&q->watchdog, next);
  1394. }
  1395. }
  1396. }
  1397. if (unlikely(len > b->max_skblen))
  1398. b->max_skblen = len;
  1399. if (skb_is_gso(skb) && q->rate_flags & CAKE_FLAG_SPLIT_GSO) {
  1400. struct sk_buff *segs, *nskb;
  1401. netdev_features_t features = netif_skb_features(skb);
  1402. unsigned int slen = 0;
  1403. segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
  1404. if (IS_ERR_OR_NULL(segs))
  1405. return qdisc_drop(skb, sch, to_free);
  1406. while (segs) {
  1407. nskb = segs->next;
  1408. segs->next = NULL;
  1409. qdisc_skb_cb(segs)->pkt_len = segs->len;
  1410. cobalt_set_enqueue_time(segs, now);
  1411. get_cobalt_cb(segs)->adjusted_len = cake_overhead(q,
  1412. segs);
  1413. flow_queue_add(flow, segs);
  1414. sch->q.qlen++;
  1415. slen += segs->len;
  1416. q->buffer_used += segs->truesize;
  1417. b->packets++;
  1418. segs = nskb;
  1419. }
  1420. /* stats */
  1421. b->bytes += slen;
  1422. b->backlogs[idx] += slen;
  1423. b->tin_backlog += slen;
  1424. sch->qstats.backlog += slen;
  1425. q->avg_window_bytes += slen;
  1426. qdisc_tree_reduce_backlog(sch, 1, len);
  1427. consume_skb(skb);
  1428. } else {
  1429. /* not splitting */
  1430. cobalt_set_enqueue_time(skb, now);
  1431. get_cobalt_cb(skb)->adjusted_len = cake_overhead(q, skb);
  1432. flow_queue_add(flow, skb);
  1433. if (q->ack_filter)
  1434. ack = cake_ack_filter(q, flow);
  1435. if (ack) {
  1436. b->ack_drops++;
  1437. sch->qstats.drops++;
  1438. b->bytes += qdisc_pkt_len(ack);
  1439. len -= qdisc_pkt_len(ack);
  1440. q->buffer_used += skb->truesize - ack->truesize;
  1441. if (q->rate_flags & CAKE_FLAG_INGRESS)
  1442. cake_advance_shaper(q, b, ack, now, true);
  1443. qdisc_tree_reduce_backlog(sch, 1, qdisc_pkt_len(ack));
  1444. consume_skb(ack);
  1445. } else {
  1446. sch->q.qlen++;
  1447. q->buffer_used += skb->truesize;
  1448. }
  1449. /* stats */
  1450. b->packets++;
  1451. b->bytes += len;
  1452. b->backlogs[idx] += len;
  1453. b->tin_backlog += len;
  1454. sch->qstats.backlog += len;
  1455. q->avg_window_bytes += len;
  1456. }
  1457. if (q->overflow_timeout)
  1458. cake_heapify_up(q, b->overflow_idx[idx]);
  1459. /* incoming bandwidth capacity estimate */
  1460. if (q->rate_flags & CAKE_FLAG_AUTORATE_INGRESS) {
  1461. u64 packet_interval = \
  1462. ktime_to_ns(ktime_sub(now, q->last_packet_time));
  1463. if (packet_interval > NSEC_PER_SEC)
  1464. packet_interval = NSEC_PER_SEC;
  1465. /* filter out short-term bursts, eg. wifi aggregation */
  1466. q->avg_packet_interval = \
  1467. cake_ewma(q->avg_packet_interval,
  1468. packet_interval,
  1469. (packet_interval > q->avg_packet_interval ?
  1470. 2 : 8));
  1471. q->last_packet_time = now;
  1472. if (packet_interval > q->avg_packet_interval) {
  1473. u64 window_interval = \
  1474. ktime_to_ns(ktime_sub(now,
  1475. q->avg_window_begin));
  1476. u64 b = q->avg_window_bytes * (u64)NSEC_PER_SEC;
  1477. do_div(b, window_interval);
  1478. q->avg_peak_bandwidth =
  1479. cake_ewma(q->avg_peak_bandwidth, b,
  1480. b > q->avg_peak_bandwidth ? 2 : 8);
  1481. q->avg_window_bytes = 0;
  1482. q->avg_window_begin = now;
  1483. if (ktime_after(now,
  1484. ktime_add_ms(q->last_reconfig_time,
  1485. 250))) {
  1486. q->rate_bps = (q->avg_peak_bandwidth * 15) >> 4;
  1487. cake_reconfigure(sch);
  1488. }
  1489. }
  1490. } else {
  1491. q->avg_window_bytes = 0;
  1492. q->last_packet_time = now;
  1493. }
  1494. /* flowchain */
  1495. if (!flow->set || flow->set == CAKE_SET_DECAYING) {
  1496. struct cake_host *srchost = &b->hosts[flow->srchost];
  1497. struct cake_host *dsthost = &b->hosts[flow->dsthost];
  1498. u16 host_load = 1;
  1499. if (!flow->set) {
  1500. list_add_tail(&flow->flowchain, &b->new_flows);
  1501. } else {
  1502. b->decaying_flow_count--;
  1503. list_move_tail(&flow->flowchain, &b->new_flows);
  1504. }
  1505. flow->set = CAKE_SET_SPARSE;
  1506. b->sparse_flow_count++;
  1507. if (cake_dsrc(q->flow_mode))
  1508. host_load = max(host_load, srchost->srchost_refcnt);
  1509. if (cake_ddst(q->flow_mode))
  1510. host_load = max(host_load, dsthost->dsthost_refcnt);
  1511. flow->deficit = (b->flow_quantum *
  1512. quantum_div[host_load]) >> 16;
  1513. } else if (flow->set == CAKE_SET_SPARSE_WAIT) {
  1514. /* this flow was empty, accounted as a sparse flow, but actually
  1515. * in the bulk rotation.
  1516. */
  1517. flow->set = CAKE_SET_BULK;
  1518. b->sparse_flow_count--;
  1519. b->bulk_flow_count++;
  1520. }
  1521. if (q->buffer_used > q->buffer_max_used)
  1522. q->buffer_max_used = q->buffer_used;
  1523. if (q->buffer_used > q->buffer_limit) {
  1524. u32 dropped = 0;
  1525. while (q->buffer_used > q->buffer_limit) {
  1526. dropped++;
  1527. cake_drop(sch, to_free);
  1528. }
  1529. b->drop_overlimit += dropped;
  1530. }
  1531. return NET_XMIT_SUCCESS;
  1532. }
  1533. static struct sk_buff *cake_dequeue_one(struct Qdisc *sch)
  1534. {
  1535. struct cake_sched_data *q = qdisc_priv(sch);
  1536. struct cake_tin_data *b = &q->tins[q->cur_tin];
  1537. struct cake_flow *flow = &b->flows[q->cur_flow];
  1538. struct sk_buff *skb = NULL;
  1539. u32 len;
  1540. if (flow->head) {
  1541. skb = dequeue_head(flow);
  1542. len = qdisc_pkt_len(skb);
  1543. b->backlogs[q->cur_flow] -= len;
  1544. b->tin_backlog -= len;
  1545. sch->qstats.backlog -= len;
  1546. q->buffer_used -= skb->truesize;
  1547. sch->q.qlen--;
  1548. if (q->overflow_timeout)
  1549. cake_heapify(q, b->overflow_idx[q->cur_flow]);
  1550. }
  1551. return skb;
  1552. }
  1553. /* Discard leftover packets from a tin no longer in use. */
  1554. static void cake_clear_tin(struct Qdisc *sch, u16 tin)
  1555. {
  1556. struct cake_sched_data *q = qdisc_priv(sch);
  1557. struct sk_buff *skb;
  1558. q->cur_tin = tin;
  1559. for (q->cur_flow = 0; q->cur_flow < CAKE_QUEUES; q->cur_flow++)
  1560. while (!!(skb = cake_dequeue_one(sch)))
  1561. kfree_skb(skb);
  1562. }
  1563. static struct sk_buff *cake_dequeue(struct Qdisc *sch)
  1564. {
  1565. struct cake_sched_data *q = qdisc_priv(sch);
  1566. struct cake_tin_data *b = &q->tins[q->cur_tin];
  1567. struct cake_host *srchost, *dsthost;
  1568. ktime_t now = ktime_get();
  1569. struct cake_flow *flow;
  1570. struct list_head *head;
  1571. bool first_flow = true;
  1572. struct sk_buff *skb;
  1573. u16 host_load;
  1574. u64 delay;
  1575. u32 len;
  1576. begin:
  1577. if (!sch->q.qlen)
  1578. return NULL;
  1579. /* global hard shaper */
  1580. if (ktime_after(q->time_next_packet, now) &&
  1581. ktime_after(q->failsafe_next_packet, now)) {
  1582. u64 next = min(ktime_to_ns(q->time_next_packet),
  1583. ktime_to_ns(q->failsafe_next_packet));
  1584. sch->qstats.overlimits++;
  1585. qdisc_watchdog_schedule_ns(&q->watchdog, next);
  1586. return NULL;
  1587. }
  1588. /* Choose a class to work on. */
  1589. if (!q->rate_ns) {
  1590. /* In unlimited mode, can't rely on shaper timings, just balance
  1591. * with DRR
  1592. */
  1593. bool wrapped = false, empty = true;
  1594. while (b->tin_deficit < 0 ||
  1595. !(b->sparse_flow_count + b->bulk_flow_count)) {
  1596. if (b->tin_deficit <= 0)
  1597. b->tin_deficit += b->tin_quantum_band;
  1598. if (b->sparse_flow_count + b->bulk_flow_count)
  1599. empty = false;
  1600. q->cur_tin++;
  1601. b++;
  1602. if (q->cur_tin >= q->tin_cnt) {
  1603. q->cur_tin = 0;
  1604. b = q->tins;
  1605. if (wrapped) {
  1606. /* It's possible for q->qlen to be
  1607. * nonzero when we actually have no
  1608. * packets anywhere.
  1609. */
  1610. if (empty)
  1611. return NULL;
  1612. } else {
  1613. wrapped = true;
  1614. }
  1615. }
  1616. }
  1617. } else {
  1618. /* In shaped mode, choose:
  1619. * - Highest-priority tin with queue and meeting schedule, or
  1620. * - The earliest-scheduled tin with queue.
  1621. */
  1622. ktime_t best_time = KTIME_MAX;
  1623. int tin, best_tin = 0;
  1624. for (tin = 0; tin < q->tin_cnt; tin++) {
  1625. b = q->tins + tin;
  1626. if ((b->sparse_flow_count + b->bulk_flow_count) > 0) {
  1627. ktime_t time_to_pkt = \
  1628. ktime_sub(b->time_next_packet, now);
  1629. if (ktime_to_ns(time_to_pkt) <= 0 ||
  1630. ktime_compare(time_to_pkt,
  1631. best_time) <= 0) {
  1632. best_time = time_to_pkt;
  1633. best_tin = tin;
  1634. }
  1635. }
  1636. }
  1637. q->cur_tin = best_tin;
  1638. b = q->tins + best_tin;
  1639. /* No point in going further if no packets to deliver. */
  1640. if (unlikely(!(b->sparse_flow_count + b->bulk_flow_count)))
  1641. return NULL;
  1642. }
  1643. retry:
  1644. /* service this class */
  1645. head = &b->decaying_flows;
  1646. if (!first_flow || list_empty(head)) {
  1647. head = &b->new_flows;
  1648. if (list_empty(head)) {
  1649. head = &b->old_flows;
  1650. if (unlikely(list_empty(head))) {
  1651. head = &b->decaying_flows;
  1652. if (unlikely(list_empty(head)))
  1653. goto begin;
  1654. }
  1655. }
  1656. }
  1657. flow = list_first_entry(head, struct cake_flow, flowchain);
  1658. q->cur_flow = flow - b->flows;
  1659. first_flow = false;
  1660. /* triple isolation (modified DRR++) */
  1661. srchost = &b->hosts[flow->srchost];
  1662. dsthost = &b->hosts[flow->dsthost];
  1663. host_load = 1;
  1664. if (cake_dsrc(q->flow_mode))
  1665. host_load = max(host_load, srchost->srchost_refcnt);
  1666. if (cake_ddst(q->flow_mode))
  1667. host_load = max(host_load, dsthost->dsthost_refcnt);
  1668. WARN_ON(host_load > CAKE_QUEUES);
  1669. /* flow isolation (DRR++) */
  1670. if (flow->deficit <= 0) {
  1671. /* The shifted prandom_u32() is a way to apply dithering to
  1672. * avoid accumulating roundoff errors
  1673. */
  1674. flow->deficit += (b->flow_quantum * quantum_div[host_load] +
  1675. (prandom_u32() >> 16)) >> 16;
  1676. list_move_tail(&flow->flowchain, &b->old_flows);
  1677. /* Keep all flows with deficits out of the sparse and decaying
  1678. * rotations. No non-empty flow can go into the decaying
  1679. * rotation, so they can't get deficits
  1680. */
  1681. if (flow->set == CAKE_SET_SPARSE) {
  1682. if (flow->head) {
  1683. b->sparse_flow_count--;
  1684. b->bulk_flow_count++;
  1685. flow->set = CAKE_SET_BULK;
  1686. } else {
  1687. /* we've moved it to the bulk rotation for
  1688. * correct deficit accounting but we still want
  1689. * to count it as a sparse flow, not a bulk one.
  1690. */
  1691. flow->set = CAKE_SET_SPARSE_WAIT;
  1692. }
  1693. }
  1694. goto retry;
  1695. }
  1696. /* Retrieve a packet via the AQM */
  1697. while (1) {
  1698. skb = cake_dequeue_one(sch);
  1699. if (!skb) {
  1700. /* this queue was actually empty */
  1701. if (cobalt_queue_empty(&flow->cvars, &b->cparams, now))
  1702. b->unresponsive_flow_count--;
  1703. if (flow->cvars.p_drop || flow->cvars.count ||
  1704. ktime_before(now, flow->cvars.drop_next)) {
  1705. /* keep in the flowchain until the state has
  1706. * decayed to rest
  1707. */
  1708. list_move_tail(&flow->flowchain,
  1709. &b->decaying_flows);
  1710. if (flow->set == CAKE_SET_BULK) {
  1711. b->bulk_flow_count--;
  1712. b->decaying_flow_count++;
  1713. } else if (flow->set == CAKE_SET_SPARSE ||
  1714. flow->set == CAKE_SET_SPARSE_WAIT) {
  1715. b->sparse_flow_count--;
  1716. b->decaying_flow_count++;
  1717. }
  1718. flow->set = CAKE_SET_DECAYING;
  1719. } else {
  1720. /* remove empty queue from the flowchain */
  1721. list_del_init(&flow->flowchain);
  1722. if (flow->set == CAKE_SET_SPARSE ||
  1723. flow->set == CAKE_SET_SPARSE_WAIT)
  1724. b->sparse_flow_count--;
  1725. else if (flow->set == CAKE_SET_BULK)
  1726. b->bulk_flow_count--;
  1727. else
  1728. b->decaying_flow_count--;
  1729. flow->set = CAKE_SET_NONE;
  1730. srchost->srchost_refcnt--;
  1731. dsthost->dsthost_refcnt--;
  1732. }
  1733. goto begin;
  1734. }
  1735. /* Last packet in queue may be marked, shouldn't be dropped */
  1736. if (!cobalt_should_drop(&flow->cvars, &b->cparams, now, skb,
  1737. (b->bulk_flow_count *
  1738. !!(q->rate_flags &
  1739. CAKE_FLAG_INGRESS))) ||
  1740. !flow->head)
  1741. break;
  1742. /* drop this packet, get another one */
  1743. if (q->rate_flags & CAKE_FLAG_INGRESS) {
  1744. len = cake_advance_shaper(q, b, skb,
  1745. now, true);
  1746. flow->deficit -= len;
  1747. b->tin_deficit -= len;
  1748. }
  1749. flow->dropped++;
  1750. b->tin_dropped++;
  1751. qdisc_tree_reduce_backlog(sch, 1, qdisc_pkt_len(skb));
  1752. qdisc_qstats_drop(sch);
  1753. kfree_skb(skb);
  1754. if (q->rate_flags & CAKE_FLAG_INGRESS)
  1755. goto retry;
  1756. }
  1757. b->tin_ecn_mark += !!flow->cvars.ecn_marked;
  1758. qdisc_bstats_update(sch, skb);
  1759. /* collect delay stats */
  1760. delay = ktime_to_ns(ktime_sub(now, cobalt_get_enqueue_time(skb)));
  1761. b->avge_delay = cake_ewma(b->avge_delay, delay, 8);
  1762. b->peak_delay = cake_ewma(b->peak_delay, delay,
  1763. delay > b->peak_delay ? 2 : 8);
  1764. b->base_delay = cake_ewma(b->base_delay, delay,
  1765. delay < b->base_delay ? 2 : 8);
  1766. len = cake_advance_shaper(q, b, skb, now, false);
  1767. flow->deficit -= len;
  1768. b->tin_deficit -= len;
  1769. if (ktime_after(q->time_next_packet, now) && sch->q.qlen) {
  1770. u64 next = min(ktime_to_ns(q->time_next_packet),
  1771. ktime_to_ns(q->failsafe_next_packet));
  1772. qdisc_watchdog_schedule_ns(&q->watchdog, next);
  1773. } else if (!sch->q.qlen) {
  1774. int i;
  1775. for (i = 0; i < q->tin_cnt; i++) {
  1776. if (q->tins[i].decaying_flow_count) {
  1777. ktime_t next = \
  1778. ktime_add_ns(now,
  1779. q->tins[i].cparams.target);
  1780. qdisc_watchdog_schedule_ns(&q->watchdog,
  1781. ktime_to_ns(next));
  1782. break;
  1783. }
  1784. }
  1785. }
  1786. if (q->overflow_timeout)
  1787. q->overflow_timeout--;
  1788. return skb;
  1789. }
  1790. static void cake_reset(struct Qdisc *sch)
  1791. {
  1792. u32 c;
  1793. for (c = 0; c < CAKE_MAX_TINS; c++)
  1794. cake_clear_tin(sch, c);
  1795. }
  1796. static const struct nla_policy cake_policy[TCA_CAKE_MAX + 1] = {
  1797. [TCA_CAKE_BASE_RATE64] = { .type = NLA_U64 },
  1798. [TCA_CAKE_DIFFSERV_MODE] = { .type = NLA_U32 },
  1799. [TCA_CAKE_ATM] = { .type = NLA_U32 },
  1800. [TCA_CAKE_FLOW_MODE] = { .type = NLA_U32 },
  1801. [TCA_CAKE_OVERHEAD] = { .type = NLA_S32 },
  1802. [TCA_CAKE_RTT] = { .type = NLA_U32 },
  1803. [TCA_CAKE_TARGET] = { .type = NLA_U32 },
  1804. [TCA_CAKE_AUTORATE] = { .type = NLA_U32 },
  1805. [TCA_CAKE_MEMORY] = { .type = NLA_U32 },
  1806. [TCA_CAKE_NAT] = { .type = NLA_U32 },
  1807. [TCA_CAKE_RAW] = { .type = NLA_U32 },
  1808. [TCA_CAKE_WASH] = { .type = NLA_U32 },
  1809. [TCA_CAKE_MPU] = { .type = NLA_U32 },
  1810. [TCA_CAKE_INGRESS] = { .type = NLA_U32 },
  1811. [TCA_CAKE_ACK_FILTER] = { .type = NLA_U32 },
  1812. };
  1813. static void cake_set_rate(struct cake_tin_data *b, u64 rate, u32 mtu,
  1814. u64 target_ns, u64 rtt_est_ns)
  1815. {
  1816. /* convert byte-rate into time-per-byte
  1817. * so it will always unwedge in reasonable time.
  1818. */
  1819. static const u64 MIN_RATE = 64;
  1820. u32 byte_target = mtu;
  1821. u64 byte_target_ns;
  1822. u8 rate_shft = 0;
  1823. u64 rate_ns = 0;
  1824. b->flow_quantum = 1514;
  1825. if (rate) {
  1826. b->flow_quantum = max(min(rate >> 12, 1514ULL), 300ULL);
  1827. rate_shft = 34;
  1828. rate_ns = ((u64)NSEC_PER_SEC) << rate_shft;
  1829. rate_ns = div64_u64(rate_ns, max(MIN_RATE, rate));
  1830. while (!!(rate_ns >> 34)) {
  1831. rate_ns >>= 1;
  1832. rate_shft--;
  1833. }
  1834. } /* else unlimited, ie. zero delay */
  1835. b->tin_rate_bps = rate;
  1836. b->tin_rate_ns = rate_ns;
  1837. b->tin_rate_shft = rate_shft;
  1838. byte_target_ns = (byte_target * rate_ns) >> rate_shft;
  1839. b->cparams.target = max((byte_target_ns * 3) / 2, target_ns);
  1840. b->cparams.interval = max(rtt_est_ns +
  1841. b->cparams.target - target_ns,
  1842. b->cparams.target * 2);
  1843. b->cparams.mtu_time = byte_target_ns;
  1844. b->cparams.p_inc = 1 << 24; /* 1/256 */
  1845. b->cparams.p_dec = 1 << 20; /* 1/4096 */
  1846. }
  1847. static int cake_config_besteffort(struct Qdisc *sch)
  1848. {
  1849. struct cake_sched_data *q = qdisc_priv(sch);
  1850. struct cake_tin_data *b = &q->tins[0];
  1851. u32 mtu = psched_mtu(qdisc_dev(sch));
  1852. u64 rate = q->rate_bps;
  1853. q->tin_cnt = 1;
  1854. q->tin_index = besteffort;
  1855. q->tin_order = normal_order;
  1856. cake_set_rate(b, rate, mtu,
  1857. us_to_ns(q->target), us_to_ns(q->interval));
  1858. b->tin_quantum_band = 65535;
  1859. b->tin_quantum_prio = 65535;
  1860. return 0;
  1861. }
  1862. static int cake_config_precedence(struct Qdisc *sch)
  1863. {
  1864. /* convert high-level (user visible) parameters into internal format */
  1865. struct cake_sched_data *q = qdisc_priv(sch);
  1866. u32 mtu = psched_mtu(qdisc_dev(sch));
  1867. u64 rate = q->rate_bps;
  1868. u32 quantum1 = 256;
  1869. u32 quantum2 = 256;
  1870. u32 i;
  1871. q->tin_cnt = 8;
  1872. q->tin_index = precedence;
  1873. q->tin_order = normal_order;
  1874. for (i = 0; i < q->tin_cnt; i++) {
  1875. struct cake_tin_data *b = &q->tins[i];
  1876. cake_set_rate(b, rate, mtu, us_to_ns(q->target),
  1877. us_to_ns(q->interval));
  1878. b->tin_quantum_prio = max_t(u16, 1U, quantum1);
  1879. b->tin_quantum_band = max_t(u16, 1U, quantum2);
  1880. /* calculate next class's parameters */
  1881. rate *= 7;
  1882. rate >>= 3;
  1883. quantum1 *= 3;
  1884. quantum1 >>= 1;
  1885. quantum2 *= 7;
  1886. quantum2 >>= 3;
  1887. }
  1888. return 0;
  1889. }
  1890. /* List of known Diffserv codepoints:
  1891. *
  1892. * Least Effort (CS1)
  1893. * Best Effort (CS0)
  1894. * Max Reliability & LLT "Lo" (TOS1)
  1895. * Max Throughput (TOS2)
  1896. * Min Delay (TOS4)
  1897. * LLT "La" (TOS5)
  1898. * Assured Forwarding 1 (AF1x) - x3
  1899. * Assured Forwarding 2 (AF2x) - x3
  1900. * Assured Forwarding 3 (AF3x) - x3
  1901. * Assured Forwarding 4 (AF4x) - x3
  1902. * Precedence Class 2 (CS2)
  1903. * Precedence Class 3 (CS3)
  1904. * Precedence Class 4 (CS4)
  1905. * Precedence Class 5 (CS5)
  1906. * Precedence Class 6 (CS6)
  1907. * Precedence Class 7 (CS7)
  1908. * Voice Admit (VA)
  1909. * Expedited Forwarding (EF)
  1910. * Total 25 codepoints.
  1911. */
  1912. /* List of traffic classes in RFC 4594:
  1913. * (roughly descending order of contended priority)
  1914. * (roughly ascending order of uncontended throughput)
  1915. *
  1916. * Network Control (CS6,CS7) - routing traffic
  1917. * Telephony (EF,VA) - aka. VoIP streams
  1918. * Signalling (CS5) - VoIP setup
  1919. * Multimedia Conferencing (AF4x) - aka. video calls
  1920. * Realtime Interactive (CS4) - eg. games
  1921. * Multimedia Streaming (AF3x) - eg. YouTube, NetFlix, Twitch
  1922. * Broadcast Video (CS3)
  1923. * Low Latency Data (AF2x,TOS4) - eg. database
  1924. * Ops, Admin, Management (CS2,TOS1) - eg. ssh
  1925. * Standard Service (CS0 & unrecognised codepoints)
  1926. * High Throughput Data (AF1x,TOS2) - eg. web traffic
  1927. * Low Priority Data (CS1) - eg. BitTorrent
  1928. * Total 12 traffic classes.
  1929. */
  1930. static int cake_config_diffserv8(struct Qdisc *sch)
  1931. {
  1932. /* Pruned list of traffic classes for typical applications:
  1933. *
  1934. * Network Control (CS6, CS7)
  1935. * Minimum Latency (EF, VA, CS5, CS4)
  1936. * Interactive Shell (CS2, TOS1)
  1937. * Low Latency Transactions (AF2x, TOS4)
  1938. * Video Streaming (AF4x, AF3x, CS3)
  1939. * Bog Standard (CS0 etc.)
  1940. * High Throughput (AF1x, TOS2)
  1941. * Background Traffic (CS1)
  1942. *
  1943. * Total 8 traffic classes.
  1944. */
  1945. struct cake_sched_data *q = qdisc_priv(sch);
  1946. u32 mtu = psched_mtu(qdisc_dev(sch));
  1947. u64 rate = q->rate_bps;
  1948. u32 quantum1 = 256;
  1949. u32 quantum2 = 256;
  1950. u32 i;
  1951. q->tin_cnt = 8;
  1952. /* codepoint to class mapping */
  1953. q->tin_index = diffserv8;
  1954. q->tin_order = normal_order;
  1955. /* class characteristics */
  1956. for (i = 0; i < q->tin_cnt; i++) {
  1957. struct cake_tin_data *b = &q->tins[i];
  1958. cake_set_rate(b, rate, mtu, us_to_ns(q->target),
  1959. us_to_ns(q->interval));
  1960. b->tin_quantum_prio = max_t(u16, 1U, quantum1);
  1961. b->tin_quantum_band = max_t(u16, 1U, quantum2);
  1962. /* calculate next class's parameters */
  1963. rate *= 7;
  1964. rate >>= 3;
  1965. quantum1 *= 3;
  1966. quantum1 >>= 1;
  1967. quantum2 *= 7;
  1968. quantum2 >>= 3;
  1969. }
  1970. return 0;
  1971. }
  1972. static int cake_config_diffserv4(struct Qdisc *sch)
  1973. {
  1974. /* Further pruned list of traffic classes for four-class system:
  1975. *
  1976. * Latency Sensitive (CS7, CS6, EF, VA, CS5, CS4)
  1977. * Streaming Media (AF4x, AF3x, CS3, AF2x, TOS4, CS2, TOS1)
  1978. * Best Effort (CS0, AF1x, TOS2, and those not specified)
  1979. * Background Traffic (CS1)
  1980. *
  1981. * Total 4 traffic classes.
  1982. */
  1983. struct cake_sched_data *q = qdisc_priv(sch);
  1984. u32 mtu = psched_mtu(qdisc_dev(sch));
  1985. u64 rate = q->rate_bps;
  1986. u32 quantum = 1024;
  1987. q->tin_cnt = 4;
  1988. /* codepoint to class mapping */
  1989. q->tin_index = diffserv4;
  1990. q->tin_order = bulk_order;
  1991. /* class characteristics */
  1992. cake_set_rate(&q->tins[0], rate, mtu,
  1993. us_to_ns(q->target), us_to_ns(q->interval));
  1994. cake_set_rate(&q->tins[1], rate >> 4, mtu,
  1995. us_to_ns(q->target), us_to_ns(q->interval));
  1996. cake_set_rate(&q->tins[2], rate >> 1, mtu,
  1997. us_to_ns(q->target), us_to_ns(q->interval));
  1998. cake_set_rate(&q->tins[3], rate >> 2, mtu,
  1999. us_to_ns(q->target), us_to_ns(q->interval));
  2000. /* priority weights */
  2001. q->tins[0].tin_quantum_prio = quantum;
  2002. q->tins[1].tin_quantum_prio = quantum >> 4;
  2003. q->tins[2].tin_quantum_prio = quantum << 2;
  2004. q->tins[3].tin_quantum_prio = quantum << 4;
  2005. /* bandwidth-sharing weights */
  2006. q->tins[0].tin_quantum_band = quantum;
  2007. q->tins[1].tin_quantum_band = quantum >> 4;
  2008. q->tins[2].tin_quantum_band = quantum >> 1;
  2009. q->tins[3].tin_quantum_band = quantum >> 2;
  2010. return 0;
  2011. }
  2012. static int cake_config_diffserv3(struct Qdisc *sch)
  2013. {
  2014. /* Simplified Diffserv structure with 3 tins.
  2015. * Low Priority (CS1)
  2016. * Best Effort
  2017. * Latency Sensitive (TOS4, VA, EF, CS6, CS7)
  2018. */
  2019. struct cake_sched_data *q = qdisc_priv(sch);
  2020. u32 mtu = psched_mtu(qdisc_dev(sch));
  2021. u64 rate = q->rate_bps;
  2022. u32 quantum = 1024;
  2023. q->tin_cnt = 3;
  2024. /* codepoint to class mapping */
  2025. q->tin_index = diffserv3;
  2026. q->tin_order = bulk_order;
  2027. /* class characteristics */
  2028. cake_set_rate(&q->tins[0], rate, mtu,
  2029. us_to_ns(q->target), us_to_ns(q->interval));
  2030. cake_set_rate(&q->tins[1], rate >> 4, mtu,
  2031. us_to_ns(q->target), us_to_ns(q->interval));
  2032. cake_set_rate(&q->tins[2], rate >> 2, mtu,
  2033. us_to_ns(q->target), us_to_ns(q->interval));
  2034. /* priority weights */
  2035. q->tins[0].tin_quantum_prio = quantum;
  2036. q->tins[1].tin_quantum_prio = quantum >> 4;
  2037. q->tins[2].tin_quantum_prio = quantum << 4;
  2038. /* bandwidth-sharing weights */
  2039. q->tins[0].tin_quantum_band = quantum;
  2040. q->tins[1].tin_quantum_band = quantum >> 4;
  2041. q->tins[2].tin_quantum_band = quantum >> 2;
  2042. return 0;
  2043. }
  2044. static void cake_reconfigure(struct Qdisc *sch)
  2045. {
  2046. struct cake_sched_data *q = qdisc_priv(sch);
  2047. int c, ft;
  2048. switch (q->tin_mode) {
  2049. case CAKE_DIFFSERV_BESTEFFORT:
  2050. ft = cake_config_besteffort(sch);
  2051. break;
  2052. case CAKE_DIFFSERV_PRECEDENCE:
  2053. ft = cake_config_precedence(sch);
  2054. break;
  2055. case CAKE_DIFFSERV_DIFFSERV8:
  2056. ft = cake_config_diffserv8(sch);
  2057. break;
  2058. case CAKE_DIFFSERV_DIFFSERV4:
  2059. ft = cake_config_diffserv4(sch);
  2060. break;
  2061. case CAKE_DIFFSERV_DIFFSERV3:
  2062. default:
  2063. ft = cake_config_diffserv3(sch);
  2064. break;
  2065. }
  2066. for (c = q->tin_cnt; c < CAKE_MAX_TINS; c++) {
  2067. cake_clear_tin(sch, c);
  2068. q->tins[c].cparams.mtu_time = q->tins[ft].cparams.mtu_time;
  2069. }
  2070. q->rate_ns = q->tins[ft].tin_rate_ns;
  2071. q->rate_shft = q->tins[ft].tin_rate_shft;
  2072. if (q->buffer_config_limit) {
  2073. q->buffer_limit = q->buffer_config_limit;
  2074. } else if (q->rate_bps) {
  2075. u64 t = q->rate_bps * q->interval;
  2076. do_div(t, USEC_PER_SEC / 4);
  2077. q->buffer_limit = max_t(u32, t, 4U << 20);
  2078. } else {
  2079. q->buffer_limit = ~0;
  2080. }
  2081. sch->flags &= ~TCQ_F_CAN_BYPASS;
  2082. q->buffer_limit = min(q->buffer_limit,
  2083. max(sch->limit * psched_mtu(qdisc_dev(sch)),
  2084. q->buffer_config_limit));
  2085. }
  2086. static int cake_change(struct Qdisc *sch, struct nlattr *opt,
  2087. struct netlink_ext_ack *extack)
  2088. {
  2089. struct cake_sched_data *q = qdisc_priv(sch);
  2090. struct nlattr *tb[TCA_CAKE_MAX + 1];
  2091. int err;
  2092. if (!opt)
  2093. return -EINVAL;
  2094. err = nla_parse_nested(tb, TCA_CAKE_MAX, opt, cake_policy, extack);
  2095. if (err < 0)
  2096. return err;
  2097. if (tb[TCA_CAKE_NAT]) {
  2098. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  2099. q->flow_mode &= ~CAKE_FLOW_NAT_FLAG;
  2100. q->flow_mode |= CAKE_FLOW_NAT_FLAG *
  2101. !!nla_get_u32(tb[TCA_CAKE_NAT]);
  2102. #else
  2103. NL_SET_ERR_MSG_ATTR(extack, tb[TCA_CAKE_NAT],
  2104. "No conntrack support in kernel");
  2105. return -EOPNOTSUPP;
  2106. #endif
  2107. }
  2108. if (tb[TCA_CAKE_BASE_RATE64])
  2109. q->rate_bps = nla_get_u64(tb[TCA_CAKE_BASE_RATE64]);
  2110. if (tb[TCA_CAKE_DIFFSERV_MODE])
  2111. q->tin_mode = nla_get_u32(tb[TCA_CAKE_DIFFSERV_MODE]);
  2112. if (tb[TCA_CAKE_WASH]) {
  2113. if (!!nla_get_u32(tb[TCA_CAKE_WASH]))
  2114. q->rate_flags |= CAKE_FLAG_WASH;
  2115. else
  2116. q->rate_flags &= ~CAKE_FLAG_WASH;
  2117. }
  2118. if (tb[TCA_CAKE_FLOW_MODE])
  2119. q->flow_mode = ((q->flow_mode & CAKE_FLOW_NAT_FLAG) |
  2120. (nla_get_u32(tb[TCA_CAKE_FLOW_MODE]) &
  2121. CAKE_FLOW_MASK));
  2122. if (tb[TCA_CAKE_ATM])
  2123. q->atm_mode = nla_get_u32(tb[TCA_CAKE_ATM]);
  2124. if (tb[TCA_CAKE_OVERHEAD]) {
  2125. q->rate_overhead = nla_get_s32(tb[TCA_CAKE_OVERHEAD]);
  2126. q->rate_flags |= CAKE_FLAG_OVERHEAD;
  2127. q->max_netlen = 0;
  2128. q->max_adjlen = 0;
  2129. q->min_netlen = ~0;
  2130. q->min_adjlen = ~0;
  2131. }
  2132. if (tb[TCA_CAKE_RAW]) {
  2133. q->rate_flags &= ~CAKE_FLAG_OVERHEAD;
  2134. q->max_netlen = 0;
  2135. q->max_adjlen = 0;
  2136. q->min_netlen = ~0;
  2137. q->min_adjlen = ~0;
  2138. }
  2139. if (tb[TCA_CAKE_MPU])
  2140. q->rate_mpu = nla_get_u32(tb[TCA_CAKE_MPU]);
  2141. if (tb[TCA_CAKE_RTT]) {
  2142. q->interval = nla_get_u32(tb[TCA_CAKE_RTT]);
  2143. if (!q->interval)
  2144. q->interval = 1;
  2145. }
  2146. if (tb[TCA_CAKE_TARGET]) {
  2147. q->target = nla_get_u32(tb[TCA_CAKE_TARGET]);
  2148. if (!q->target)
  2149. q->target = 1;
  2150. }
  2151. if (tb[TCA_CAKE_AUTORATE]) {
  2152. if (!!nla_get_u32(tb[TCA_CAKE_AUTORATE]))
  2153. q->rate_flags |= CAKE_FLAG_AUTORATE_INGRESS;
  2154. else
  2155. q->rate_flags &= ~CAKE_FLAG_AUTORATE_INGRESS;
  2156. }
  2157. if (tb[TCA_CAKE_INGRESS]) {
  2158. if (!!nla_get_u32(tb[TCA_CAKE_INGRESS]))
  2159. q->rate_flags |= CAKE_FLAG_INGRESS;
  2160. else
  2161. q->rate_flags &= ~CAKE_FLAG_INGRESS;
  2162. }
  2163. if (tb[TCA_CAKE_ACK_FILTER])
  2164. q->ack_filter = nla_get_u32(tb[TCA_CAKE_ACK_FILTER]);
  2165. if (tb[TCA_CAKE_MEMORY])
  2166. q->buffer_config_limit = nla_get_u32(tb[TCA_CAKE_MEMORY]);
  2167. if (tb[TCA_CAKE_SPLIT_GSO]) {
  2168. if (!!nla_get_u32(tb[TCA_CAKE_SPLIT_GSO]))
  2169. q->rate_flags |= CAKE_FLAG_SPLIT_GSO;
  2170. else
  2171. q->rate_flags &= ~CAKE_FLAG_SPLIT_GSO;
  2172. }
  2173. if (q->tins) {
  2174. sch_tree_lock(sch);
  2175. cake_reconfigure(sch);
  2176. sch_tree_unlock(sch);
  2177. }
  2178. return 0;
  2179. }
  2180. static void cake_destroy(struct Qdisc *sch)
  2181. {
  2182. struct cake_sched_data *q = qdisc_priv(sch);
  2183. qdisc_watchdog_cancel(&q->watchdog);
  2184. tcf_block_put(q->block);
  2185. kvfree(q->tins);
  2186. }
  2187. static int cake_init(struct Qdisc *sch, struct nlattr *opt,
  2188. struct netlink_ext_ack *extack)
  2189. {
  2190. struct cake_sched_data *q = qdisc_priv(sch);
  2191. int i, j, err;
  2192. sch->limit = 10240;
  2193. q->tin_mode = CAKE_DIFFSERV_DIFFSERV3;
  2194. q->flow_mode = CAKE_FLOW_TRIPLE;
  2195. q->rate_bps = 0; /* unlimited by default */
  2196. q->interval = 100000; /* 100ms default */
  2197. q->target = 5000; /* 5ms: codel RFC argues
  2198. * for 5 to 10% of interval
  2199. */
  2200. q->rate_flags |= CAKE_FLAG_SPLIT_GSO;
  2201. q->cur_tin = 0;
  2202. q->cur_flow = 0;
  2203. qdisc_watchdog_init(&q->watchdog, sch);
  2204. if (opt) {
  2205. int err = cake_change(sch, opt, extack);
  2206. if (err)
  2207. return err;
  2208. }
  2209. err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
  2210. if (err)
  2211. return err;
  2212. quantum_div[0] = ~0;
  2213. for (i = 1; i <= CAKE_QUEUES; i++)
  2214. quantum_div[i] = 65535 / i;
  2215. q->tins = kvzalloc(CAKE_MAX_TINS * sizeof(struct cake_tin_data),
  2216. GFP_KERNEL);
  2217. if (!q->tins)
  2218. goto nomem;
  2219. for (i = 0; i < CAKE_MAX_TINS; i++) {
  2220. struct cake_tin_data *b = q->tins + i;
  2221. INIT_LIST_HEAD(&b->new_flows);
  2222. INIT_LIST_HEAD(&b->old_flows);
  2223. INIT_LIST_HEAD(&b->decaying_flows);
  2224. b->sparse_flow_count = 0;
  2225. b->bulk_flow_count = 0;
  2226. b->decaying_flow_count = 0;
  2227. for (j = 0; j < CAKE_QUEUES; j++) {
  2228. struct cake_flow *flow = b->flows + j;
  2229. u32 k = j * CAKE_MAX_TINS + i;
  2230. INIT_LIST_HEAD(&flow->flowchain);
  2231. cobalt_vars_init(&flow->cvars);
  2232. q->overflow_heap[k].t = i;
  2233. q->overflow_heap[k].b = j;
  2234. b->overflow_idx[j] = k;
  2235. }
  2236. }
  2237. cake_reconfigure(sch);
  2238. q->avg_peak_bandwidth = q->rate_bps;
  2239. q->min_netlen = ~0;
  2240. q->min_adjlen = ~0;
  2241. return 0;
  2242. nomem:
  2243. cake_destroy(sch);
  2244. return -ENOMEM;
  2245. }
  2246. static int cake_dump(struct Qdisc *sch, struct sk_buff *skb)
  2247. {
  2248. struct cake_sched_data *q = qdisc_priv(sch);
  2249. struct nlattr *opts;
  2250. opts = nla_nest_start(skb, TCA_OPTIONS);
  2251. if (!opts)
  2252. goto nla_put_failure;
  2253. if (nla_put_u64_64bit(skb, TCA_CAKE_BASE_RATE64, q->rate_bps,
  2254. TCA_CAKE_PAD))
  2255. goto nla_put_failure;
  2256. if (nla_put_u32(skb, TCA_CAKE_FLOW_MODE,
  2257. q->flow_mode & CAKE_FLOW_MASK))
  2258. goto nla_put_failure;
  2259. if (nla_put_u32(skb, TCA_CAKE_RTT, q->interval))
  2260. goto nla_put_failure;
  2261. if (nla_put_u32(skb, TCA_CAKE_TARGET, q->target))
  2262. goto nla_put_failure;
  2263. if (nla_put_u32(skb, TCA_CAKE_MEMORY, q->buffer_config_limit))
  2264. goto nla_put_failure;
  2265. if (nla_put_u32(skb, TCA_CAKE_AUTORATE,
  2266. !!(q->rate_flags & CAKE_FLAG_AUTORATE_INGRESS)))
  2267. goto nla_put_failure;
  2268. if (nla_put_u32(skb, TCA_CAKE_INGRESS,
  2269. !!(q->rate_flags & CAKE_FLAG_INGRESS)))
  2270. goto nla_put_failure;
  2271. if (nla_put_u32(skb, TCA_CAKE_ACK_FILTER, q->ack_filter))
  2272. goto nla_put_failure;
  2273. if (nla_put_u32(skb, TCA_CAKE_NAT,
  2274. !!(q->flow_mode & CAKE_FLOW_NAT_FLAG)))
  2275. goto nla_put_failure;
  2276. if (nla_put_u32(skb, TCA_CAKE_DIFFSERV_MODE, q->tin_mode))
  2277. goto nla_put_failure;
  2278. if (nla_put_u32(skb, TCA_CAKE_WASH,
  2279. !!(q->rate_flags & CAKE_FLAG_WASH)))
  2280. goto nla_put_failure;
  2281. if (nla_put_u32(skb, TCA_CAKE_OVERHEAD, q->rate_overhead))
  2282. goto nla_put_failure;
  2283. if (!(q->rate_flags & CAKE_FLAG_OVERHEAD))
  2284. if (nla_put_u32(skb, TCA_CAKE_RAW, 0))
  2285. goto nla_put_failure;
  2286. if (nla_put_u32(skb, TCA_CAKE_ATM, q->atm_mode))
  2287. goto nla_put_failure;
  2288. if (nla_put_u32(skb, TCA_CAKE_MPU, q->rate_mpu))
  2289. goto nla_put_failure;
  2290. if (nla_put_u32(skb, TCA_CAKE_SPLIT_GSO,
  2291. !!(q->rate_flags & CAKE_FLAG_SPLIT_GSO)))
  2292. goto nla_put_failure;
  2293. return nla_nest_end(skb, opts);
  2294. nla_put_failure:
  2295. return -1;
  2296. }
  2297. static int cake_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  2298. {
  2299. struct nlattr *stats = nla_nest_start(d->skb, TCA_STATS_APP);
  2300. struct cake_sched_data *q = qdisc_priv(sch);
  2301. struct nlattr *tstats, *ts;
  2302. int i;
  2303. if (!stats)
  2304. return -1;
  2305. #define PUT_STAT_U32(attr, data) do { \
  2306. if (nla_put_u32(d->skb, TCA_CAKE_STATS_ ## attr, data)) \
  2307. goto nla_put_failure; \
  2308. } while (0)
  2309. #define PUT_STAT_U64(attr, data) do { \
  2310. if (nla_put_u64_64bit(d->skb, TCA_CAKE_STATS_ ## attr, \
  2311. data, TCA_CAKE_STATS_PAD)) \
  2312. goto nla_put_failure; \
  2313. } while (0)
  2314. PUT_STAT_U64(CAPACITY_ESTIMATE64, q->avg_peak_bandwidth);
  2315. PUT_STAT_U32(MEMORY_LIMIT, q->buffer_limit);
  2316. PUT_STAT_U32(MEMORY_USED, q->buffer_max_used);
  2317. PUT_STAT_U32(AVG_NETOFF, ((q->avg_netoff + 0x8000) >> 16));
  2318. PUT_STAT_U32(MAX_NETLEN, q->max_netlen);
  2319. PUT_STAT_U32(MAX_ADJLEN, q->max_adjlen);
  2320. PUT_STAT_U32(MIN_NETLEN, q->min_netlen);
  2321. PUT_STAT_U32(MIN_ADJLEN, q->min_adjlen);
  2322. #undef PUT_STAT_U32
  2323. #undef PUT_STAT_U64
  2324. tstats = nla_nest_start(d->skb, TCA_CAKE_STATS_TIN_STATS);
  2325. if (!tstats)
  2326. goto nla_put_failure;
  2327. #define PUT_TSTAT_U32(attr, data) do { \
  2328. if (nla_put_u32(d->skb, TCA_CAKE_TIN_STATS_ ## attr, data)) \
  2329. goto nla_put_failure; \
  2330. } while (0)
  2331. #define PUT_TSTAT_U64(attr, data) do { \
  2332. if (nla_put_u64_64bit(d->skb, TCA_CAKE_TIN_STATS_ ## attr, \
  2333. data, TCA_CAKE_TIN_STATS_PAD)) \
  2334. goto nla_put_failure; \
  2335. } while (0)
  2336. for (i = 0; i < q->tin_cnt; i++) {
  2337. struct cake_tin_data *b = &q->tins[q->tin_order[i]];
  2338. ts = nla_nest_start(d->skb, i + 1);
  2339. if (!ts)
  2340. goto nla_put_failure;
  2341. PUT_TSTAT_U64(THRESHOLD_RATE64, b->tin_rate_bps);
  2342. PUT_TSTAT_U64(SENT_BYTES64, b->bytes);
  2343. PUT_TSTAT_U32(BACKLOG_BYTES, b->tin_backlog);
  2344. PUT_TSTAT_U32(TARGET_US,
  2345. ktime_to_us(ns_to_ktime(b->cparams.target)));
  2346. PUT_TSTAT_U32(INTERVAL_US,
  2347. ktime_to_us(ns_to_ktime(b->cparams.interval)));
  2348. PUT_TSTAT_U32(SENT_PACKETS, b->packets);
  2349. PUT_TSTAT_U32(DROPPED_PACKETS, b->tin_dropped);
  2350. PUT_TSTAT_U32(ECN_MARKED_PACKETS, b->tin_ecn_mark);
  2351. PUT_TSTAT_U32(ACKS_DROPPED_PACKETS, b->ack_drops);
  2352. PUT_TSTAT_U32(PEAK_DELAY_US,
  2353. ktime_to_us(ns_to_ktime(b->peak_delay)));
  2354. PUT_TSTAT_U32(AVG_DELAY_US,
  2355. ktime_to_us(ns_to_ktime(b->avge_delay)));
  2356. PUT_TSTAT_U32(BASE_DELAY_US,
  2357. ktime_to_us(ns_to_ktime(b->base_delay)));
  2358. PUT_TSTAT_U32(WAY_INDIRECT_HITS, b->way_hits);
  2359. PUT_TSTAT_U32(WAY_MISSES, b->way_misses);
  2360. PUT_TSTAT_U32(WAY_COLLISIONS, b->way_collisions);
  2361. PUT_TSTAT_U32(SPARSE_FLOWS, b->sparse_flow_count +
  2362. b->decaying_flow_count);
  2363. PUT_TSTAT_U32(BULK_FLOWS, b->bulk_flow_count);
  2364. PUT_TSTAT_U32(UNRESPONSIVE_FLOWS, b->unresponsive_flow_count);
  2365. PUT_TSTAT_U32(MAX_SKBLEN, b->max_skblen);
  2366. PUT_TSTAT_U32(FLOW_QUANTUM, b->flow_quantum);
  2367. nla_nest_end(d->skb, ts);
  2368. }
  2369. #undef PUT_TSTAT_U32
  2370. #undef PUT_TSTAT_U64
  2371. nla_nest_end(d->skb, tstats);
  2372. return nla_nest_end(d->skb, stats);
  2373. nla_put_failure:
  2374. nla_nest_cancel(d->skb, stats);
  2375. return -1;
  2376. }
  2377. static struct Qdisc *cake_leaf(struct Qdisc *sch, unsigned long arg)
  2378. {
  2379. return NULL;
  2380. }
  2381. static unsigned long cake_find(struct Qdisc *sch, u32 classid)
  2382. {
  2383. return 0;
  2384. }
  2385. static unsigned long cake_bind(struct Qdisc *sch, unsigned long parent,
  2386. u32 classid)
  2387. {
  2388. return 0;
  2389. }
  2390. static void cake_unbind(struct Qdisc *q, unsigned long cl)
  2391. {
  2392. }
  2393. static struct tcf_block *cake_tcf_block(struct Qdisc *sch, unsigned long cl,
  2394. struct netlink_ext_ack *extack)
  2395. {
  2396. struct cake_sched_data *q = qdisc_priv(sch);
  2397. if (cl)
  2398. return NULL;
  2399. return q->block;
  2400. }
  2401. static int cake_dump_class(struct Qdisc *sch, unsigned long cl,
  2402. struct sk_buff *skb, struct tcmsg *tcm)
  2403. {
  2404. tcm->tcm_handle |= TC_H_MIN(cl);
  2405. return 0;
  2406. }
  2407. static int cake_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  2408. struct gnet_dump *d)
  2409. {
  2410. struct cake_sched_data *q = qdisc_priv(sch);
  2411. const struct cake_flow *flow = NULL;
  2412. struct gnet_stats_queue qs = { 0 };
  2413. struct nlattr *stats;
  2414. u32 idx = cl - 1;
  2415. if (idx < CAKE_QUEUES * q->tin_cnt) {
  2416. const struct cake_tin_data *b = \
  2417. &q->tins[q->tin_order[idx / CAKE_QUEUES]];
  2418. const struct sk_buff *skb;
  2419. flow = &b->flows[idx % CAKE_QUEUES];
  2420. if (flow->head) {
  2421. sch_tree_lock(sch);
  2422. skb = flow->head;
  2423. while (skb) {
  2424. qs.qlen++;
  2425. skb = skb->next;
  2426. }
  2427. sch_tree_unlock(sch);
  2428. }
  2429. qs.backlog = b->backlogs[idx % CAKE_QUEUES];
  2430. qs.drops = flow->dropped;
  2431. }
  2432. if (gnet_stats_copy_queue(d, NULL, &qs, qs.qlen) < 0)
  2433. return -1;
  2434. if (flow) {
  2435. ktime_t now = ktime_get();
  2436. stats = nla_nest_start(d->skb, TCA_STATS_APP);
  2437. if (!stats)
  2438. return -1;
  2439. #define PUT_STAT_U32(attr, data) do { \
  2440. if (nla_put_u32(d->skb, TCA_CAKE_STATS_ ## attr, data)) \
  2441. goto nla_put_failure; \
  2442. } while (0)
  2443. #define PUT_STAT_S32(attr, data) do { \
  2444. if (nla_put_s32(d->skb, TCA_CAKE_STATS_ ## attr, data)) \
  2445. goto nla_put_failure; \
  2446. } while (0)
  2447. PUT_STAT_S32(DEFICIT, flow->deficit);
  2448. PUT_STAT_U32(DROPPING, flow->cvars.dropping);
  2449. PUT_STAT_U32(COBALT_COUNT, flow->cvars.count);
  2450. PUT_STAT_U32(P_DROP, flow->cvars.p_drop);
  2451. if (flow->cvars.p_drop) {
  2452. PUT_STAT_S32(BLUE_TIMER_US,
  2453. ktime_to_us(
  2454. ktime_sub(now,
  2455. flow->cvars.blue_timer)));
  2456. }
  2457. if (flow->cvars.dropping) {
  2458. PUT_STAT_S32(DROP_NEXT_US,
  2459. ktime_to_us(
  2460. ktime_sub(now,
  2461. flow->cvars.drop_next)));
  2462. }
  2463. if (nla_nest_end(d->skb, stats) < 0)
  2464. return -1;
  2465. }
  2466. return 0;
  2467. nla_put_failure:
  2468. nla_nest_cancel(d->skb, stats);
  2469. return -1;
  2470. }
  2471. static void cake_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  2472. {
  2473. struct cake_sched_data *q = qdisc_priv(sch);
  2474. unsigned int i, j;
  2475. if (arg->stop)
  2476. return;
  2477. for (i = 0; i < q->tin_cnt; i++) {
  2478. struct cake_tin_data *b = &q->tins[q->tin_order[i]];
  2479. for (j = 0; j < CAKE_QUEUES; j++) {
  2480. if (list_empty(&b->flows[j].flowchain) ||
  2481. arg->count < arg->skip) {
  2482. arg->count++;
  2483. continue;
  2484. }
  2485. if (arg->fn(sch, i * CAKE_QUEUES + j + 1, arg) < 0) {
  2486. arg->stop = 1;
  2487. break;
  2488. }
  2489. arg->count++;
  2490. }
  2491. }
  2492. }
  2493. static const struct Qdisc_class_ops cake_class_ops = {
  2494. .leaf = cake_leaf,
  2495. .find = cake_find,
  2496. .tcf_block = cake_tcf_block,
  2497. .bind_tcf = cake_bind,
  2498. .unbind_tcf = cake_unbind,
  2499. .dump = cake_dump_class,
  2500. .dump_stats = cake_dump_class_stats,
  2501. .walk = cake_walk,
  2502. };
  2503. static struct Qdisc_ops cake_qdisc_ops __read_mostly = {
  2504. .cl_ops = &cake_class_ops,
  2505. .id = "cake",
  2506. .priv_size = sizeof(struct cake_sched_data),
  2507. .enqueue = cake_enqueue,
  2508. .dequeue = cake_dequeue,
  2509. .peek = qdisc_peek_dequeued,
  2510. .init = cake_init,
  2511. .reset = cake_reset,
  2512. .destroy = cake_destroy,
  2513. .change = cake_change,
  2514. .dump = cake_dump,
  2515. .dump_stats = cake_dump_stats,
  2516. .owner = THIS_MODULE,
  2517. };
  2518. static int __init cake_module_init(void)
  2519. {
  2520. return register_qdisc(&cake_qdisc_ops);
  2521. }
  2522. static void __exit cake_module_exit(void)
  2523. {
  2524. unregister_qdisc(&cake_qdisc_ops);
  2525. }
  2526. module_init(cake_module_init)
  2527. module_exit(cake_module_exit)
  2528. MODULE_AUTHOR("Jonathan Morton");
  2529. MODULE_LICENSE("Dual BSD/GPL");
  2530. MODULE_DESCRIPTION("The CAKE shaper.");