sch_cake.c 77 KB

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