sch_cake.c 80 KB

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