sch_cake.c 77 KB

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