sch_cake.c 87 KB

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