vcap_api.c 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Microchip VCAP API
  3. *
  4. * Copyright (c) 2022 Microchip Technology Inc. and its subsidiaries.
  5. */
  6. #include <linux/types.h>
  7. #include "vcap_api_private.h"
  8. static int keyfield_size_table[] = {
  9. [VCAP_FIELD_BIT] = sizeof(struct vcap_u1_key),
  10. [VCAP_FIELD_U32] = sizeof(struct vcap_u32_key),
  11. [VCAP_FIELD_U48] = sizeof(struct vcap_u48_key),
  12. [VCAP_FIELD_U56] = sizeof(struct vcap_u56_key),
  13. [VCAP_FIELD_U64] = sizeof(struct vcap_u64_key),
  14. [VCAP_FIELD_U72] = sizeof(struct vcap_u72_key),
  15. [VCAP_FIELD_U112] = sizeof(struct vcap_u112_key),
  16. [VCAP_FIELD_U128] = sizeof(struct vcap_u128_key),
  17. };
  18. static int actionfield_size_table[] = {
  19. [VCAP_FIELD_BIT] = sizeof(struct vcap_u1_action),
  20. [VCAP_FIELD_U32] = sizeof(struct vcap_u32_action),
  21. [VCAP_FIELD_U48] = sizeof(struct vcap_u48_action),
  22. [VCAP_FIELD_U56] = sizeof(struct vcap_u56_action),
  23. [VCAP_FIELD_U64] = sizeof(struct vcap_u64_action),
  24. [VCAP_FIELD_U72] = sizeof(struct vcap_u72_action),
  25. [VCAP_FIELD_U112] = sizeof(struct vcap_u112_action),
  26. [VCAP_FIELD_U128] = sizeof(struct vcap_u128_action),
  27. };
  28. /* Moving a rule in the VCAP address space */
  29. struct vcap_rule_move {
  30. int addr; /* address to move */
  31. int offset; /* change in address */
  32. int count; /* blocksize of addresses to move */
  33. };
  34. /* Stores the filter cookie and chain id that enabled the port */
  35. struct vcap_enabled_port {
  36. struct list_head list; /* for insertion in enabled ports list */
  37. struct net_device *ndev; /* the enabled port */
  38. unsigned long cookie; /* filter that enabled the port */
  39. int src_cid; /* source chain id */
  40. int dst_cid; /* destination chain id */
  41. };
  42. void vcap_iter_set(struct vcap_stream_iter *itr, int sw_width,
  43. const struct vcap_typegroup *tg, u32 offset)
  44. {
  45. memset(itr, 0, sizeof(*itr));
  46. itr->offset = offset;
  47. itr->sw_width = sw_width;
  48. itr->regs_per_sw = DIV_ROUND_UP(sw_width, 32);
  49. itr->tg = tg;
  50. }
  51. static void vcap_iter_skip_tg(struct vcap_stream_iter *itr)
  52. {
  53. /* Compensate the field offset for preceding typegroups.
  54. * A typegroup table ends with an all-zero terminator.
  55. */
  56. while (itr->tg->width && itr->offset >= itr->tg->offset) {
  57. itr->offset += itr->tg->width;
  58. itr->tg++; /* next typegroup */
  59. }
  60. }
  61. void vcap_iter_update(struct vcap_stream_iter *itr)
  62. {
  63. int sw_idx, sw_bitpos;
  64. /* Calculate the subword index and bitposition for current bit */
  65. sw_idx = itr->offset / itr->sw_width;
  66. sw_bitpos = itr->offset % itr->sw_width;
  67. /* Calculate the register index and bitposition for current bit */
  68. itr->reg_idx = (sw_idx * itr->regs_per_sw) + (sw_bitpos / 32);
  69. itr->reg_bitpos = sw_bitpos % 32;
  70. }
  71. void vcap_iter_init(struct vcap_stream_iter *itr, int sw_width,
  72. const struct vcap_typegroup *tg, u32 offset)
  73. {
  74. vcap_iter_set(itr, sw_width, tg, offset);
  75. vcap_iter_skip_tg(itr);
  76. vcap_iter_update(itr);
  77. }
  78. void vcap_iter_next(struct vcap_stream_iter *itr)
  79. {
  80. itr->offset++;
  81. vcap_iter_skip_tg(itr);
  82. vcap_iter_update(itr);
  83. }
  84. static void vcap_set_bit(u32 *stream, struct vcap_stream_iter *itr, bool value)
  85. {
  86. u32 mask = BIT(itr->reg_bitpos);
  87. u32 *p = &stream[itr->reg_idx];
  88. if (value)
  89. *p |= mask;
  90. else
  91. *p &= ~mask;
  92. }
  93. static void vcap_encode_bit(u32 *stream, struct vcap_stream_iter *itr, bool val)
  94. {
  95. /* When intersected by a type group field, stream the type group bits
  96. * before continuing with the value bit
  97. */
  98. while (itr->tg->width &&
  99. itr->offset >= itr->tg->offset &&
  100. itr->offset < itr->tg->offset + itr->tg->width) {
  101. int tg_bitpos = itr->tg->offset - itr->offset;
  102. vcap_set_bit(stream, itr, (itr->tg->value >> tg_bitpos) & 0x1);
  103. itr->offset++;
  104. vcap_iter_update(itr);
  105. }
  106. vcap_set_bit(stream, itr, val);
  107. }
  108. static void vcap_encode_field(u32 *stream, struct vcap_stream_iter *itr,
  109. int width, const u8 *value)
  110. {
  111. int idx;
  112. /* Loop over the field value bits and add the value bits one by one to
  113. * the output stream.
  114. */
  115. for (idx = 0; idx < width; idx++) {
  116. u8 bidx = idx & GENMASK(2, 0);
  117. /* Encode one field value bit */
  118. vcap_encode_bit(stream, itr, (value[idx / 8] >> bidx) & 0x1);
  119. vcap_iter_next(itr);
  120. }
  121. }
  122. static void vcap_encode_typegroups(u32 *stream, int sw_width,
  123. const struct vcap_typegroup *tg,
  124. bool mask)
  125. {
  126. struct vcap_stream_iter iter;
  127. int idx;
  128. /* Mask bits must be set to zeros (inverted later when writing to the
  129. * mask cache register), so that the mask typegroup bits consist of
  130. * match-1 or match-0, or both
  131. */
  132. vcap_iter_set(&iter, sw_width, tg, 0);
  133. while (iter.tg->width) {
  134. /* Set position to current typegroup bit */
  135. iter.offset = iter.tg->offset;
  136. vcap_iter_update(&iter);
  137. for (idx = 0; idx < iter.tg->width; idx++) {
  138. /* Iterate over current typegroup bits. Mask typegroup
  139. * bits are always set
  140. */
  141. if (mask)
  142. vcap_set_bit(stream, &iter, 0x1);
  143. else
  144. vcap_set_bit(stream, &iter,
  145. (iter.tg->value >> idx) & 0x1);
  146. iter.offset++;
  147. vcap_iter_update(&iter);
  148. }
  149. iter.tg++; /* next typegroup */
  150. }
  151. }
  152. static bool vcap_bitarray_zero(int width, u8 *value)
  153. {
  154. int bytes = DIV_ROUND_UP(width, BITS_PER_BYTE);
  155. u8 total = 0, bmask = 0xff;
  156. int rwidth = width;
  157. int idx;
  158. for (idx = 0; idx < bytes; ++idx, rwidth -= BITS_PER_BYTE) {
  159. if (rwidth && rwidth < BITS_PER_BYTE)
  160. bmask = (1 << rwidth) - 1;
  161. total += value[idx] & bmask;
  162. }
  163. return total == 0;
  164. }
  165. static bool vcap_get_bit(u32 *stream, struct vcap_stream_iter *itr)
  166. {
  167. u32 mask = BIT(itr->reg_bitpos);
  168. u32 *p = &stream[itr->reg_idx];
  169. return !!(*p & mask);
  170. }
  171. static void vcap_decode_field(u32 *stream, struct vcap_stream_iter *itr,
  172. int width, u8 *value)
  173. {
  174. int idx;
  175. /* Loop over the field value bits and get the field bits and
  176. * set them in the output value byte array
  177. */
  178. for (idx = 0; idx < width; idx++) {
  179. u8 bidx = idx & 0x7;
  180. /* Decode one field value bit */
  181. if (vcap_get_bit(stream, itr))
  182. *value |= 1 << bidx;
  183. vcap_iter_next(itr);
  184. if (bidx == 7)
  185. value++;
  186. }
  187. }
  188. /* Verify that the type id in the stream matches the type id of the keyset */
  189. static bool vcap_verify_keystream_keyset(struct vcap_control *vctrl,
  190. enum vcap_type vt,
  191. u32 *keystream,
  192. u32 *mskstream,
  193. enum vcap_keyfield_set keyset)
  194. {
  195. const struct vcap_info *vcap = &vctrl->vcaps[vt];
  196. const struct vcap_field *typefld;
  197. const struct vcap_typegroup *tgt;
  198. const struct vcap_field *fields;
  199. struct vcap_stream_iter iter;
  200. const struct vcap_set *info;
  201. u32 value = 0;
  202. u32 mask = 0;
  203. if (vcap_keyfield_count(vctrl, vt, keyset) == 0)
  204. return false;
  205. info = vcap_keyfieldset(vctrl, vt, keyset);
  206. /* Check that the keyset is valid */
  207. if (!info)
  208. return false;
  209. /* a type_id of value -1 means that there is no type field */
  210. if (info->type_id == (u8)-1)
  211. return true;
  212. /* Get a valid typegroup for the specific keyset */
  213. tgt = vcap_keyfield_typegroup(vctrl, vt, keyset);
  214. if (!tgt)
  215. return false;
  216. fields = vcap_keyfields(vctrl, vt, keyset);
  217. if (!fields)
  218. return false;
  219. typefld = &fields[VCAP_KF_TYPE];
  220. vcap_iter_init(&iter, vcap->sw_width, tgt, typefld->offset);
  221. vcap_decode_field(mskstream, &iter, typefld->width, (u8 *)&mask);
  222. /* no type info if there are no mask bits */
  223. if (vcap_bitarray_zero(typefld->width, (u8 *)&mask))
  224. return false;
  225. /* Get the value of the type field in the stream and compare to the
  226. * one define in the vcap keyset
  227. */
  228. vcap_iter_init(&iter, vcap->sw_width, tgt, typefld->offset);
  229. vcap_decode_field(keystream, &iter, typefld->width, (u8 *)&value);
  230. return (value & mask) == (info->type_id & mask);
  231. }
  232. /* Verify that the typegroup bits have the correct values */
  233. static int vcap_verify_typegroups(u32 *stream, int sw_width,
  234. const struct vcap_typegroup *tgt, bool mask,
  235. int sw_max)
  236. {
  237. struct vcap_stream_iter iter;
  238. int sw_cnt, idx;
  239. vcap_iter_set(&iter, sw_width, tgt, 0);
  240. sw_cnt = 0;
  241. while (iter.tg->width) {
  242. u32 value = 0;
  243. u32 tg_value = iter.tg->value;
  244. if (mask)
  245. tg_value = (1 << iter.tg->width) - 1;
  246. /* Set position to current typegroup bit */
  247. iter.offset = iter.tg->offset;
  248. vcap_iter_update(&iter);
  249. for (idx = 0; idx < iter.tg->width; idx++) {
  250. /* Decode one typegroup bit */
  251. if (vcap_get_bit(stream, &iter))
  252. value |= 1 << idx;
  253. iter.offset++;
  254. vcap_iter_update(&iter);
  255. }
  256. if (value != tg_value)
  257. return -EINVAL;
  258. iter.tg++; /* next typegroup */
  259. sw_cnt++;
  260. /* Stop checking more typegroups */
  261. if (sw_max && sw_cnt >= sw_max)
  262. break;
  263. }
  264. return 0;
  265. }
  266. /* Find the subword width of the key typegroup that matches the stream data */
  267. static int vcap_find_keystream_typegroup_sw(struct vcap_control *vctrl,
  268. enum vcap_type vt, u32 *stream,
  269. bool mask, int sw_max)
  270. {
  271. const struct vcap_typegroup **tgt;
  272. int sw_idx, res;
  273. tgt = vctrl->vcaps[vt].keyfield_set_typegroups;
  274. /* Try the longest subword match first */
  275. for (sw_idx = vctrl->vcaps[vt].sw_count; sw_idx >= 0; sw_idx--) {
  276. if (!tgt[sw_idx])
  277. continue;
  278. res = vcap_verify_typegroups(stream, vctrl->vcaps[vt].sw_width,
  279. tgt[sw_idx], mask, sw_max);
  280. if (res == 0)
  281. return sw_idx;
  282. }
  283. return -EINVAL;
  284. }
  285. /* Verify that the typegroup information, subword count, keyset and type id
  286. * are in sync and correct, return the list of matching keysets
  287. */
  288. int
  289. vcap_find_keystream_keysets(struct vcap_control *vctrl,
  290. enum vcap_type vt,
  291. u32 *keystream,
  292. u32 *mskstream,
  293. bool mask, int sw_max,
  294. struct vcap_keyset_list *kslist)
  295. {
  296. const struct vcap_set *keyfield_set;
  297. int sw_count, idx;
  298. sw_count = vcap_find_keystream_typegroup_sw(vctrl, vt, keystream, mask,
  299. sw_max);
  300. if (sw_count < 0)
  301. return sw_count;
  302. keyfield_set = vctrl->vcaps[vt].keyfield_set;
  303. for (idx = 0; idx < vctrl->vcaps[vt].keyfield_set_size; ++idx) {
  304. if (keyfield_set[idx].sw_per_item != sw_count)
  305. continue;
  306. if (vcap_verify_keystream_keyset(vctrl, vt, keystream,
  307. mskstream, idx))
  308. vcap_keyset_list_add(kslist, idx);
  309. }
  310. if (kslist->cnt > 0)
  311. return 0;
  312. return -EINVAL;
  313. }
  314. EXPORT_SYMBOL_GPL(vcap_find_keystream_keysets);
  315. /* Read key data from a VCAP address and discover if there are any rule keysets
  316. * here
  317. */
  318. int vcap_addr_keysets(struct vcap_control *vctrl,
  319. struct net_device *ndev,
  320. struct vcap_admin *admin,
  321. int addr,
  322. struct vcap_keyset_list *kslist)
  323. {
  324. enum vcap_type vt = admin->vtype;
  325. int keyset_sw_regs, idx;
  326. u32 key = 0, mask = 0;
  327. /* Read the cache at the specified address */
  328. keyset_sw_regs = DIV_ROUND_UP(vctrl->vcaps[vt].sw_width, 32);
  329. vctrl->ops->update(ndev, admin, VCAP_CMD_READ, VCAP_SEL_ALL, addr);
  330. vctrl->ops->cache_read(ndev, admin, VCAP_SEL_ENTRY, 0,
  331. keyset_sw_regs);
  332. /* Skip uninitialized key/mask entries */
  333. for (idx = 0; idx < keyset_sw_regs; ++idx) {
  334. key |= ~admin->cache.keystream[idx];
  335. mask |= admin->cache.maskstream[idx];
  336. }
  337. if (key == 0 && mask == 0)
  338. return -EINVAL;
  339. /* Decode and locate the keysets */
  340. return vcap_find_keystream_keysets(vctrl, vt, admin->cache.keystream,
  341. admin->cache.maskstream, false, 0,
  342. kslist);
  343. }
  344. EXPORT_SYMBOL_GPL(vcap_addr_keysets);
  345. /* Return the list of keyfields for the keyset */
  346. const struct vcap_field *vcap_keyfields(struct vcap_control *vctrl,
  347. enum vcap_type vt,
  348. enum vcap_keyfield_set keyset)
  349. {
  350. /* Check that the keyset exists in the vcap keyset list */
  351. if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
  352. return NULL;
  353. return vctrl->vcaps[vt].keyfield_set_map[keyset];
  354. }
  355. /* Return the keyset information for the keyset */
  356. const struct vcap_set *vcap_keyfieldset(struct vcap_control *vctrl,
  357. enum vcap_type vt,
  358. enum vcap_keyfield_set keyset)
  359. {
  360. const struct vcap_set *kset;
  361. /* Check that the keyset exists in the vcap keyset list */
  362. if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
  363. return NULL;
  364. kset = &vctrl->vcaps[vt].keyfield_set[keyset];
  365. if (kset->sw_per_item == 0 || kset->sw_per_item > vctrl->vcaps[vt].sw_count)
  366. return NULL;
  367. return kset;
  368. }
  369. EXPORT_SYMBOL_GPL(vcap_keyfieldset);
  370. /* Return the typegroup table for the matching keyset (using subword size) */
  371. const struct vcap_typegroup *
  372. vcap_keyfield_typegroup(struct vcap_control *vctrl,
  373. enum vcap_type vt, enum vcap_keyfield_set keyset)
  374. {
  375. const struct vcap_set *kset = vcap_keyfieldset(vctrl, vt, keyset);
  376. /* Check that the keyset is valid */
  377. if (!kset)
  378. return NULL;
  379. return vctrl->vcaps[vt].keyfield_set_typegroups[kset->sw_per_item];
  380. }
  381. /* Return the number of keyfields in the keyset */
  382. int vcap_keyfield_count(struct vcap_control *vctrl,
  383. enum vcap_type vt, enum vcap_keyfield_set keyset)
  384. {
  385. /* Check that the keyset exists in the vcap keyset list */
  386. if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
  387. return 0;
  388. return vctrl->vcaps[vt].keyfield_set_map_size[keyset];
  389. }
  390. static void vcap_encode_keyfield(struct vcap_rule_internal *ri,
  391. const struct vcap_client_keyfield *kf,
  392. const struct vcap_field *rf,
  393. const struct vcap_typegroup *tgt)
  394. {
  395. int sw_width = ri->vctrl->vcaps[ri->admin->vtype].sw_width;
  396. struct vcap_cache_data *cache = &ri->admin->cache;
  397. struct vcap_stream_iter iter;
  398. const u8 *value, *mask;
  399. /* Encode the fields for the key and the mask in their respective
  400. * streams, respecting the subword width.
  401. */
  402. switch (kf->ctrl.type) {
  403. case VCAP_FIELD_BIT:
  404. value = &kf->data.u1.value;
  405. mask = &kf->data.u1.mask;
  406. break;
  407. case VCAP_FIELD_U32:
  408. value = (const u8 *)&kf->data.u32.value;
  409. mask = (const u8 *)&kf->data.u32.mask;
  410. break;
  411. case VCAP_FIELD_U48:
  412. value = kf->data.u48.value;
  413. mask = kf->data.u48.mask;
  414. break;
  415. case VCAP_FIELD_U56:
  416. value = kf->data.u56.value;
  417. mask = kf->data.u56.mask;
  418. break;
  419. case VCAP_FIELD_U64:
  420. value = kf->data.u64.value;
  421. mask = kf->data.u64.mask;
  422. break;
  423. case VCAP_FIELD_U72:
  424. value = kf->data.u72.value;
  425. mask = kf->data.u72.mask;
  426. break;
  427. case VCAP_FIELD_U112:
  428. value = kf->data.u112.value;
  429. mask = kf->data.u112.mask;
  430. break;
  431. case VCAP_FIELD_U128:
  432. value = kf->data.u128.value;
  433. mask = kf->data.u128.mask;
  434. break;
  435. }
  436. vcap_iter_init(&iter, sw_width, tgt, rf->offset);
  437. vcap_encode_field(cache->keystream, &iter, rf->width, value);
  438. vcap_iter_init(&iter, sw_width, tgt, rf->offset);
  439. vcap_encode_field(cache->maskstream, &iter, rf->width, mask);
  440. }
  441. static void vcap_encode_keyfield_typegroups(struct vcap_control *vctrl,
  442. struct vcap_rule_internal *ri,
  443. const struct vcap_typegroup *tgt)
  444. {
  445. int sw_width = vctrl->vcaps[ri->admin->vtype].sw_width;
  446. struct vcap_cache_data *cache = &ri->admin->cache;
  447. /* Encode the typegroup bits for the key and the mask in their streams,
  448. * respecting the subword width.
  449. */
  450. vcap_encode_typegroups(cache->keystream, sw_width, tgt, false);
  451. vcap_encode_typegroups(cache->maskstream, sw_width, tgt, true);
  452. }
  453. /* Copy data from src to dst but reverse the data in chunks of 32bits.
  454. * For example if src is 00:11:22:33:44:55 where 55 is LSB the dst will
  455. * have the value 22:33:44:55:00:11.
  456. */
  457. static void vcap_copy_to_w32be(u8 *dst, const u8 *src, int size)
  458. {
  459. for (int idx = 0; idx < size; ++idx) {
  460. int first_byte_index = 0;
  461. int nidx;
  462. first_byte_index = size - (((idx >> 2) + 1) << 2);
  463. if (first_byte_index < 0)
  464. first_byte_index = 0;
  465. nidx = idx + first_byte_index - (idx & ~0x3);
  466. dst[nidx] = src[idx];
  467. }
  468. }
  469. static void
  470. vcap_copy_from_client_keyfield(struct vcap_rule *rule,
  471. struct vcap_client_keyfield *dst,
  472. const struct vcap_client_keyfield *src)
  473. {
  474. struct vcap_rule_internal *ri = to_intrule(rule);
  475. const struct vcap_client_keyfield_data *sdata;
  476. struct vcap_client_keyfield_data *ddata;
  477. int size;
  478. dst->ctrl.type = src->ctrl.type;
  479. dst->ctrl.key = src->ctrl.key;
  480. INIT_LIST_HEAD(&dst->ctrl.list);
  481. sdata = &src->data;
  482. ddata = &dst->data;
  483. if (!ri->admin->w32be) {
  484. memcpy(ddata, sdata, sizeof(dst->data));
  485. return;
  486. }
  487. size = keyfield_size_table[dst->ctrl.type] / 2;
  488. switch (dst->ctrl.type) {
  489. case VCAP_FIELD_BIT:
  490. case VCAP_FIELD_U32:
  491. memcpy(ddata, sdata, sizeof(dst->data));
  492. break;
  493. case VCAP_FIELD_U48:
  494. vcap_copy_to_w32be(ddata->u48.value, src->data.u48.value, size);
  495. vcap_copy_to_w32be(ddata->u48.mask, src->data.u48.mask, size);
  496. break;
  497. case VCAP_FIELD_U56:
  498. vcap_copy_to_w32be(ddata->u56.value, sdata->u56.value, size);
  499. vcap_copy_to_w32be(ddata->u56.mask, sdata->u56.mask, size);
  500. break;
  501. case VCAP_FIELD_U64:
  502. vcap_copy_to_w32be(ddata->u64.value, sdata->u64.value, size);
  503. vcap_copy_to_w32be(ddata->u64.mask, sdata->u64.mask, size);
  504. break;
  505. case VCAP_FIELD_U72:
  506. vcap_copy_to_w32be(ddata->u72.value, sdata->u72.value, size);
  507. vcap_copy_to_w32be(ddata->u72.mask, sdata->u72.mask, size);
  508. break;
  509. case VCAP_FIELD_U112:
  510. vcap_copy_to_w32be(ddata->u112.value, sdata->u112.value, size);
  511. vcap_copy_to_w32be(ddata->u112.mask, sdata->u112.mask, size);
  512. break;
  513. case VCAP_FIELD_U128:
  514. vcap_copy_to_w32be(ddata->u128.value, sdata->u128.value, size);
  515. vcap_copy_to_w32be(ddata->u128.mask, sdata->u128.mask, size);
  516. break;
  517. }
  518. }
  519. static void
  520. vcap_copy_from_client_actionfield(struct vcap_rule *rule,
  521. struct vcap_client_actionfield *dst,
  522. const struct vcap_client_actionfield *src)
  523. {
  524. struct vcap_rule_internal *ri = to_intrule(rule);
  525. const struct vcap_client_actionfield_data *sdata;
  526. struct vcap_client_actionfield_data *ddata;
  527. int size;
  528. dst->ctrl.type = src->ctrl.type;
  529. dst->ctrl.action = src->ctrl.action;
  530. INIT_LIST_HEAD(&dst->ctrl.list);
  531. sdata = &src->data;
  532. ddata = &dst->data;
  533. if (!ri->admin->w32be) {
  534. memcpy(ddata, sdata, sizeof(dst->data));
  535. return;
  536. }
  537. size = actionfield_size_table[dst->ctrl.type];
  538. switch (dst->ctrl.type) {
  539. case VCAP_FIELD_BIT:
  540. case VCAP_FIELD_U32:
  541. memcpy(ddata, sdata, sizeof(dst->data));
  542. break;
  543. case VCAP_FIELD_U48:
  544. vcap_copy_to_w32be(ddata->u48.value, sdata->u48.value, size);
  545. break;
  546. case VCAP_FIELD_U56:
  547. vcap_copy_to_w32be(ddata->u56.value, sdata->u56.value, size);
  548. break;
  549. case VCAP_FIELD_U64:
  550. vcap_copy_to_w32be(ddata->u64.value, sdata->u64.value, size);
  551. break;
  552. case VCAP_FIELD_U72:
  553. vcap_copy_to_w32be(ddata->u72.value, sdata->u72.value, size);
  554. break;
  555. case VCAP_FIELD_U112:
  556. vcap_copy_to_w32be(ddata->u112.value, sdata->u112.value, size);
  557. break;
  558. case VCAP_FIELD_U128:
  559. vcap_copy_to_w32be(ddata->u128.value, sdata->u128.value, size);
  560. break;
  561. }
  562. }
  563. static int vcap_encode_rule_keyset(struct vcap_rule_internal *ri)
  564. {
  565. const struct vcap_client_keyfield *ckf;
  566. const struct vcap_typegroup *tg_table;
  567. struct vcap_client_keyfield tempkf;
  568. const struct vcap_field *kf_table;
  569. int keyset_size;
  570. /* Get a valid set of fields for the specific keyset */
  571. kf_table = vcap_keyfields(ri->vctrl, ri->admin->vtype, ri->data.keyset);
  572. if (!kf_table) {
  573. pr_err("%s:%d: no fields available for this keyset: %d\n",
  574. __func__, __LINE__, ri->data.keyset);
  575. return -EINVAL;
  576. }
  577. /* Get a valid typegroup for the specific keyset */
  578. tg_table = vcap_keyfield_typegroup(ri->vctrl, ri->admin->vtype,
  579. ri->data.keyset);
  580. if (!tg_table) {
  581. pr_err("%s:%d: no typegroups available for this keyset: %d\n",
  582. __func__, __LINE__, ri->data.keyset);
  583. return -EINVAL;
  584. }
  585. /* Get a valid size for the specific keyset */
  586. keyset_size = vcap_keyfield_count(ri->vctrl, ri->admin->vtype,
  587. ri->data.keyset);
  588. if (keyset_size == 0) {
  589. pr_err("%s:%d: zero field count for this keyset: %d\n",
  590. __func__, __LINE__, ri->data.keyset);
  591. return -EINVAL;
  592. }
  593. /* Iterate over the keyfields (key, mask) in the rule
  594. * and encode these bits
  595. */
  596. if (list_empty(&ri->data.keyfields)) {
  597. pr_err("%s:%d: no keyfields in the rule\n", __func__, __LINE__);
  598. return -EINVAL;
  599. }
  600. list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
  601. /* Check that the client entry exists in the keyset */
  602. if (ckf->ctrl.key >= keyset_size) {
  603. pr_err("%s:%d: key %d is not in vcap\n",
  604. __func__, __LINE__, ckf->ctrl.key);
  605. return -EINVAL;
  606. }
  607. vcap_copy_from_client_keyfield(&ri->data, &tempkf, ckf);
  608. vcap_encode_keyfield(ri, &tempkf, &kf_table[ckf->ctrl.key],
  609. tg_table);
  610. }
  611. /* Add typegroup bits to the key/mask bitstreams */
  612. vcap_encode_keyfield_typegroups(ri->vctrl, ri, tg_table);
  613. return 0;
  614. }
  615. /* Return the list of actionfields for the actionset */
  616. const struct vcap_field *
  617. vcap_actionfields(struct vcap_control *vctrl,
  618. enum vcap_type vt, enum vcap_actionfield_set actionset)
  619. {
  620. /* Check that the actionset exists in the vcap actionset list */
  621. if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
  622. return NULL;
  623. return vctrl->vcaps[vt].actionfield_set_map[actionset];
  624. }
  625. const struct vcap_set *
  626. vcap_actionfieldset(struct vcap_control *vctrl,
  627. enum vcap_type vt, enum vcap_actionfield_set actionset)
  628. {
  629. const struct vcap_set *aset;
  630. /* Check that the actionset exists in the vcap actionset list */
  631. if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
  632. return NULL;
  633. aset = &vctrl->vcaps[vt].actionfield_set[actionset];
  634. if (aset->sw_per_item == 0 || aset->sw_per_item > vctrl->vcaps[vt].sw_count)
  635. return NULL;
  636. return aset;
  637. }
  638. /* Return the typegroup table for the matching actionset (using subword size) */
  639. const struct vcap_typegroup *
  640. vcap_actionfield_typegroup(struct vcap_control *vctrl,
  641. enum vcap_type vt, enum vcap_actionfield_set actionset)
  642. {
  643. const struct vcap_set *aset = vcap_actionfieldset(vctrl, vt, actionset);
  644. /* Check that the actionset is valid */
  645. if (!aset)
  646. return NULL;
  647. return vctrl->vcaps[vt].actionfield_set_typegroups[aset->sw_per_item];
  648. }
  649. /* Return the number of actionfields in the actionset */
  650. int vcap_actionfield_count(struct vcap_control *vctrl,
  651. enum vcap_type vt,
  652. enum vcap_actionfield_set actionset)
  653. {
  654. /* Check that the actionset exists in the vcap actionset list */
  655. if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
  656. return 0;
  657. return vctrl->vcaps[vt].actionfield_set_map_size[actionset];
  658. }
  659. static void vcap_encode_actionfield(struct vcap_rule_internal *ri,
  660. const struct vcap_client_actionfield *af,
  661. const struct vcap_field *rf,
  662. const struct vcap_typegroup *tgt)
  663. {
  664. int act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
  665. struct vcap_cache_data *cache = &ri->admin->cache;
  666. struct vcap_stream_iter iter;
  667. const u8 *value;
  668. /* Encode the action field in the stream, respecting the subword width */
  669. switch (af->ctrl.type) {
  670. case VCAP_FIELD_BIT:
  671. value = &af->data.u1.value;
  672. break;
  673. case VCAP_FIELD_U32:
  674. value = (const u8 *)&af->data.u32.value;
  675. break;
  676. case VCAP_FIELD_U48:
  677. value = af->data.u48.value;
  678. break;
  679. case VCAP_FIELD_U56:
  680. value = af->data.u56.value;
  681. break;
  682. case VCAP_FIELD_U64:
  683. value = af->data.u64.value;
  684. break;
  685. case VCAP_FIELD_U72:
  686. value = af->data.u72.value;
  687. break;
  688. case VCAP_FIELD_U112:
  689. value = af->data.u112.value;
  690. break;
  691. case VCAP_FIELD_U128:
  692. value = af->data.u128.value;
  693. break;
  694. }
  695. vcap_iter_init(&iter, act_width, tgt, rf->offset);
  696. vcap_encode_field(cache->actionstream, &iter, rf->width, value);
  697. }
  698. static void vcap_encode_actionfield_typegroups(struct vcap_rule_internal *ri,
  699. const struct vcap_typegroup *tgt)
  700. {
  701. int sw_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
  702. struct vcap_cache_data *cache = &ri->admin->cache;
  703. /* Encode the typegroup bits for the actionstream respecting the subword
  704. * width.
  705. */
  706. vcap_encode_typegroups(cache->actionstream, sw_width, tgt, false);
  707. }
  708. static int vcap_encode_rule_actionset(struct vcap_rule_internal *ri)
  709. {
  710. const struct vcap_client_actionfield *caf;
  711. const struct vcap_typegroup *tg_table;
  712. struct vcap_client_actionfield tempaf;
  713. const struct vcap_field *af_table;
  714. int actionset_size;
  715. /* Get a valid set of actionset fields for the specific actionset */
  716. af_table = vcap_actionfields(ri->vctrl, ri->admin->vtype,
  717. ri->data.actionset);
  718. if (!af_table) {
  719. pr_err("%s:%d: no fields available for this actionset: %d\n",
  720. __func__, __LINE__, ri->data.actionset);
  721. return -EINVAL;
  722. }
  723. /* Get a valid typegroup for the specific actionset */
  724. tg_table = vcap_actionfield_typegroup(ri->vctrl, ri->admin->vtype,
  725. ri->data.actionset);
  726. if (!tg_table) {
  727. pr_err("%s:%d: no typegroups available for this actionset: %d\n",
  728. __func__, __LINE__, ri->data.actionset);
  729. return -EINVAL;
  730. }
  731. /* Get a valid actionset size for the specific actionset */
  732. actionset_size = vcap_actionfield_count(ri->vctrl, ri->admin->vtype,
  733. ri->data.actionset);
  734. if (actionset_size == 0) {
  735. pr_err("%s:%d: zero field count for this actionset: %d\n",
  736. __func__, __LINE__, ri->data.actionset);
  737. return -EINVAL;
  738. }
  739. /* Iterate over the actionfields in the rule
  740. * and encode these bits
  741. */
  742. if (list_empty(&ri->data.actionfields))
  743. pr_warn("%s:%d: no actionfields in the rule\n",
  744. __func__, __LINE__);
  745. list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
  746. /* Check that the client action exists in the actionset */
  747. if (caf->ctrl.action >= actionset_size) {
  748. pr_err("%s:%d: action %d is not in vcap\n",
  749. __func__, __LINE__, caf->ctrl.action);
  750. return -EINVAL;
  751. }
  752. vcap_copy_from_client_actionfield(&ri->data, &tempaf, caf);
  753. vcap_encode_actionfield(ri, &tempaf,
  754. &af_table[caf->ctrl.action], tg_table);
  755. }
  756. /* Add typegroup bits to the entry bitstreams */
  757. vcap_encode_actionfield_typegroups(ri, tg_table);
  758. return 0;
  759. }
  760. static int vcap_encode_rule(struct vcap_rule_internal *ri)
  761. {
  762. int err;
  763. err = vcap_encode_rule_keyset(ri);
  764. if (err)
  765. return err;
  766. err = vcap_encode_rule_actionset(ri);
  767. if (err)
  768. return err;
  769. return 0;
  770. }
  771. int vcap_api_check(struct vcap_control *ctrl)
  772. {
  773. if (!ctrl) {
  774. pr_err("%s:%d: vcap control is missing\n", __func__, __LINE__);
  775. return -EINVAL;
  776. }
  777. if (!ctrl->ops || !ctrl->ops->validate_keyset ||
  778. !ctrl->ops->add_default_fields || !ctrl->ops->cache_erase ||
  779. !ctrl->ops->cache_write || !ctrl->ops->cache_read ||
  780. !ctrl->ops->init || !ctrl->ops->update || !ctrl->ops->move ||
  781. !ctrl->ops->port_info) {
  782. pr_err("%s:%d: client operations are missing\n",
  783. __func__, __LINE__);
  784. return -ENOENT;
  785. }
  786. return 0;
  787. }
  788. void vcap_erase_cache(struct vcap_rule_internal *ri)
  789. {
  790. ri->vctrl->ops->cache_erase(ri->admin);
  791. }
  792. /* Update the keyset for the rule */
  793. int vcap_set_rule_set_keyset(struct vcap_rule *rule,
  794. enum vcap_keyfield_set keyset)
  795. {
  796. struct vcap_rule_internal *ri = to_intrule(rule);
  797. const struct vcap_set *kset;
  798. int sw_width;
  799. kset = vcap_keyfieldset(ri->vctrl, ri->admin->vtype, keyset);
  800. /* Check that the keyset is valid */
  801. if (!kset)
  802. return -EINVAL;
  803. ri->keyset_sw = kset->sw_per_item;
  804. sw_width = ri->vctrl->vcaps[ri->admin->vtype].sw_width;
  805. ri->keyset_sw_regs = DIV_ROUND_UP(sw_width, 32);
  806. ri->data.keyset = keyset;
  807. return 0;
  808. }
  809. EXPORT_SYMBOL_GPL(vcap_set_rule_set_keyset);
  810. /* Update the actionset for the rule */
  811. int vcap_set_rule_set_actionset(struct vcap_rule *rule,
  812. enum vcap_actionfield_set actionset)
  813. {
  814. struct vcap_rule_internal *ri = to_intrule(rule);
  815. const struct vcap_set *aset;
  816. int act_width;
  817. aset = vcap_actionfieldset(ri->vctrl, ri->admin->vtype, actionset);
  818. /* Check that the actionset is valid */
  819. if (!aset)
  820. return -EINVAL;
  821. ri->actionset_sw = aset->sw_per_item;
  822. act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
  823. ri->actionset_sw_regs = DIV_ROUND_UP(act_width, 32);
  824. ri->data.actionset = actionset;
  825. return 0;
  826. }
  827. EXPORT_SYMBOL_GPL(vcap_set_rule_set_actionset);
  828. /* Check if a rule with this id exists */
  829. static bool vcap_rule_exists(struct vcap_control *vctrl, u32 id)
  830. {
  831. struct vcap_rule_internal *ri;
  832. struct vcap_admin *admin;
  833. /* Look for the rule id in all vcaps */
  834. list_for_each_entry(admin, &vctrl->list, list)
  835. list_for_each_entry(ri, &admin->rules, list)
  836. if (ri->data.id == id)
  837. return true;
  838. return false;
  839. }
  840. /* Find a rule with a provided rule id return a locked vcap */
  841. static struct vcap_rule_internal *
  842. vcap_get_locked_rule(struct vcap_control *vctrl, u32 id)
  843. {
  844. struct vcap_rule_internal *ri;
  845. struct vcap_admin *admin;
  846. /* Look for the rule id in all vcaps */
  847. list_for_each_entry(admin, &vctrl->list, list) {
  848. mutex_lock(&admin->lock);
  849. list_for_each_entry(ri, &admin->rules, list)
  850. if (ri->data.id == id)
  851. return ri;
  852. mutex_unlock(&admin->lock);
  853. }
  854. return NULL;
  855. }
  856. /* Find a rule id with a provided cookie */
  857. int vcap_lookup_rule_by_cookie(struct vcap_control *vctrl, u64 cookie)
  858. {
  859. struct vcap_rule_internal *ri;
  860. struct vcap_admin *admin;
  861. int id = 0;
  862. /* Look for the rule id in all vcaps */
  863. list_for_each_entry(admin, &vctrl->list, list) {
  864. mutex_lock(&admin->lock);
  865. list_for_each_entry(ri, &admin->rules, list) {
  866. if (ri->data.cookie == cookie) {
  867. id = ri->data.id;
  868. break;
  869. }
  870. }
  871. mutex_unlock(&admin->lock);
  872. if (id)
  873. return id;
  874. }
  875. return -ENOENT;
  876. }
  877. EXPORT_SYMBOL_GPL(vcap_lookup_rule_by_cookie);
  878. /* Get number of rules in a vcap instance lookup chain id range */
  879. int vcap_admin_rule_count(struct vcap_admin *admin, int cid)
  880. {
  881. int max_cid = roundup(cid + 1, VCAP_CID_LOOKUP_SIZE);
  882. int min_cid = rounddown(cid, VCAP_CID_LOOKUP_SIZE);
  883. struct vcap_rule_internal *elem;
  884. int count = 0;
  885. list_for_each_entry(elem, &admin->rules, list) {
  886. mutex_lock(&admin->lock);
  887. if (elem->data.vcap_chain_id >= min_cid &&
  888. elem->data.vcap_chain_id < max_cid)
  889. ++count;
  890. mutex_unlock(&admin->lock);
  891. }
  892. return count;
  893. }
  894. EXPORT_SYMBOL_GPL(vcap_admin_rule_count);
  895. /* Make a copy of the rule, shallow or full */
  896. static struct vcap_rule_internal *vcap_dup_rule(struct vcap_rule_internal *ri,
  897. bool full)
  898. {
  899. struct vcap_client_actionfield *caf, *newcaf;
  900. struct vcap_client_keyfield *ckf, *newckf;
  901. struct vcap_rule_internal *duprule;
  902. /* Allocate the client part */
  903. duprule = kzalloc(sizeof(*duprule), GFP_KERNEL);
  904. if (!duprule)
  905. return ERR_PTR(-ENOMEM);
  906. *duprule = *ri;
  907. /* Not inserted in the VCAP */
  908. INIT_LIST_HEAD(&duprule->list);
  909. /* No elements in these lists */
  910. INIT_LIST_HEAD(&duprule->data.keyfields);
  911. INIT_LIST_HEAD(&duprule->data.actionfields);
  912. /* A full rule copy includes keys and actions */
  913. if (!full)
  914. return duprule;
  915. list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
  916. newckf = kmemdup(ckf, sizeof(*newckf), GFP_KERNEL);
  917. if (!newckf)
  918. goto err;
  919. list_add_tail(&newckf->ctrl.list, &duprule->data.keyfields);
  920. }
  921. list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
  922. newcaf = kmemdup(caf, sizeof(*newcaf), GFP_KERNEL);
  923. if (!newcaf)
  924. goto err;
  925. list_add_tail(&newcaf->ctrl.list, &duprule->data.actionfields);
  926. }
  927. return duprule;
  928. err:
  929. list_for_each_entry_safe(ckf, newckf, &duprule->data.keyfields, ctrl.list) {
  930. list_del(&ckf->ctrl.list);
  931. kfree(ckf);
  932. }
  933. list_for_each_entry_safe(caf, newcaf, &duprule->data.actionfields, ctrl.list) {
  934. list_del(&caf->ctrl.list);
  935. kfree(caf);
  936. }
  937. kfree(duprule);
  938. return ERR_PTR(-ENOMEM);
  939. }
  940. static void vcap_apply_width(u8 *dst, int width, int bytes)
  941. {
  942. u8 bmask;
  943. int idx;
  944. for (idx = 0; idx < bytes; idx++) {
  945. if (width > 0)
  946. if (width < 8)
  947. bmask = (1 << width) - 1;
  948. else
  949. bmask = ~0;
  950. else
  951. bmask = 0;
  952. dst[idx] &= bmask;
  953. width -= 8;
  954. }
  955. }
  956. static void vcap_copy_from_w32be(u8 *dst, u8 *src, int size, int width)
  957. {
  958. int idx, ridx, wstart, nidx;
  959. int tail_bytes = (((size + 4) >> 2) << 2) - size;
  960. for (idx = 0, ridx = size - 1; idx < size; ++idx, --ridx) {
  961. wstart = (idx >> 2) << 2;
  962. nidx = wstart + 3 - (idx & 0x3);
  963. if (nidx >= size)
  964. nidx -= tail_bytes;
  965. dst[nidx] = src[ridx];
  966. }
  967. vcap_apply_width(dst, width, size);
  968. }
  969. static void vcap_copy_action_bit_field(struct vcap_u1_action *field, u8 *value)
  970. {
  971. field->value = (*value) & 0x1;
  972. }
  973. static void vcap_copy_limited_actionfield(u8 *dstvalue, u8 *srcvalue,
  974. int width, int bytes)
  975. {
  976. memcpy(dstvalue, srcvalue, bytes);
  977. vcap_apply_width(dstvalue, width, bytes);
  978. }
  979. static void vcap_copy_to_client_actionfield(struct vcap_rule_internal *ri,
  980. struct vcap_client_actionfield *field,
  981. u8 *value, u16 width)
  982. {
  983. int field_size = actionfield_size_table[field->ctrl.type];
  984. if (ri->admin->w32be) {
  985. switch (field->ctrl.type) {
  986. case VCAP_FIELD_BIT:
  987. vcap_copy_action_bit_field(&field->data.u1, value);
  988. break;
  989. case VCAP_FIELD_U32:
  990. vcap_copy_limited_actionfield((u8 *)&field->data.u32.value,
  991. value,
  992. width, field_size);
  993. break;
  994. case VCAP_FIELD_U48:
  995. vcap_copy_from_w32be(field->data.u48.value, value,
  996. field_size, width);
  997. break;
  998. case VCAP_FIELD_U56:
  999. vcap_copy_from_w32be(field->data.u56.value, value,
  1000. field_size, width);
  1001. break;
  1002. case VCAP_FIELD_U64:
  1003. vcap_copy_from_w32be(field->data.u64.value, value,
  1004. field_size, width);
  1005. break;
  1006. case VCAP_FIELD_U72:
  1007. vcap_copy_from_w32be(field->data.u72.value, value,
  1008. field_size, width);
  1009. break;
  1010. case VCAP_FIELD_U112:
  1011. vcap_copy_from_w32be(field->data.u112.value, value,
  1012. field_size, width);
  1013. break;
  1014. case VCAP_FIELD_U128:
  1015. vcap_copy_from_w32be(field->data.u128.value, value,
  1016. field_size, width);
  1017. break;
  1018. }
  1019. } else {
  1020. switch (field->ctrl.type) {
  1021. case VCAP_FIELD_BIT:
  1022. vcap_copy_action_bit_field(&field->data.u1, value);
  1023. break;
  1024. case VCAP_FIELD_U32:
  1025. vcap_copy_limited_actionfield((u8 *)&field->data.u32.value,
  1026. value,
  1027. width, field_size);
  1028. break;
  1029. case VCAP_FIELD_U48:
  1030. vcap_copy_limited_actionfield(field->data.u48.value,
  1031. value,
  1032. width, field_size);
  1033. break;
  1034. case VCAP_FIELD_U56:
  1035. vcap_copy_limited_actionfield(field->data.u56.value,
  1036. value,
  1037. width, field_size);
  1038. break;
  1039. case VCAP_FIELD_U64:
  1040. vcap_copy_limited_actionfield(field->data.u64.value,
  1041. value,
  1042. width, field_size);
  1043. break;
  1044. case VCAP_FIELD_U72:
  1045. vcap_copy_limited_actionfield(field->data.u72.value,
  1046. value,
  1047. width, field_size);
  1048. break;
  1049. case VCAP_FIELD_U112:
  1050. vcap_copy_limited_actionfield(field->data.u112.value,
  1051. value,
  1052. width, field_size);
  1053. break;
  1054. case VCAP_FIELD_U128:
  1055. vcap_copy_limited_actionfield(field->data.u128.value,
  1056. value,
  1057. width, field_size);
  1058. break;
  1059. }
  1060. }
  1061. }
  1062. static void vcap_copy_key_bit_field(struct vcap_u1_key *field,
  1063. u8 *value, u8 *mask)
  1064. {
  1065. field->value = (*value) & 0x1;
  1066. field->mask = (*mask) & 0x1;
  1067. }
  1068. static void vcap_copy_limited_keyfield(u8 *dstvalue, u8 *dstmask,
  1069. u8 *srcvalue, u8 *srcmask,
  1070. int width, int bytes)
  1071. {
  1072. memcpy(dstvalue, srcvalue, bytes);
  1073. vcap_apply_width(dstvalue, width, bytes);
  1074. memcpy(dstmask, srcmask, bytes);
  1075. vcap_apply_width(dstmask, width, bytes);
  1076. }
  1077. static void vcap_copy_to_client_keyfield(struct vcap_rule_internal *ri,
  1078. struct vcap_client_keyfield *field,
  1079. u8 *value, u8 *mask, u16 width)
  1080. {
  1081. int field_size = keyfield_size_table[field->ctrl.type] / 2;
  1082. if (ri->admin->w32be) {
  1083. switch (field->ctrl.type) {
  1084. case VCAP_FIELD_BIT:
  1085. vcap_copy_key_bit_field(&field->data.u1, value, mask);
  1086. break;
  1087. case VCAP_FIELD_U32:
  1088. vcap_copy_limited_keyfield((u8 *)&field->data.u32.value,
  1089. (u8 *)&field->data.u32.mask,
  1090. value, mask,
  1091. width, field_size);
  1092. break;
  1093. case VCAP_FIELD_U48:
  1094. vcap_copy_from_w32be(field->data.u48.value, value,
  1095. field_size, width);
  1096. vcap_copy_from_w32be(field->data.u48.mask, mask,
  1097. field_size, width);
  1098. break;
  1099. case VCAP_FIELD_U56:
  1100. vcap_copy_from_w32be(field->data.u56.value, value,
  1101. field_size, width);
  1102. vcap_copy_from_w32be(field->data.u56.mask, mask,
  1103. field_size, width);
  1104. break;
  1105. case VCAP_FIELD_U64:
  1106. vcap_copy_from_w32be(field->data.u64.value, value,
  1107. field_size, width);
  1108. vcap_copy_from_w32be(field->data.u64.mask, mask,
  1109. field_size, width);
  1110. break;
  1111. case VCAP_FIELD_U72:
  1112. vcap_copy_from_w32be(field->data.u72.value, value,
  1113. field_size, width);
  1114. vcap_copy_from_w32be(field->data.u72.mask, mask,
  1115. field_size, width);
  1116. break;
  1117. case VCAP_FIELD_U112:
  1118. vcap_copy_from_w32be(field->data.u112.value, value,
  1119. field_size, width);
  1120. vcap_copy_from_w32be(field->data.u112.mask, mask,
  1121. field_size, width);
  1122. break;
  1123. case VCAP_FIELD_U128:
  1124. vcap_copy_from_w32be(field->data.u128.value, value,
  1125. field_size, width);
  1126. vcap_copy_from_w32be(field->data.u128.mask, mask,
  1127. field_size, width);
  1128. break;
  1129. }
  1130. } else {
  1131. switch (field->ctrl.type) {
  1132. case VCAP_FIELD_BIT:
  1133. vcap_copy_key_bit_field(&field->data.u1, value, mask);
  1134. break;
  1135. case VCAP_FIELD_U32:
  1136. vcap_copy_limited_keyfield((u8 *)&field->data.u32.value,
  1137. (u8 *)&field->data.u32.mask,
  1138. value, mask,
  1139. width, field_size);
  1140. break;
  1141. case VCAP_FIELD_U48:
  1142. vcap_copy_limited_keyfield(field->data.u48.value,
  1143. field->data.u48.mask,
  1144. value, mask,
  1145. width, field_size);
  1146. break;
  1147. case VCAP_FIELD_U56:
  1148. vcap_copy_limited_keyfield(field->data.u56.value,
  1149. field->data.u56.mask,
  1150. value, mask,
  1151. width, field_size);
  1152. break;
  1153. case VCAP_FIELD_U64:
  1154. vcap_copy_limited_keyfield(field->data.u64.value,
  1155. field->data.u64.mask,
  1156. value, mask,
  1157. width, field_size);
  1158. break;
  1159. case VCAP_FIELD_U72:
  1160. vcap_copy_limited_keyfield(field->data.u72.value,
  1161. field->data.u72.mask,
  1162. value, mask,
  1163. width, field_size);
  1164. break;
  1165. case VCAP_FIELD_U112:
  1166. vcap_copy_limited_keyfield(field->data.u112.value,
  1167. field->data.u112.mask,
  1168. value, mask,
  1169. width, field_size);
  1170. break;
  1171. case VCAP_FIELD_U128:
  1172. vcap_copy_limited_keyfield(field->data.u128.value,
  1173. field->data.u128.mask,
  1174. value, mask,
  1175. width, field_size);
  1176. break;
  1177. }
  1178. }
  1179. }
  1180. static void vcap_rule_alloc_keyfield(struct vcap_rule_internal *ri,
  1181. const struct vcap_field *keyfield,
  1182. enum vcap_key_field key,
  1183. u8 *value, u8 *mask)
  1184. {
  1185. struct vcap_client_keyfield *field;
  1186. field = kzalloc(sizeof(*field), GFP_KERNEL);
  1187. if (!field)
  1188. return;
  1189. INIT_LIST_HEAD(&field->ctrl.list);
  1190. field->ctrl.key = key;
  1191. field->ctrl.type = keyfield->type;
  1192. vcap_copy_to_client_keyfield(ri, field, value, mask, keyfield->width);
  1193. list_add_tail(&field->ctrl.list, &ri->data.keyfields);
  1194. }
  1195. /* Read key data from a VCAP address and discover if there is a rule keyset
  1196. * here
  1197. */
  1198. static bool
  1199. vcap_verify_actionstream_actionset(struct vcap_control *vctrl,
  1200. enum vcap_type vt,
  1201. u32 *actionstream,
  1202. enum vcap_actionfield_set actionset)
  1203. {
  1204. const struct vcap_typegroup *tgt;
  1205. const struct vcap_field *fields;
  1206. const struct vcap_set *info;
  1207. if (vcap_actionfield_count(vctrl, vt, actionset) == 0)
  1208. return false;
  1209. info = vcap_actionfieldset(vctrl, vt, actionset);
  1210. /* Check that the actionset is valid */
  1211. if (!info)
  1212. return false;
  1213. /* a type_id of value -1 means that there is no type field */
  1214. if (info->type_id == (u8)-1)
  1215. return true;
  1216. /* Get a valid typegroup for the specific actionset */
  1217. tgt = vcap_actionfield_typegroup(vctrl, vt, actionset);
  1218. if (!tgt)
  1219. return false;
  1220. fields = vcap_actionfields(vctrl, vt, actionset);
  1221. if (!fields)
  1222. return false;
  1223. /* Later this will be expanded with a check of the type id */
  1224. return true;
  1225. }
  1226. /* Find the subword width of the action typegroup that matches the stream data
  1227. */
  1228. static int vcap_find_actionstream_typegroup_sw(struct vcap_control *vctrl,
  1229. enum vcap_type vt, u32 *stream,
  1230. int sw_max)
  1231. {
  1232. const struct vcap_typegroup **tgt;
  1233. int sw_idx, res;
  1234. tgt = vctrl->vcaps[vt].actionfield_set_typegroups;
  1235. /* Try the longest subword match first */
  1236. for (sw_idx = vctrl->vcaps[vt].sw_count; sw_idx >= 0; sw_idx--) {
  1237. if (!tgt[sw_idx])
  1238. continue;
  1239. res = vcap_verify_typegroups(stream, vctrl->vcaps[vt].act_width,
  1240. tgt[sw_idx], false, sw_max);
  1241. if (res == 0)
  1242. return sw_idx;
  1243. }
  1244. return -EINVAL;
  1245. }
  1246. /* Verify that the typegroup information, subword count, actionset and type id
  1247. * are in sync and correct, return the actionset
  1248. */
  1249. static enum vcap_actionfield_set
  1250. vcap_find_actionstream_actionset(struct vcap_control *vctrl,
  1251. enum vcap_type vt,
  1252. u32 *stream,
  1253. int sw_max)
  1254. {
  1255. const struct vcap_set *actionfield_set;
  1256. int sw_count, idx;
  1257. bool res;
  1258. sw_count = vcap_find_actionstream_typegroup_sw(vctrl, vt, stream,
  1259. sw_max);
  1260. if (sw_count < 0)
  1261. return sw_count;
  1262. actionfield_set = vctrl->vcaps[vt].actionfield_set;
  1263. for (idx = 0; idx < vctrl->vcaps[vt].actionfield_set_size; ++idx) {
  1264. if (actionfield_set[idx].sw_per_item != sw_count)
  1265. continue;
  1266. res = vcap_verify_actionstream_actionset(vctrl, vt,
  1267. stream, idx);
  1268. if (res)
  1269. return idx;
  1270. }
  1271. return -EINVAL;
  1272. }
  1273. /* Store action value in an element in a list for the client */
  1274. static void vcap_rule_alloc_actionfield(struct vcap_rule_internal *ri,
  1275. const struct vcap_field *actionfield,
  1276. enum vcap_action_field action,
  1277. u8 *value)
  1278. {
  1279. struct vcap_client_actionfield *field;
  1280. field = kzalloc(sizeof(*field), GFP_KERNEL);
  1281. if (!field)
  1282. return;
  1283. INIT_LIST_HEAD(&field->ctrl.list);
  1284. field->ctrl.action = action;
  1285. field->ctrl.type = actionfield->type;
  1286. vcap_copy_to_client_actionfield(ri, field, value, actionfield->width);
  1287. list_add_tail(&field->ctrl.list, &ri->data.actionfields);
  1288. }
  1289. static int vcap_decode_actionset(struct vcap_rule_internal *ri)
  1290. {
  1291. struct vcap_control *vctrl = ri->vctrl;
  1292. struct vcap_admin *admin = ri->admin;
  1293. const struct vcap_field *actionfield;
  1294. enum vcap_actionfield_set actionset;
  1295. enum vcap_type vt = admin->vtype;
  1296. const struct vcap_typegroup *tgt;
  1297. struct vcap_stream_iter iter;
  1298. int idx, res, actfield_count;
  1299. u32 *actstream;
  1300. u8 value[16];
  1301. actstream = admin->cache.actionstream;
  1302. res = vcap_find_actionstream_actionset(vctrl, vt, actstream, 0);
  1303. if (res < 0) {
  1304. pr_err("%s:%d: could not find valid actionset: %d\n",
  1305. __func__, __LINE__, res);
  1306. return -EINVAL;
  1307. }
  1308. actionset = res;
  1309. actfield_count = vcap_actionfield_count(vctrl, vt, actionset);
  1310. actionfield = vcap_actionfields(vctrl, vt, actionset);
  1311. tgt = vcap_actionfield_typegroup(vctrl, vt, actionset);
  1312. /* Start decoding the stream */
  1313. for (idx = 0; idx < actfield_count; ++idx) {
  1314. if (actionfield[idx].width <= 0)
  1315. continue;
  1316. /* Get the action */
  1317. memset(value, 0, DIV_ROUND_UP(actionfield[idx].width, 8));
  1318. vcap_iter_init(&iter, vctrl->vcaps[vt].act_width, tgt,
  1319. actionfield[idx].offset);
  1320. vcap_decode_field(actstream, &iter, actionfield[idx].width,
  1321. value);
  1322. /* Skip if no bits are set */
  1323. if (vcap_bitarray_zero(actionfield[idx].width, value))
  1324. continue;
  1325. vcap_rule_alloc_actionfield(ri, &actionfield[idx], idx, value);
  1326. /* Later the action id will also be checked */
  1327. }
  1328. return vcap_set_rule_set_actionset((struct vcap_rule *)ri, actionset);
  1329. }
  1330. static int vcap_decode_keyset(struct vcap_rule_internal *ri)
  1331. {
  1332. struct vcap_control *vctrl = ri->vctrl;
  1333. struct vcap_stream_iter kiter, miter;
  1334. struct vcap_admin *admin = ri->admin;
  1335. enum vcap_keyfield_set keysets[10];
  1336. const struct vcap_field *keyfield;
  1337. enum vcap_type vt = admin->vtype;
  1338. const struct vcap_typegroup *tgt;
  1339. struct vcap_keyset_list matches;
  1340. enum vcap_keyfield_set keyset;
  1341. int idx, res, keyfield_count;
  1342. u32 *maskstream;
  1343. u32 *keystream;
  1344. u8 value[16];
  1345. u8 mask[16];
  1346. keystream = admin->cache.keystream;
  1347. maskstream = admin->cache.maskstream;
  1348. matches.keysets = keysets;
  1349. matches.cnt = 0;
  1350. matches.max = ARRAY_SIZE(keysets);
  1351. res = vcap_find_keystream_keysets(vctrl, vt, keystream, maskstream,
  1352. false, 0, &matches);
  1353. if (res < 0) {
  1354. pr_err("%s:%d: could not find valid keysets: %d\n",
  1355. __func__, __LINE__, res);
  1356. return -EINVAL;
  1357. }
  1358. keyset = matches.keysets[0];
  1359. keyfield_count = vcap_keyfield_count(vctrl, vt, keyset);
  1360. keyfield = vcap_keyfields(vctrl, vt, keyset);
  1361. tgt = vcap_keyfield_typegroup(vctrl, vt, keyset);
  1362. /* Start decoding the streams */
  1363. for (idx = 0; idx < keyfield_count; ++idx) {
  1364. if (keyfield[idx].width <= 0)
  1365. continue;
  1366. /* First get the mask */
  1367. memset(mask, 0, DIV_ROUND_UP(keyfield[idx].width, 8));
  1368. vcap_iter_init(&miter, vctrl->vcaps[vt].sw_width, tgt,
  1369. keyfield[idx].offset);
  1370. vcap_decode_field(maskstream, &miter, keyfield[idx].width,
  1371. mask);
  1372. /* Skip if no mask bits are set */
  1373. if (vcap_bitarray_zero(keyfield[idx].width, mask))
  1374. continue;
  1375. /* Get the key */
  1376. memset(value, 0, DIV_ROUND_UP(keyfield[idx].width, 8));
  1377. vcap_iter_init(&kiter, vctrl->vcaps[vt].sw_width, tgt,
  1378. keyfield[idx].offset);
  1379. vcap_decode_field(keystream, &kiter, keyfield[idx].width,
  1380. value);
  1381. vcap_rule_alloc_keyfield(ri, &keyfield[idx], idx, value, mask);
  1382. }
  1383. return vcap_set_rule_set_keyset((struct vcap_rule *)ri, keyset);
  1384. }
  1385. /* Read VCAP content into the VCAP cache */
  1386. static int vcap_read_rule(struct vcap_rule_internal *ri)
  1387. {
  1388. struct vcap_admin *admin = ri->admin;
  1389. int sw_idx, ent_idx = 0, act_idx = 0;
  1390. u32 addr = ri->addr;
  1391. if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) {
  1392. pr_err("%s:%d: rule is empty\n", __func__, __LINE__);
  1393. return -EINVAL;
  1394. }
  1395. vcap_erase_cache(ri);
  1396. /* Use the values in the streams to read the VCAP cache */
  1397. for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) {
  1398. ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ,
  1399. VCAP_SEL_ALL, addr);
  1400. ri->vctrl->ops->cache_read(ri->ndev, admin,
  1401. VCAP_SEL_ENTRY, ent_idx,
  1402. ri->keyset_sw_regs);
  1403. ri->vctrl->ops->cache_read(ri->ndev, admin,
  1404. VCAP_SEL_ACTION, act_idx,
  1405. ri->actionset_sw_regs);
  1406. if (sw_idx == 0)
  1407. ri->vctrl->ops->cache_read(ri->ndev, admin,
  1408. VCAP_SEL_COUNTER,
  1409. ri->counter_id, 0);
  1410. ent_idx += ri->keyset_sw_regs;
  1411. act_idx += ri->actionset_sw_regs;
  1412. }
  1413. return 0;
  1414. }
  1415. /* Write VCAP cache content to the VCAP HW instance */
  1416. static int vcap_write_rule(struct vcap_rule_internal *ri)
  1417. {
  1418. struct vcap_admin *admin = ri->admin;
  1419. int sw_idx, ent_idx = 0, act_idx = 0;
  1420. u32 addr = ri->addr;
  1421. if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) {
  1422. pr_err("%s:%d: rule is empty\n", __func__, __LINE__);
  1423. return -EINVAL;
  1424. }
  1425. /* Use the values in the streams to write the VCAP cache */
  1426. for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) {
  1427. ri->vctrl->ops->cache_write(ri->ndev, admin,
  1428. VCAP_SEL_ENTRY, ent_idx,
  1429. ri->keyset_sw_regs);
  1430. ri->vctrl->ops->cache_write(ri->ndev, admin,
  1431. VCAP_SEL_ACTION, act_idx,
  1432. ri->actionset_sw_regs);
  1433. ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE,
  1434. VCAP_SEL_ALL, addr);
  1435. ent_idx += ri->keyset_sw_regs;
  1436. act_idx += ri->actionset_sw_regs;
  1437. }
  1438. return 0;
  1439. }
  1440. static int vcap_write_counter(struct vcap_rule_internal *ri,
  1441. struct vcap_counter *ctr)
  1442. {
  1443. struct vcap_admin *admin = ri->admin;
  1444. admin->cache.counter = ctr->value;
  1445. admin->cache.sticky = ctr->sticky;
  1446. ri->vctrl->ops->cache_write(ri->ndev, admin, VCAP_SEL_COUNTER,
  1447. ri->counter_id, 0);
  1448. ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE,
  1449. VCAP_SEL_COUNTER, ri->addr);
  1450. return 0;
  1451. }
  1452. /* Convert a chain id to a VCAP lookup index */
  1453. int vcap_chain_id_to_lookup(struct vcap_admin *admin, int cur_cid)
  1454. {
  1455. int lookup_first = admin->vinst * admin->lookups_per_instance;
  1456. int lookup_last = lookup_first + admin->lookups_per_instance;
  1457. int cid_next = admin->first_cid + VCAP_CID_LOOKUP_SIZE;
  1458. int cid = admin->first_cid;
  1459. int lookup;
  1460. for (lookup = lookup_first; lookup < lookup_last; ++lookup,
  1461. cid += VCAP_CID_LOOKUP_SIZE, cid_next += VCAP_CID_LOOKUP_SIZE)
  1462. if (cur_cid >= cid && cur_cid < cid_next)
  1463. return lookup;
  1464. return 0;
  1465. }
  1466. EXPORT_SYMBOL_GPL(vcap_chain_id_to_lookup);
  1467. /* Lookup a vcap instance using chain id */
  1468. struct vcap_admin *vcap_find_admin(struct vcap_control *vctrl, int cid)
  1469. {
  1470. struct vcap_admin *admin;
  1471. if (vcap_api_check(vctrl))
  1472. return NULL;
  1473. list_for_each_entry(admin, &vctrl->list, list) {
  1474. if (cid >= admin->first_cid && cid <= admin->last_cid)
  1475. return admin;
  1476. }
  1477. return NULL;
  1478. }
  1479. EXPORT_SYMBOL_GPL(vcap_find_admin);
  1480. /* Is this the last admin instance ordered by chain id and direction */
  1481. static bool vcap_admin_is_last(struct vcap_control *vctrl,
  1482. struct vcap_admin *admin,
  1483. bool ingress)
  1484. {
  1485. struct vcap_admin *iter, *last = NULL;
  1486. int max_cid = 0;
  1487. list_for_each_entry(iter, &vctrl->list, list) {
  1488. if (iter->first_cid > max_cid &&
  1489. iter->ingress == ingress) {
  1490. last = iter;
  1491. max_cid = iter->first_cid;
  1492. }
  1493. }
  1494. if (!last)
  1495. return false;
  1496. return admin == last;
  1497. }
  1498. /* Calculate the value used for chaining VCAP rules */
  1499. int vcap_chain_offset(struct vcap_control *vctrl, int from_cid, int to_cid)
  1500. {
  1501. int diff = to_cid - from_cid;
  1502. if (diff < 0) /* Wrong direction */
  1503. return diff;
  1504. to_cid %= VCAP_CID_LOOKUP_SIZE;
  1505. if (to_cid == 0) /* Destination aligned to a lookup == no chaining */
  1506. return 0;
  1507. diff %= VCAP_CID_LOOKUP_SIZE; /* Limit to a value within a lookup */
  1508. return diff;
  1509. }
  1510. EXPORT_SYMBOL_GPL(vcap_chain_offset);
  1511. /* Is the next chain id in one of the following lookups
  1512. * For now this does not support filters linked to other filters using
  1513. * keys and actions. That will be added later.
  1514. */
  1515. bool vcap_is_next_lookup(struct vcap_control *vctrl, int src_cid, int dst_cid)
  1516. {
  1517. struct vcap_admin *admin;
  1518. int next_cid;
  1519. if (vcap_api_check(vctrl))
  1520. return false;
  1521. /* The offset must be at least one lookup so round up one chain */
  1522. next_cid = roundup(src_cid + 1, VCAP_CID_LOOKUP_SIZE);
  1523. if (dst_cid < next_cid)
  1524. return false;
  1525. admin = vcap_find_admin(vctrl, dst_cid);
  1526. if (!admin)
  1527. return false;
  1528. return true;
  1529. }
  1530. EXPORT_SYMBOL_GPL(vcap_is_next_lookup);
  1531. /* Check if there is room for a new rule */
  1532. static int vcap_rule_space(struct vcap_admin *admin, int size)
  1533. {
  1534. if (admin->last_used_addr - size < admin->first_valid_addr) {
  1535. pr_err("%s:%d: No room for rule size: %u, %u\n",
  1536. __func__, __LINE__, size, admin->first_valid_addr);
  1537. return -ENOSPC;
  1538. }
  1539. return 0;
  1540. }
  1541. /* Add the keyset typefield to the list of rule keyfields */
  1542. static int vcap_add_type_keyfield(struct vcap_rule *rule)
  1543. {
  1544. struct vcap_rule_internal *ri = to_intrule(rule);
  1545. enum vcap_keyfield_set keyset = rule->keyset;
  1546. enum vcap_type vt = ri->admin->vtype;
  1547. const struct vcap_field *fields;
  1548. const struct vcap_set *kset;
  1549. int ret = -EINVAL;
  1550. kset = vcap_keyfieldset(ri->vctrl, vt, keyset);
  1551. if (!kset)
  1552. return ret;
  1553. if (kset->type_id == (u8)-1) /* No type field is needed */
  1554. return 0;
  1555. fields = vcap_keyfields(ri->vctrl, vt, keyset);
  1556. if (!fields)
  1557. return -EINVAL;
  1558. if (fields[VCAP_KF_TYPE].width > 1) {
  1559. ret = vcap_rule_add_key_u32(rule, VCAP_KF_TYPE,
  1560. kset->type_id, 0xff);
  1561. } else {
  1562. if (kset->type_id)
  1563. ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE,
  1564. VCAP_BIT_1);
  1565. else
  1566. ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE,
  1567. VCAP_BIT_0);
  1568. }
  1569. return 0;
  1570. }
  1571. /* Add the actionset typefield to the list of rule actionfields */
  1572. static int vcap_add_type_actionfield(struct vcap_rule *rule)
  1573. {
  1574. enum vcap_actionfield_set actionset = rule->actionset;
  1575. struct vcap_rule_internal *ri = to_intrule(rule);
  1576. enum vcap_type vt = ri->admin->vtype;
  1577. const struct vcap_field *fields;
  1578. const struct vcap_set *aset;
  1579. int ret = -EINVAL;
  1580. aset = vcap_actionfieldset(ri->vctrl, vt, actionset);
  1581. if (!aset)
  1582. return ret;
  1583. if (aset->type_id == (u8)-1) /* No type field is needed */
  1584. return 0;
  1585. fields = vcap_actionfields(ri->vctrl, vt, actionset);
  1586. if (!fields)
  1587. return -EINVAL;
  1588. if (fields[VCAP_AF_TYPE].width > 1) {
  1589. ret = vcap_rule_add_action_u32(rule, VCAP_AF_TYPE,
  1590. aset->type_id);
  1591. } else {
  1592. if (aset->type_id)
  1593. ret = vcap_rule_add_action_bit(rule, VCAP_AF_TYPE,
  1594. VCAP_BIT_1);
  1595. else
  1596. ret = vcap_rule_add_action_bit(rule, VCAP_AF_TYPE,
  1597. VCAP_BIT_0);
  1598. }
  1599. return ret;
  1600. }
  1601. /* Add a keyset to a keyset list */
  1602. bool vcap_keyset_list_add(struct vcap_keyset_list *keysetlist,
  1603. enum vcap_keyfield_set keyset)
  1604. {
  1605. int idx;
  1606. if (keysetlist->cnt < keysetlist->max) {
  1607. /* Avoid duplicates */
  1608. for (idx = 0; idx < keysetlist->cnt; ++idx)
  1609. if (keysetlist->keysets[idx] == keyset)
  1610. return keysetlist->cnt < keysetlist->max;
  1611. keysetlist->keysets[keysetlist->cnt++] = keyset;
  1612. }
  1613. return keysetlist->cnt < keysetlist->max;
  1614. }
  1615. EXPORT_SYMBOL_GPL(vcap_keyset_list_add);
  1616. /* Add a actionset to a actionset list */
  1617. static bool vcap_actionset_list_add(struct vcap_actionset_list *actionsetlist,
  1618. enum vcap_actionfield_set actionset)
  1619. {
  1620. int idx;
  1621. if (actionsetlist->cnt < actionsetlist->max) {
  1622. /* Avoid duplicates */
  1623. for (idx = 0; idx < actionsetlist->cnt; ++idx)
  1624. if (actionsetlist->actionsets[idx] == actionset)
  1625. return actionsetlist->cnt < actionsetlist->max;
  1626. actionsetlist->actionsets[actionsetlist->cnt++] = actionset;
  1627. }
  1628. return actionsetlist->cnt < actionsetlist->max;
  1629. }
  1630. /* map keyset id to a string with the keyset name */
  1631. const char *vcap_keyset_name(struct vcap_control *vctrl,
  1632. enum vcap_keyfield_set keyset)
  1633. {
  1634. return vctrl->stats->keyfield_set_names[keyset];
  1635. }
  1636. EXPORT_SYMBOL_GPL(vcap_keyset_name);
  1637. /* map key field id to a string with the key name */
  1638. const char *vcap_keyfield_name(struct vcap_control *vctrl,
  1639. enum vcap_key_field key)
  1640. {
  1641. return vctrl->stats->keyfield_names[key];
  1642. }
  1643. EXPORT_SYMBOL_GPL(vcap_keyfield_name);
  1644. /* map actionset id to a string with the actionset name */
  1645. const char *vcap_actionset_name(struct vcap_control *vctrl,
  1646. enum vcap_actionfield_set actionset)
  1647. {
  1648. return vctrl->stats->actionfield_set_names[actionset];
  1649. }
  1650. /* map action field id to a string with the action name */
  1651. const char *vcap_actionfield_name(struct vcap_control *vctrl,
  1652. enum vcap_action_field action)
  1653. {
  1654. return vctrl->stats->actionfield_names[action];
  1655. }
  1656. /* Return the keyfield that matches a key in a keyset */
  1657. static const struct vcap_field *
  1658. vcap_find_keyset_keyfield(struct vcap_control *vctrl,
  1659. enum vcap_type vtype,
  1660. enum vcap_keyfield_set keyset,
  1661. enum vcap_key_field key)
  1662. {
  1663. const struct vcap_field *fields;
  1664. int idx, count;
  1665. fields = vcap_keyfields(vctrl, vtype, keyset);
  1666. if (!fields)
  1667. return NULL;
  1668. /* Iterate the keyfields of the keyset */
  1669. count = vcap_keyfield_count(vctrl, vtype, keyset);
  1670. for (idx = 0; idx < count; ++idx) {
  1671. if (fields[idx].width == 0)
  1672. continue;
  1673. if (key == idx)
  1674. return &fields[idx];
  1675. }
  1676. return NULL;
  1677. }
  1678. /* Match a list of keys against the keysets available in a vcap type */
  1679. static bool _vcap_rule_find_keysets(struct vcap_rule_internal *ri,
  1680. struct vcap_keyset_list *matches)
  1681. {
  1682. const struct vcap_client_keyfield *ckf;
  1683. int keyset, found, keycount, map_size;
  1684. const struct vcap_field **map;
  1685. enum vcap_type vtype;
  1686. vtype = ri->admin->vtype;
  1687. map = ri->vctrl->vcaps[vtype].keyfield_set_map;
  1688. map_size = ri->vctrl->vcaps[vtype].keyfield_set_size;
  1689. /* Get a count of the keyfields we want to match */
  1690. keycount = 0;
  1691. list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
  1692. ++keycount;
  1693. matches->cnt = 0;
  1694. /* Iterate the keysets of the VCAP */
  1695. for (keyset = 0; keyset < map_size; ++keyset) {
  1696. if (!map[keyset])
  1697. continue;
  1698. /* Iterate the keys in the rule */
  1699. found = 0;
  1700. list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
  1701. if (vcap_find_keyset_keyfield(ri->vctrl, vtype,
  1702. keyset, ckf->ctrl.key))
  1703. ++found;
  1704. /* Save the keyset if all keyfields were found */
  1705. if (found == keycount)
  1706. if (!vcap_keyset_list_add(matches, keyset))
  1707. /* bail out when the quota is filled */
  1708. break;
  1709. }
  1710. return matches->cnt > 0;
  1711. }
  1712. /* Match a list of keys against the keysets available in a vcap type */
  1713. bool vcap_rule_find_keysets(struct vcap_rule *rule,
  1714. struct vcap_keyset_list *matches)
  1715. {
  1716. struct vcap_rule_internal *ri = to_intrule(rule);
  1717. return _vcap_rule_find_keysets(ri, matches);
  1718. }
  1719. EXPORT_SYMBOL_GPL(vcap_rule_find_keysets);
  1720. /* Return the actionfield that matches a action in a actionset */
  1721. static const struct vcap_field *
  1722. vcap_find_actionset_actionfield(struct vcap_control *vctrl,
  1723. enum vcap_type vtype,
  1724. enum vcap_actionfield_set actionset,
  1725. enum vcap_action_field action)
  1726. {
  1727. const struct vcap_field *fields;
  1728. int idx, count;
  1729. fields = vcap_actionfields(vctrl, vtype, actionset);
  1730. if (!fields)
  1731. return NULL;
  1732. /* Iterate the actionfields of the actionset */
  1733. count = vcap_actionfield_count(vctrl, vtype, actionset);
  1734. for (idx = 0; idx < count; ++idx) {
  1735. if (fields[idx].width == 0)
  1736. continue;
  1737. if (action == idx)
  1738. return &fields[idx];
  1739. }
  1740. return NULL;
  1741. }
  1742. /* Match a list of actions against the actionsets available in a vcap type */
  1743. static bool vcap_rule_find_actionsets(struct vcap_rule_internal *ri,
  1744. struct vcap_actionset_list *matches)
  1745. {
  1746. int actionset, found, actioncount, map_size;
  1747. const struct vcap_client_actionfield *ckf;
  1748. const struct vcap_field **map;
  1749. enum vcap_type vtype;
  1750. vtype = ri->admin->vtype;
  1751. map = ri->vctrl->vcaps[vtype].actionfield_set_map;
  1752. map_size = ri->vctrl->vcaps[vtype].actionfield_set_size;
  1753. /* Get a count of the actionfields we want to match */
  1754. actioncount = 0;
  1755. list_for_each_entry(ckf, &ri->data.actionfields, ctrl.list)
  1756. ++actioncount;
  1757. matches->cnt = 0;
  1758. /* Iterate the actionsets of the VCAP */
  1759. for (actionset = 0; actionset < map_size; ++actionset) {
  1760. if (!map[actionset])
  1761. continue;
  1762. /* Iterate the actions in the rule */
  1763. found = 0;
  1764. list_for_each_entry(ckf, &ri->data.actionfields, ctrl.list)
  1765. if (vcap_find_actionset_actionfield(ri->vctrl, vtype,
  1766. actionset,
  1767. ckf->ctrl.action))
  1768. ++found;
  1769. /* Save the actionset if all actionfields were found */
  1770. if (found == actioncount)
  1771. if (!vcap_actionset_list_add(matches, actionset))
  1772. /* bail out when the quota is filled */
  1773. break;
  1774. }
  1775. return matches->cnt > 0;
  1776. }
  1777. /* Validate a rule with respect to available port keys */
  1778. int vcap_val_rule(struct vcap_rule *rule, u16 l3_proto)
  1779. {
  1780. struct vcap_rule_internal *ri = to_intrule(rule);
  1781. struct vcap_keyset_list matches = {};
  1782. enum vcap_keyfield_set keysets[10];
  1783. int ret;
  1784. ret = vcap_api_check(ri->vctrl);
  1785. if (ret)
  1786. return ret;
  1787. if (!ri->admin) {
  1788. ri->data.exterr = VCAP_ERR_NO_ADMIN;
  1789. return -EINVAL;
  1790. }
  1791. if (!ri->ndev) {
  1792. ri->data.exterr = VCAP_ERR_NO_NETDEV;
  1793. return -EINVAL;
  1794. }
  1795. matches.keysets = keysets;
  1796. matches.max = ARRAY_SIZE(keysets);
  1797. if (ri->data.keyset == VCAP_KFS_NO_VALUE) {
  1798. /* Iterate over rule keyfields and select keysets that fits */
  1799. if (!_vcap_rule_find_keysets(ri, &matches)) {
  1800. ri->data.exterr = VCAP_ERR_NO_KEYSET_MATCH;
  1801. return -EINVAL;
  1802. }
  1803. } else {
  1804. /* prepare for keyset validation */
  1805. keysets[0] = ri->data.keyset;
  1806. matches.cnt = 1;
  1807. }
  1808. /* Pick a keyset that is supported in the port lookups */
  1809. ret = ri->vctrl->ops->validate_keyset(ri->ndev, ri->admin, rule,
  1810. &matches, l3_proto);
  1811. if (ret < 0) {
  1812. pr_err("%s:%d: keyset validation failed: %d\n",
  1813. __func__, __LINE__, ret);
  1814. ri->data.exterr = VCAP_ERR_NO_PORT_KEYSET_MATCH;
  1815. return ret;
  1816. }
  1817. /* use the keyset that is supported in the port lookups */
  1818. ret = vcap_set_rule_set_keyset(rule, ret);
  1819. if (ret < 0) {
  1820. pr_err("%s:%d: keyset was not updated: %d\n",
  1821. __func__, __LINE__, ret);
  1822. return ret;
  1823. }
  1824. if (ri->data.actionset == VCAP_AFS_NO_VALUE) {
  1825. struct vcap_actionset_list matches = {};
  1826. enum vcap_actionfield_set actionsets[10];
  1827. matches.actionsets = actionsets;
  1828. matches.max = ARRAY_SIZE(actionsets);
  1829. /* Find an actionset that fits the rule actions */
  1830. if (!vcap_rule_find_actionsets(ri, &matches)) {
  1831. ri->data.exterr = VCAP_ERR_NO_ACTIONSET_MATCH;
  1832. return -EINVAL;
  1833. }
  1834. ret = vcap_set_rule_set_actionset(rule, actionsets[0]);
  1835. if (ret < 0) {
  1836. pr_err("%s:%d: actionset was not updated: %d\n",
  1837. __func__, __LINE__, ret);
  1838. return ret;
  1839. }
  1840. }
  1841. vcap_add_type_keyfield(rule);
  1842. vcap_add_type_actionfield(rule);
  1843. /* Add default fields to this rule */
  1844. ri->vctrl->ops->add_default_fields(ri->ndev, ri->admin, rule);
  1845. /* Rule size is the maximum of the entry and action subword count */
  1846. ri->size = max(ri->keyset_sw, ri->actionset_sw);
  1847. /* Finally check if there is room for the rule in the VCAP */
  1848. return vcap_rule_space(ri->admin, ri->size);
  1849. }
  1850. EXPORT_SYMBOL_GPL(vcap_val_rule);
  1851. /* Entries are sorted with increasing values of sort_key.
  1852. * I.e. Lowest numerical sort_key is first in list.
  1853. * In order to locate largest keys first in list we negate the key size with
  1854. * (max_size - size).
  1855. */
  1856. static u32 vcap_sort_key(u32 max_size, u32 size, u8 user, u16 prio)
  1857. {
  1858. return ((max_size - size) << 24) | (user << 16) | prio;
  1859. }
  1860. /* calculate the address of the next rule after this (lower address and prio) */
  1861. static u32 vcap_next_rule_addr(u32 addr, struct vcap_rule_internal *ri)
  1862. {
  1863. return ((addr - ri->size) / ri->size) * ri->size;
  1864. }
  1865. /* Assign a unique rule id and autogenerate one if id == 0 */
  1866. static u32 vcap_set_rule_id(struct vcap_rule_internal *ri)
  1867. {
  1868. if (ri->data.id != 0)
  1869. return ri->data.id;
  1870. for (u32 next_id = 1; next_id < ~0; ++next_id) {
  1871. if (!vcap_rule_exists(ri->vctrl, next_id)) {
  1872. ri->data.id = next_id;
  1873. break;
  1874. }
  1875. }
  1876. return ri->data.id;
  1877. }
  1878. static int vcap_insert_rule(struct vcap_rule_internal *ri,
  1879. struct vcap_rule_move *move)
  1880. {
  1881. int sw_count = ri->vctrl->vcaps[ri->admin->vtype].sw_count;
  1882. struct vcap_rule_internal *duprule, *iter, *elem = NULL;
  1883. struct vcap_admin *admin = ri->admin;
  1884. u32 addr;
  1885. ri->sort_key = vcap_sort_key(sw_count, ri->size, ri->data.user,
  1886. ri->data.priority);
  1887. /* Insert the new rule in the list of rule based on the sort key
  1888. * If the rule needs to be inserted between existing rules then move
  1889. * these rules to make room for the new rule and update their start
  1890. * address.
  1891. */
  1892. list_for_each_entry(iter, &admin->rules, list) {
  1893. if (ri->sort_key < iter->sort_key) {
  1894. elem = iter;
  1895. break;
  1896. }
  1897. }
  1898. if (!elem) {
  1899. ri->addr = vcap_next_rule_addr(admin->last_used_addr, ri);
  1900. admin->last_used_addr = ri->addr;
  1901. /* Add a copy of the rule to the VCAP list */
  1902. duprule = vcap_dup_rule(ri, ri->state == VCAP_RS_DISABLED);
  1903. if (IS_ERR(duprule))
  1904. return PTR_ERR(duprule);
  1905. list_add_tail(&duprule->list, &admin->rules);
  1906. return 0;
  1907. }
  1908. /* Reuse the space of the current rule */
  1909. addr = elem->addr + elem->size;
  1910. ri->addr = vcap_next_rule_addr(addr, ri);
  1911. addr = ri->addr;
  1912. /* Add a copy of the rule to the VCAP list */
  1913. duprule = vcap_dup_rule(ri, ri->state == VCAP_RS_DISABLED);
  1914. if (IS_ERR(duprule))
  1915. return PTR_ERR(duprule);
  1916. /* Add before the current entry */
  1917. list_add_tail(&duprule->list, &elem->list);
  1918. /* Update the current rule */
  1919. elem->addr = vcap_next_rule_addr(addr, elem);
  1920. addr = elem->addr;
  1921. /* Update the address in the remaining rules in the list */
  1922. list_for_each_entry_continue(elem, &admin->rules, list) {
  1923. elem->addr = vcap_next_rule_addr(addr, elem);
  1924. addr = elem->addr;
  1925. }
  1926. /* Update the move info */
  1927. move->addr = admin->last_used_addr;
  1928. move->count = ri->addr - addr;
  1929. move->offset = admin->last_used_addr - addr;
  1930. admin->last_used_addr = addr;
  1931. return 0;
  1932. }
  1933. static void vcap_move_rules(struct vcap_rule_internal *ri,
  1934. struct vcap_rule_move *move)
  1935. {
  1936. ri->vctrl->ops->move(ri->ndev, ri->admin, move->addr,
  1937. move->offset, move->count);
  1938. }
  1939. /* Check if the chain is already used to enable a VCAP lookup for this port */
  1940. static bool vcap_is_chain_used(struct vcap_control *vctrl,
  1941. struct net_device *ndev, int src_cid)
  1942. {
  1943. struct vcap_enabled_port *eport;
  1944. struct vcap_admin *admin;
  1945. list_for_each_entry(admin, &vctrl->list, list)
  1946. list_for_each_entry(eport, &admin->enabled, list)
  1947. if (eport->src_cid == src_cid && eport->ndev == ndev)
  1948. return true;
  1949. return false;
  1950. }
  1951. /* Fetch the next chain in the enabled list for the port */
  1952. static int vcap_get_next_chain(struct vcap_control *vctrl,
  1953. struct net_device *ndev,
  1954. int dst_cid)
  1955. {
  1956. struct vcap_enabled_port *eport;
  1957. struct vcap_admin *admin;
  1958. list_for_each_entry(admin, &vctrl->list, list) {
  1959. list_for_each_entry(eport, &admin->enabled, list) {
  1960. if (eport->ndev != ndev)
  1961. continue;
  1962. if (eport->src_cid == dst_cid)
  1963. return eport->dst_cid;
  1964. }
  1965. }
  1966. return 0;
  1967. }
  1968. static bool vcap_path_exist(struct vcap_control *vctrl, struct net_device *ndev,
  1969. int dst_cid)
  1970. {
  1971. int cid = rounddown(dst_cid, VCAP_CID_LOOKUP_SIZE);
  1972. struct vcap_enabled_port *eport = NULL;
  1973. struct vcap_enabled_port *elem;
  1974. struct vcap_admin *admin;
  1975. int tmp;
  1976. if (cid == 0) /* Chain zero is always available */
  1977. return true;
  1978. /* Find first entry that starts from chain 0*/
  1979. list_for_each_entry(admin, &vctrl->list, list) {
  1980. list_for_each_entry(elem, &admin->enabled, list) {
  1981. if (elem->src_cid == 0 && elem->ndev == ndev) {
  1982. eport = elem;
  1983. break;
  1984. }
  1985. }
  1986. if (eport)
  1987. break;
  1988. }
  1989. if (!eport)
  1990. return false;
  1991. tmp = eport->dst_cid;
  1992. while (tmp != cid && tmp != 0)
  1993. tmp = vcap_get_next_chain(vctrl, ndev, tmp);
  1994. return !!tmp;
  1995. }
  1996. /* Internal clients can always store their rules in HW
  1997. * External clients can store their rules if the chain is enabled all
  1998. * the way from chain 0, otherwise the rule will be cached until
  1999. * the chain is enabled.
  2000. */
  2001. static void vcap_rule_set_state(struct vcap_rule_internal *ri)
  2002. {
  2003. if (ri->data.user <= VCAP_USER_QOS)
  2004. ri->state = VCAP_RS_PERMANENT;
  2005. else if (vcap_path_exist(ri->vctrl, ri->ndev, ri->data.vcap_chain_id))
  2006. ri->state = VCAP_RS_ENABLED;
  2007. else
  2008. ri->state = VCAP_RS_DISABLED;
  2009. }
  2010. /* Encode and write a validated rule to the VCAP */
  2011. int vcap_add_rule(struct vcap_rule *rule)
  2012. {
  2013. struct vcap_rule_internal *ri = to_intrule(rule);
  2014. struct vcap_rule_move move = {0};
  2015. struct vcap_counter ctr = {0};
  2016. int ret;
  2017. ret = vcap_api_check(ri->vctrl);
  2018. if (ret)
  2019. return ret;
  2020. /* Insert the new rule in the list of vcap rules */
  2021. mutex_lock(&ri->admin->lock);
  2022. vcap_rule_set_state(ri);
  2023. ret = vcap_insert_rule(ri, &move);
  2024. if (ret < 0) {
  2025. pr_err("%s:%d: could not insert rule in vcap list: %d\n",
  2026. __func__, __LINE__, ret);
  2027. goto out;
  2028. }
  2029. if (move.count > 0)
  2030. vcap_move_rules(ri, &move);
  2031. /* Set the counter to zero */
  2032. ret = vcap_write_counter(ri, &ctr);
  2033. if (ret)
  2034. goto out;
  2035. if (ri->state == VCAP_RS_DISABLED) {
  2036. /* Erase the rule area */
  2037. ri->vctrl->ops->init(ri->ndev, ri->admin, ri->addr, ri->size);
  2038. goto out;
  2039. }
  2040. vcap_erase_cache(ri);
  2041. ret = vcap_encode_rule(ri);
  2042. if (ret) {
  2043. pr_err("%s:%d: rule encoding error: %d\n", __func__, __LINE__, ret);
  2044. goto out;
  2045. }
  2046. ret = vcap_write_rule(ri);
  2047. if (ret) {
  2048. pr_err("%s:%d: rule write error: %d\n", __func__, __LINE__, ret);
  2049. goto out;
  2050. }
  2051. out:
  2052. mutex_unlock(&ri->admin->lock);
  2053. return ret;
  2054. }
  2055. EXPORT_SYMBOL_GPL(vcap_add_rule);
  2056. /* Allocate a new rule with the provided arguments */
  2057. struct vcap_rule *vcap_alloc_rule(struct vcap_control *vctrl,
  2058. struct net_device *ndev, int vcap_chain_id,
  2059. enum vcap_user user, u16 priority,
  2060. u32 id)
  2061. {
  2062. struct vcap_rule_internal *ri;
  2063. struct vcap_admin *admin;
  2064. int err, maxsize;
  2065. err = vcap_api_check(vctrl);
  2066. if (err)
  2067. return ERR_PTR(err);
  2068. if (!ndev)
  2069. return ERR_PTR(-ENODEV);
  2070. /* Get the VCAP instance */
  2071. admin = vcap_find_admin(vctrl, vcap_chain_id);
  2072. if (!admin)
  2073. return ERR_PTR(-ENOENT);
  2074. /* Sanity check that this VCAP is supported on this platform */
  2075. if (vctrl->vcaps[admin->vtype].rows == 0)
  2076. return ERR_PTR(-EINVAL);
  2077. mutex_lock(&admin->lock);
  2078. /* Check if a rule with this id already exists */
  2079. if (vcap_rule_exists(vctrl, id)) {
  2080. err = -EINVAL;
  2081. goto out_unlock;
  2082. }
  2083. /* Check if there is room for the rule in the block(s) of the VCAP */
  2084. maxsize = vctrl->vcaps[admin->vtype].sw_count; /* worst case rule size */
  2085. if (vcap_rule_space(admin, maxsize)) {
  2086. err = -ENOSPC;
  2087. goto out_unlock;
  2088. }
  2089. /* Create a container for the rule and return it */
  2090. ri = kzalloc(sizeof(*ri), GFP_KERNEL);
  2091. if (!ri) {
  2092. err = -ENOMEM;
  2093. goto out_unlock;
  2094. }
  2095. ri->data.vcap_chain_id = vcap_chain_id;
  2096. ri->data.user = user;
  2097. ri->data.priority = priority;
  2098. ri->data.id = id;
  2099. ri->data.keyset = VCAP_KFS_NO_VALUE;
  2100. ri->data.actionset = VCAP_AFS_NO_VALUE;
  2101. INIT_LIST_HEAD(&ri->list);
  2102. INIT_LIST_HEAD(&ri->data.keyfields);
  2103. INIT_LIST_HEAD(&ri->data.actionfields);
  2104. ri->ndev = ndev;
  2105. ri->admin = admin; /* refer to the vcap instance */
  2106. ri->vctrl = vctrl; /* refer to the client */
  2107. if (vcap_set_rule_id(ri) == 0) {
  2108. err = -EINVAL;
  2109. goto out_free;
  2110. }
  2111. mutex_unlock(&admin->lock);
  2112. return (struct vcap_rule *)ri;
  2113. out_free:
  2114. kfree(ri);
  2115. out_unlock:
  2116. mutex_unlock(&admin->lock);
  2117. return ERR_PTR(err);
  2118. }
  2119. EXPORT_SYMBOL_GPL(vcap_alloc_rule);
  2120. /* Free mem of a rule owned by client after the rule as been added to the VCAP */
  2121. void vcap_free_rule(struct vcap_rule *rule)
  2122. {
  2123. struct vcap_rule_internal *ri = to_intrule(rule);
  2124. struct vcap_client_actionfield *caf, *next_caf;
  2125. struct vcap_client_keyfield *ckf, *next_ckf;
  2126. /* Deallocate the list of keys and actions */
  2127. list_for_each_entry_safe(ckf, next_ckf, &ri->data.keyfields, ctrl.list) {
  2128. list_del(&ckf->ctrl.list);
  2129. kfree(ckf);
  2130. }
  2131. list_for_each_entry_safe(caf, next_caf, &ri->data.actionfields, ctrl.list) {
  2132. list_del(&caf->ctrl.list);
  2133. kfree(caf);
  2134. }
  2135. /* Deallocate the rule */
  2136. kfree(rule);
  2137. }
  2138. EXPORT_SYMBOL_GPL(vcap_free_rule);
  2139. /* Decode a rule from the VCAP cache and return a copy */
  2140. struct vcap_rule *vcap_decode_rule(struct vcap_rule_internal *elem)
  2141. {
  2142. struct vcap_rule_internal *ri;
  2143. int err;
  2144. ri = vcap_dup_rule(elem, elem->state == VCAP_RS_DISABLED);
  2145. if (IS_ERR(ri))
  2146. return ERR_CAST(ri);
  2147. if (ri->state == VCAP_RS_DISABLED)
  2148. goto out;
  2149. err = vcap_read_rule(ri);
  2150. if (err)
  2151. return ERR_PTR(err);
  2152. err = vcap_decode_keyset(ri);
  2153. if (err)
  2154. return ERR_PTR(err);
  2155. err = vcap_decode_actionset(ri);
  2156. if (err)
  2157. return ERR_PTR(err);
  2158. out:
  2159. return &ri->data;
  2160. }
  2161. struct vcap_rule *vcap_get_rule(struct vcap_control *vctrl, u32 id)
  2162. {
  2163. struct vcap_rule_internal *elem;
  2164. struct vcap_rule *rule;
  2165. int err;
  2166. err = vcap_api_check(vctrl);
  2167. if (err)
  2168. return ERR_PTR(err);
  2169. elem = vcap_get_locked_rule(vctrl, id);
  2170. if (!elem)
  2171. return ERR_PTR(-ENOENT);
  2172. rule = vcap_decode_rule(elem);
  2173. mutex_unlock(&elem->admin->lock);
  2174. return rule;
  2175. }
  2176. EXPORT_SYMBOL_GPL(vcap_get_rule);
  2177. /* Update existing rule */
  2178. int vcap_mod_rule(struct vcap_rule *rule)
  2179. {
  2180. struct vcap_rule_internal *ri = to_intrule(rule);
  2181. struct vcap_counter ctr;
  2182. int err;
  2183. err = vcap_api_check(ri->vctrl);
  2184. if (err)
  2185. return err;
  2186. if (!vcap_get_locked_rule(ri->vctrl, ri->data.id))
  2187. return -ENOENT;
  2188. vcap_rule_set_state(ri);
  2189. if (ri->state == VCAP_RS_DISABLED)
  2190. goto out;
  2191. /* Encode the bitstreams to the VCAP cache */
  2192. vcap_erase_cache(ri);
  2193. err = vcap_encode_rule(ri);
  2194. if (err)
  2195. goto out;
  2196. err = vcap_write_rule(ri);
  2197. if (err)
  2198. goto out;
  2199. memset(&ctr, 0, sizeof(ctr));
  2200. err = vcap_write_counter(ri, &ctr);
  2201. out:
  2202. mutex_unlock(&ri->admin->lock);
  2203. return err;
  2204. }
  2205. EXPORT_SYMBOL_GPL(vcap_mod_rule);
  2206. /* Return the alignment offset for a new rule address */
  2207. static int vcap_valid_rule_move(struct vcap_rule_internal *el, int offset)
  2208. {
  2209. return (el->addr + offset) % el->size;
  2210. }
  2211. /* Update the rule address with an offset */
  2212. static void vcap_adjust_rule_addr(struct vcap_rule_internal *el, int offset)
  2213. {
  2214. el->addr += offset;
  2215. }
  2216. /* Rules needs to be moved to fill the gap of the deleted rule */
  2217. static int vcap_fill_rule_gap(struct vcap_rule_internal *ri)
  2218. {
  2219. struct vcap_admin *admin = ri->admin;
  2220. struct vcap_rule_internal *elem;
  2221. struct vcap_rule_move move;
  2222. int gap = 0, offset = 0;
  2223. /* If the first rule is deleted: Move other rules to the top */
  2224. if (list_is_first(&ri->list, &admin->rules))
  2225. offset = admin->last_valid_addr + 1 - ri->addr - ri->size;
  2226. /* Locate gaps between odd size rules and adjust the move */
  2227. elem = ri;
  2228. list_for_each_entry_continue(elem, &admin->rules, list)
  2229. gap += vcap_valid_rule_move(elem, ri->size);
  2230. /* Update the address in the remaining rules in the list */
  2231. elem = ri;
  2232. list_for_each_entry_continue(elem, &admin->rules, list)
  2233. vcap_adjust_rule_addr(elem, ri->size + gap + offset);
  2234. /* Update the move info */
  2235. move.addr = admin->last_used_addr;
  2236. move.count = ri->addr - admin->last_used_addr - gap;
  2237. move.offset = -(ri->size + gap + offset);
  2238. /* Do the actual move operation */
  2239. vcap_move_rules(ri, &move);
  2240. return gap + offset;
  2241. }
  2242. /* Delete rule in a VCAP instance */
  2243. int vcap_del_rule(struct vcap_control *vctrl, struct net_device *ndev, u32 id)
  2244. {
  2245. struct vcap_rule_internal *ri, *elem;
  2246. struct vcap_admin *admin;
  2247. int gap = 0, err;
  2248. /* This will later also handle rule moving */
  2249. if (!ndev)
  2250. return -ENODEV;
  2251. err = vcap_api_check(vctrl);
  2252. if (err)
  2253. return err;
  2254. /* Look for the rule id in all vcaps */
  2255. ri = vcap_get_locked_rule(vctrl, id);
  2256. if (!ri)
  2257. return -ENOENT;
  2258. admin = ri->admin;
  2259. if (ri->addr > admin->last_used_addr)
  2260. gap = vcap_fill_rule_gap(ri);
  2261. /* Delete the rule from the list of rules and the cache */
  2262. list_del(&ri->list);
  2263. vctrl->ops->init(ndev, admin, admin->last_used_addr, ri->size + gap);
  2264. vcap_free_rule(&ri->data);
  2265. /* Update the last used address, set to default when no rules */
  2266. if (list_empty(&admin->rules)) {
  2267. admin->last_used_addr = admin->last_valid_addr + 1;
  2268. } else {
  2269. elem = list_last_entry(&admin->rules, struct vcap_rule_internal,
  2270. list);
  2271. admin->last_used_addr = elem->addr;
  2272. }
  2273. mutex_unlock(&admin->lock);
  2274. return err;
  2275. }
  2276. EXPORT_SYMBOL_GPL(vcap_del_rule);
  2277. /* Delete all rules in the VCAP instance */
  2278. int vcap_del_rules(struct vcap_control *vctrl, struct vcap_admin *admin)
  2279. {
  2280. struct vcap_enabled_port *eport, *next_eport;
  2281. struct vcap_rule_internal *ri, *next_ri;
  2282. int ret = vcap_api_check(vctrl);
  2283. if (ret)
  2284. return ret;
  2285. mutex_lock(&admin->lock);
  2286. list_for_each_entry_safe(ri, next_ri, &admin->rules, list) {
  2287. vctrl->ops->init(ri->ndev, admin, ri->addr, ri->size);
  2288. list_del(&ri->list);
  2289. vcap_free_rule(&ri->data);
  2290. }
  2291. admin->last_used_addr = admin->last_valid_addr;
  2292. /* Remove list of enabled ports */
  2293. list_for_each_entry_safe(eport, next_eport, &admin->enabled, list) {
  2294. list_del(&eport->list);
  2295. kfree(eport);
  2296. }
  2297. mutex_unlock(&admin->lock);
  2298. return 0;
  2299. }
  2300. EXPORT_SYMBOL_GPL(vcap_del_rules);
  2301. /* Find a client key field in a rule */
  2302. static struct vcap_client_keyfield *
  2303. vcap_find_keyfield(struct vcap_rule *rule, enum vcap_key_field key)
  2304. {
  2305. struct vcap_rule_internal *ri = to_intrule(rule);
  2306. struct vcap_client_keyfield *ckf;
  2307. list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
  2308. if (ckf->ctrl.key == key)
  2309. return ckf;
  2310. return NULL;
  2311. }
  2312. /* Find information on a key field in a rule */
  2313. const struct vcap_field *vcap_lookup_keyfield(struct vcap_rule *rule,
  2314. enum vcap_key_field key)
  2315. {
  2316. struct vcap_rule_internal *ri = to_intrule(rule);
  2317. enum vcap_keyfield_set keyset = rule->keyset;
  2318. enum vcap_type vt = ri->admin->vtype;
  2319. const struct vcap_field *fields;
  2320. if (keyset == VCAP_KFS_NO_VALUE)
  2321. return NULL;
  2322. fields = vcap_keyfields(ri->vctrl, vt, keyset);
  2323. if (!fields)
  2324. return NULL;
  2325. return &fields[key];
  2326. }
  2327. EXPORT_SYMBOL_GPL(vcap_lookup_keyfield);
  2328. /* Check if the keyfield is already in the rule */
  2329. static bool vcap_keyfield_unique(struct vcap_rule *rule,
  2330. enum vcap_key_field key)
  2331. {
  2332. struct vcap_rule_internal *ri = to_intrule(rule);
  2333. const struct vcap_client_keyfield *ckf;
  2334. list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
  2335. if (ckf->ctrl.key == key)
  2336. return false;
  2337. return true;
  2338. }
  2339. /* Check if the keyfield is in the keyset */
  2340. static bool vcap_keyfield_match_keyset(struct vcap_rule *rule,
  2341. enum vcap_key_field key)
  2342. {
  2343. struct vcap_rule_internal *ri = to_intrule(rule);
  2344. enum vcap_keyfield_set keyset = rule->keyset;
  2345. enum vcap_type vt = ri->admin->vtype;
  2346. const struct vcap_field *fields;
  2347. /* the field is accepted if the rule has no keyset yet */
  2348. if (keyset == VCAP_KFS_NO_VALUE)
  2349. return true;
  2350. fields = vcap_keyfields(ri->vctrl, vt, keyset);
  2351. if (!fields)
  2352. return false;
  2353. /* if there is a width there is a way */
  2354. return fields[key].width > 0;
  2355. }
  2356. static int vcap_rule_add_key(struct vcap_rule *rule,
  2357. enum vcap_key_field key,
  2358. enum vcap_field_type ftype,
  2359. struct vcap_client_keyfield_data *data)
  2360. {
  2361. struct vcap_rule_internal *ri = to_intrule(rule);
  2362. struct vcap_client_keyfield *field;
  2363. if (!vcap_keyfield_unique(rule, key)) {
  2364. pr_warn("%s:%d: keyfield %s is already in the rule\n",
  2365. __func__, __LINE__,
  2366. vcap_keyfield_name(ri->vctrl, key));
  2367. return -EINVAL;
  2368. }
  2369. if (!vcap_keyfield_match_keyset(rule, key)) {
  2370. pr_err("%s:%d: keyfield %s does not belong in the rule keyset\n",
  2371. __func__, __LINE__,
  2372. vcap_keyfield_name(ri->vctrl, key));
  2373. return -EINVAL;
  2374. }
  2375. field = kzalloc(sizeof(*field), GFP_KERNEL);
  2376. if (!field)
  2377. return -ENOMEM;
  2378. memcpy(&field->data, data, sizeof(field->data));
  2379. field->ctrl.key = key;
  2380. field->ctrl.type = ftype;
  2381. list_add_tail(&field->ctrl.list, &rule->keyfields);
  2382. return 0;
  2383. }
  2384. static void vcap_rule_set_key_bitsize(struct vcap_u1_key *u1, enum vcap_bit val)
  2385. {
  2386. switch (val) {
  2387. case VCAP_BIT_0:
  2388. u1->value = 0;
  2389. u1->mask = 1;
  2390. break;
  2391. case VCAP_BIT_1:
  2392. u1->value = 1;
  2393. u1->mask = 1;
  2394. break;
  2395. case VCAP_BIT_ANY:
  2396. u1->value = 0;
  2397. u1->mask = 0;
  2398. break;
  2399. }
  2400. }
  2401. /* Add a bit key with value and mask to the rule */
  2402. int vcap_rule_add_key_bit(struct vcap_rule *rule, enum vcap_key_field key,
  2403. enum vcap_bit val)
  2404. {
  2405. struct vcap_client_keyfield_data data;
  2406. vcap_rule_set_key_bitsize(&data.u1, val);
  2407. return vcap_rule_add_key(rule, key, VCAP_FIELD_BIT, &data);
  2408. }
  2409. EXPORT_SYMBOL_GPL(vcap_rule_add_key_bit);
  2410. /* Add a 32 bit key field with value and mask to the rule */
  2411. int vcap_rule_add_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
  2412. u32 value, u32 mask)
  2413. {
  2414. struct vcap_client_keyfield_data data;
  2415. data.u32.value = value;
  2416. data.u32.mask = mask;
  2417. return vcap_rule_add_key(rule, key, VCAP_FIELD_U32, &data);
  2418. }
  2419. EXPORT_SYMBOL_GPL(vcap_rule_add_key_u32);
  2420. /* Add a 48 bit key with value and mask to the rule */
  2421. int vcap_rule_add_key_u48(struct vcap_rule *rule, enum vcap_key_field key,
  2422. struct vcap_u48_key *fieldval)
  2423. {
  2424. struct vcap_client_keyfield_data data;
  2425. memcpy(&data.u48, fieldval, sizeof(data.u48));
  2426. return vcap_rule_add_key(rule, key, VCAP_FIELD_U48, &data);
  2427. }
  2428. EXPORT_SYMBOL_GPL(vcap_rule_add_key_u48);
  2429. /* Add a 72 bit key with value and mask to the rule */
  2430. int vcap_rule_add_key_u72(struct vcap_rule *rule, enum vcap_key_field key,
  2431. struct vcap_u72_key *fieldval)
  2432. {
  2433. struct vcap_client_keyfield_data data;
  2434. memcpy(&data.u72, fieldval, sizeof(data.u72));
  2435. return vcap_rule_add_key(rule, key, VCAP_FIELD_U72, &data);
  2436. }
  2437. EXPORT_SYMBOL_GPL(vcap_rule_add_key_u72);
  2438. /* Add a 128 bit key with value and mask to the rule */
  2439. int vcap_rule_add_key_u128(struct vcap_rule *rule, enum vcap_key_field key,
  2440. struct vcap_u128_key *fieldval)
  2441. {
  2442. struct vcap_client_keyfield_data data;
  2443. memcpy(&data.u128, fieldval, sizeof(data.u128));
  2444. return vcap_rule_add_key(rule, key, VCAP_FIELD_U128, &data);
  2445. }
  2446. EXPORT_SYMBOL_GPL(vcap_rule_add_key_u128);
  2447. int vcap_rule_get_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
  2448. u32 *value, u32 *mask)
  2449. {
  2450. struct vcap_client_keyfield *ckf;
  2451. ckf = vcap_find_keyfield(rule, key);
  2452. if (!ckf)
  2453. return -ENOENT;
  2454. *value = ckf->data.u32.value;
  2455. *mask = ckf->data.u32.mask;
  2456. return 0;
  2457. }
  2458. EXPORT_SYMBOL_GPL(vcap_rule_get_key_u32);
  2459. /* Find a client action field in a rule */
  2460. struct vcap_client_actionfield *
  2461. vcap_find_actionfield(struct vcap_rule *rule, enum vcap_action_field act)
  2462. {
  2463. struct vcap_rule_internal *ri = (struct vcap_rule_internal *)rule;
  2464. struct vcap_client_actionfield *caf;
  2465. list_for_each_entry(caf, &ri->data.actionfields, ctrl.list)
  2466. if (caf->ctrl.action == act)
  2467. return caf;
  2468. return NULL;
  2469. }
  2470. EXPORT_SYMBOL_GPL(vcap_find_actionfield);
  2471. /* Check if the actionfield is already in the rule */
  2472. static bool vcap_actionfield_unique(struct vcap_rule *rule,
  2473. enum vcap_action_field act)
  2474. {
  2475. struct vcap_rule_internal *ri = to_intrule(rule);
  2476. const struct vcap_client_actionfield *caf;
  2477. list_for_each_entry(caf, &ri->data.actionfields, ctrl.list)
  2478. if (caf->ctrl.action == act)
  2479. return false;
  2480. return true;
  2481. }
  2482. /* Check if the actionfield is in the actionset */
  2483. static bool vcap_actionfield_match_actionset(struct vcap_rule *rule,
  2484. enum vcap_action_field action)
  2485. {
  2486. enum vcap_actionfield_set actionset = rule->actionset;
  2487. struct vcap_rule_internal *ri = to_intrule(rule);
  2488. enum vcap_type vt = ri->admin->vtype;
  2489. const struct vcap_field *fields;
  2490. /* the field is accepted if the rule has no actionset yet */
  2491. if (actionset == VCAP_AFS_NO_VALUE)
  2492. return true;
  2493. fields = vcap_actionfields(ri->vctrl, vt, actionset);
  2494. if (!fields)
  2495. return false;
  2496. /* if there is a width there is a way */
  2497. return fields[action].width > 0;
  2498. }
  2499. static int vcap_rule_add_action(struct vcap_rule *rule,
  2500. enum vcap_action_field action,
  2501. enum vcap_field_type ftype,
  2502. struct vcap_client_actionfield_data *data)
  2503. {
  2504. struct vcap_rule_internal *ri = to_intrule(rule);
  2505. struct vcap_client_actionfield *field;
  2506. if (!vcap_actionfield_unique(rule, action)) {
  2507. pr_warn("%s:%d: actionfield %s is already in the rule\n",
  2508. __func__, __LINE__,
  2509. vcap_actionfield_name(ri->vctrl, action));
  2510. return -EINVAL;
  2511. }
  2512. if (!vcap_actionfield_match_actionset(rule, action)) {
  2513. pr_err("%s:%d: actionfield %s does not belong in the rule actionset\n",
  2514. __func__, __LINE__,
  2515. vcap_actionfield_name(ri->vctrl, action));
  2516. return -EINVAL;
  2517. }
  2518. field = kzalloc(sizeof(*field), GFP_KERNEL);
  2519. if (!field)
  2520. return -ENOMEM;
  2521. memcpy(&field->data, data, sizeof(field->data));
  2522. field->ctrl.action = action;
  2523. field->ctrl.type = ftype;
  2524. list_add_tail(&field->ctrl.list, &rule->actionfields);
  2525. return 0;
  2526. }
  2527. static void vcap_rule_set_action_bitsize(struct vcap_u1_action *u1,
  2528. enum vcap_bit val)
  2529. {
  2530. switch (val) {
  2531. case VCAP_BIT_0:
  2532. u1->value = 0;
  2533. break;
  2534. case VCAP_BIT_1:
  2535. u1->value = 1;
  2536. break;
  2537. case VCAP_BIT_ANY:
  2538. u1->value = 0;
  2539. break;
  2540. }
  2541. }
  2542. /* Add a bit action with value to the rule */
  2543. int vcap_rule_add_action_bit(struct vcap_rule *rule,
  2544. enum vcap_action_field action,
  2545. enum vcap_bit val)
  2546. {
  2547. struct vcap_client_actionfield_data data;
  2548. vcap_rule_set_action_bitsize(&data.u1, val);
  2549. return vcap_rule_add_action(rule, action, VCAP_FIELD_BIT, &data);
  2550. }
  2551. EXPORT_SYMBOL_GPL(vcap_rule_add_action_bit);
  2552. /* Add a 32 bit action field with value to the rule */
  2553. int vcap_rule_add_action_u32(struct vcap_rule *rule,
  2554. enum vcap_action_field action,
  2555. u32 value)
  2556. {
  2557. struct vcap_client_actionfield_data data;
  2558. data.u32.value = value;
  2559. return vcap_rule_add_action(rule, action, VCAP_FIELD_U32, &data);
  2560. }
  2561. EXPORT_SYMBOL_GPL(vcap_rule_add_action_u32);
  2562. /* Add a 72 bit action field with value to the rule */
  2563. int vcap_rule_add_action_u72(struct vcap_rule *rule,
  2564. enum vcap_action_field action,
  2565. struct vcap_u72_action *fieldval)
  2566. {
  2567. struct vcap_client_actionfield_data data;
  2568. memcpy(&data.u72, fieldval, sizeof(data.u72));
  2569. return vcap_rule_add_action(rule, action, VCAP_FIELD_U72, &data);
  2570. }
  2571. EXPORT_SYMBOL_GPL(vcap_rule_add_action_u72);
  2572. static int vcap_read_counter(struct vcap_rule_internal *ri,
  2573. struct vcap_counter *ctr)
  2574. {
  2575. struct vcap_admin *admin = ri->admin;
  2576. ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ, VCAP_SEL_COUNTER,
  2577. ri->addr);
  2578. ri->vctrl->ops->cache_read(ri->ndev, admin, VCAP_SEL_COUNTER,
  2579. ri->counter_id, 0);
  2580. ctr->value = admin->cache.counter;
  2581. ctr->sticky = admin->cache.sticky;
  2582. return 0;
  2583. }
  2584. /* Copy to host byte order */
  2585. void vcap_netbytes_copy(u8 *dst, u8 *src, int count)
  2586. {
  2587. int idx;
  2588. for (idx = 0; idx < count; ++idx, ++dst)
  2589. *dst = src[count - idx - 1];
  2590. }
  2591. EXPORT_SYMBOL_GPL(vcap_netbytes_copy);
  2592. /* Convert validation error code into tc extack error message */
  2593. void vcap_set_tc_exterr(struct flow_cls_offload *fco, struct vcap_rule *vrule)
  2594. {
  2595. switch (vrule->exterr) {
  2596. case VCAP_ERR_NONE:
  2597. break;
  2598. case VCAP_ERR_NO_ADMIN:
  2599. NL_SET_ERR_MSG_MOD(fco->common.extack,
  2600. "Missing VCAP instance");
  2601. break;
  2602. case VCAP_ERR_NO_NETDEV:
  2603. NL_SET_ERR_MSG_MOD(fco->common.extack,
  2604. "Missing network interface");
  2605. break;
  2606. case VCAP_ERR_NO_KEYSET_MATCH:
  2607. NL_SET_ERR_MSG_MOD(fco->common.extack,
  2608. "No keyset matched the filter keys");
  2609. break;
  2610. case VCAP_ERR_NO_ACTIONSET_MATCH:
  2611. NL_SET_ERR_MSG_MOD(fco->common.extack,
  2612. "No actionset matched the filter actions");
  2613. break;
  2614. case VCAP_ERR_NO_PORT_KEYSET_MATCH:
  2615. NL_SET_ERR_MSG_MOD(fco->common.extack,
  2616. "No port keyset matched the filter keys");
  2617. break;
  2618. }
  2619. }
  2620. EXPORT_SYMBOL_GPL(vcap_set_tc_exterr);
  2621. /* Write a rule to VCAP HW to enable it */
  2622. static int vcap_enable_rule(struct vcap_rule_internal *ri)
  2623. {
  2624. struct vcap_client_actionfield *af, *naf;
  2625. struct vcap_client_keyfield *kf, *nkf;
  2626. int err;
  2627. vcap_erase_cache(ri);
  2628. err = vcap_encode_rule(ri);
  2629. if (err)
  2630. goto out;
  2631. err = vcap_write_rule(ri);
  2632. if (err)
  2633. goto out;
  2634. /* Deallocate the list of keys and actions */
  2635. list_for_each_entry_safe(kf, nkf, &ri->data.keyfields, ctrl.list) {
  2636. list_del(&kf->ctrl.list);
  2637. kfree(kf);
  2638. }
  2639. list_for_each_entry_safe(af, naf, &ri->data.actionfields, ctrl.list) {
  2640. list_del(&af->ctrl.list);
  2641. kfree(af);
  2642. }
  2643. ri->state = VCAP_RS_ENABLED;
  2644. out:
  2645. return err;
  2646. }
  2647. /* Enable all disabled rules for a specific chain/port in the VCAP HW */
  2648. static int vcap_enable_rules(struct vcap_control *vctrl,
  2649. struct net_device *ndev, int chain)
  2650. {
  2651. int next_chain = chain + VCAP_CID_LOOKUP_SIZE;
  2652. struct vcap_rule_internal *ri;
  2653. struct vcap_admin *admin;
  2654. int err = 0;
  2655. list_for_each_entry(admin, &vctrl->list, list) {
  2656. if (!(chain >= admin->first_cid && chain <= admin->last_cid))
  2657. continue;
  2658. /* Found the admin, now find the offloadable rules */
  2659. mutex_lock(&admin->lock);
  2660. list_for_each_entry(ri, &admin->rules, list) {
  2661. /* Is the rule in the lookup defined by the chain */
  2662. if (!(ri->data.vcap_chain_id >= chain &&
  2663. ri->data.vcap_chain_id < next_chain)) {
  2664. continue;
  2665. }
  2666. if (ri->ndev != ndev)
  2667. continue;
  2668. if (ri->state != VCAP_RS_DISABLED)
  2669. continue;
  2670. err = vcap_enable_rule(ri);
  2671. if (err)
  2672. break;
  2673. }
  2674. mutex_unlock(&admin->lock);
  2675. if (err)
  2676. break;
  2677. }
  2678. return err;
  2679. }
  2680. /* Read and erase a rule from VCAP HW to disable it */
  2681. static int vcap_disable_rule(struct vcap_rule_internal *ri)
  2682. {
  2683. int err;
  2684. err = vcap_read_rule(ri);
  2685. if (err)
  2686. return err;
  2687. err = vcap_decode_keyset(ri);
  2688. if (err)
  2689. return err;
  2690. err = vcap_decode_actionset(ri);
  2691. if (err)
  2692. return err;
  2693. ri->state = VCAP_RS_DISABLED;
  2694. ri->vctrl->ops->init(ri->ndev, ri->admin, ri->addr, ri->size);
  2695. return 0;
  2696. }
  2697. /* Disable all enabled rules for a specific chain/port in the VCAP HW */
  2698. static int vcap_disable_rules(struct vcap_control *vctrl,
  2699. struct net_device *ndev, int chain)
  2700. {
  2701. struct vcap_rule_internal *ri;
  2702. struct vcap_admin *admin;
  2703. int err = 0;
  2704. list_for_each_entry(admin, &vctrl->list, list) {
  2705. if (!(chain >= admin->first_cid && chain <= admin->last_cid))
  2706. continue;
  2707. /* Found the admin, now find the rules on the chain */
  2708. mutex_lock(&admin->lock);
  2709. list_for_each_entry(ri, &admin->rules, list) {
  2710. if (ri->data.vcap_chain_id != chain)
  2711. continue;
  2712. if (ri->ndev != ndev)
  2713. continue;
  2714. if (ri->state != VCAP_RS_ENABLED)
  2715. continue;
  2716. err = vcap_disable_rule(ri);
  2717. if (err)
  2718. break;
  2719. }
  2720. mutex_unlock(&admin->lock);
  2721. if (err)
  2722. break;
  2723. }
  2724. return err;
  2725. }
  2726. /* Check if this port is already enabled for this VCAP instance */
  2727. static bool vcap_is_enabled(struct vcap_control *vctrl, struct net_device *ndev,
  2728. int dst_cid)
  2729. {
  2730. struct vcap_enabled_port *eport;
  2731. struct vcap_admin *admin;
  2732. list_for_each_entry(admin, &vctrl->list, list)
  2733. list_for_each_entry(eport, &admin->enabled, list)
  2734. if (eport->dst_cid == dst_cid && eport->ndev == ndev)
  2735. return true;
  2736. return false;
  2737. }
  2738. /* Enable this port and chain id in a VCAP instance */
  2739. static int vcap_enable(struct vcap_control *vctrl, struct net_device *ndev,
  2740. unsigned long cookie, int src_cid, int dst_cid)
  2741. {
  2742. struct vcap_enabled_port *eport;
  2743. struct vcap_admin *admin;
  2744. if (src_cid >= dst_cid)
  2745. return -EFAULT;
  2746. admin = vcap_find_admin(vctrl, dst_cid);
  2747. if (!admin)
  2748. return -ENOENT;
  2749. eport = kzalloc(sizeof(*eport), GFP_KERNEL);
  2750. if (!eport)
  2751. return -ENOMEM;
  2752. eport->ndev = ndev;
  2753. eport->cookie = cookie;
  2754. eport->src_cid = src_cid;
  2755. eport->dst_cid = dst_cid;
  2756. mutex_lock(&admin->lock);
  2757. list_add_tail(&eport->list, &admin->enabled);
  2758. mutex_unlock(&admin->lock);
  2759. if (vcap_path_exist(vctrl, ndev, src_cid)) {
  2760. /* Enable chained lookups */
  2761. while (dst_cid) {
  2762. admin = vcap_find_admin(vctrl, dst_cid);
  2763. if (!admin)
  2764. return -ENOENT;
  2765. vcap_enable_rules(vctrl, ndev, dst_cid);
  2766. dst_cid = vcap_get_next_chain(vctrl, ndev, dst_cid);
  2767. }
  2768. }
  2769. return 0;
  2770. }
  2771. /* Disable this port and chain id for a VCAP instance */
  2772. static int vcap_disable(struct vcap_control *vctrl, struct net_device *ndev,
  2773. unsigned long cookie)
  2774. {
  2775. struct vcap_enabled_port *elem, *eport = NULL;
  2776. struct vcap_admin *found = NULL, *admin;
  2777. int dst_cid;
  2778. list_for_each_entry(admin, &vctrl->list, list) {
  2779. list_for_each_entry(elem, &admin->enabled, list) {
  2780. if (elem->cookie == cookie && elem->ndev == ndev) {
  2781. eport = elem;
  2782. found = admin;
  2783. break;
  2784. }
  2785. }
  2786. if (eport)
  2787. break;
  2788. }
  2789. if (!eport)
  2790. return -ENOENT;
  2791. /* Disable chained lookups */
  2792. dst_cid = eport->dst_cid;
  2793. while (dst_cid) {
  2794. admin = vcap_find_admin(vctrl, dst_cid);
  2795. if (!admin)
  2796. return -ENOENT;
  2797. vcap_disable_rules(vctrl, ndev, dst_cid);
  2798. dst_cid = vcap_get_next_chain(vctrl, ndev, dst_cid);
  2799. }
  2800. mutex_lock(&found->lock);
  2801. list_del(&eport->list);
  2802. mutex_unlock(&found->lock);
  2803. kfree(eport);
  2804. return 0;
  2805. }
  2806. /* Enable/Disable the VCAP instance lookups */
  2807. int vcap_enable_lookups(struct vcap_control *vctrl, struct net_device *ndev,
  2808. int src_cid, int dst_cid, unsigned long cookie,
  2809. bool enable)
  2810. {
  2811. int err;
  2812. err = vcap_api_check(vctrl);
  2813. if (err)
  2814. return err;
  2815. if (!ndev)
  2816. return -ENODEV;
  2817. /* Source and destination must be the first chain in a lookup */
  2818. if (src_cid % VCAP_CID_LOOKUP_SIZE)
  2819. return -EFAULT;
  2820. if (dst_cid % VCAP_CID_LOOKUP_SIZE)
  2821. return -EFAULT;
  2822. if (enable) {
  2823. if (vcap_is_enabled(vctrl, ndev, dst_cid))
  2824. return -EADDRINUSE;
  2825. if (vcap_is_chain_used(vctrl, ndev, src_cid))
  2826. return -EADDRNOTAVAIL;
  2827. err = vcap_enable(vctrl, ndev, cookie, src_cid, dst_cid);
  2828. } else {
  2829. err = vcap_disable(vctrl, ndev, cookie);
  2830. }
  2831. return err;
  2832. }
  2833. EXPORT_SYMBOL_GPL(vcap_enable_lookups);
  2834. /* Is this chain id the last lookup of all VCAPs */
  2835. bool vcap_is_last_chain(struct vcap_control *vctrl, int cid, bool ingress)
  2836. {
  2837. struct vcap_admin *admin;
  2838. int lookup;
  2839. if (vcap_api_check(vctrl))
  2840. return false;
  2841. admin = vcap_find_admin(vctrl, cid);
  2842. if (!admin)
  2843. return false;
  2844. if (!vcap_admin_is_last(vctrl, admin, ingress))
  2845. return false;
  2846. /* This must be the last lookup in this VCAP type */
  2847. lookup = vcap_chain_id_to_lookup(admin, cid);
  2848. return lookup == admin->lookups - 1;
  2849. }
  2850. EXPORT_SYMBOL_GPL(vcap_is_last_chain);
  2851. /* Set a rule counter id (for certain vcaps only) */
  2852. void vcap_rule_set_counter_id(struct vcap_rule *rule, u32 counter_id)
  2853. {
  2854. struct vcap_rule_internal *ri = to_intrule(rule);
  2855. ri->counter_id = counter_id;
  2856. }
  2857. EXPORT_SYMBOL_GPL(vcap_rule_set_counter_id);
  2858. int vcap_rule_set_counter(struct vcap_rule *rule, struct vcap_counter *ctr)
  2859. {
  2860. struct vcap_rule_internal *ri = to_intrule(rule);
  2861. int err;
  2862. err = vcap_api_check(ri->vctrl);
  2863. if (err)
  2864. return err;
  2865. if (!ctr) {
  2866. pr_err("%s:%d: counter is missing\n", __func__, __LINE__);
  2867. return -EINVAL;
  2868. }
  2869. mutex_lock(&ri->admin->lock);
  2870. err = vcap_write_counter(ri, ctr);
  2871. mutex_unlock(&ri->admin->lock);
  2872. return err;
  2873. }
  2874. EXPORT_SYMBOL_GPL(vcap_rule_set_counter);
  2875. int vcap_rule_get_counter(struct vcap_rule *rule, struct vcap_counter *ctr)
  2876. {
  2877. struct vcap_rule_internal *ri = to_intrule(rule);
  2878. int err;
  2879. err = vcap_api_check(ri->vctrl);
  2880. if (err)
  2881. return err;
  2882. if (!ctr) {
  2883. pr_err("%s:%d: counter is missing\n", __func__, __LINE__);
  2884. return -EINVAL;
  2885. }
  2886. mutex_lock(&ri->admin->lock);
  2887. err = vcap_read_counter(ri, ctr);
  2888. mutex_unlock(&ri->admin->lock);
  2889. return err;
  2890. }
  2891. EXPORT_SYMBOL_GPL(vcap_rule_get_counter);
  2892. /* Get a copy of a client key field */
  2893. static int vcap_rule_get_key(struct vcap_rule *rule,
  2894. enum vcap_key_field key,
  2895. struct vcap_client_keyfield *ckf)
  2896. {
  2897. struct vcap_client_keyfield *field;
  2898. field = vcap_find_keyfield(rule, key);
  2899. if (!field)
  2900. return -EINVAL;
  2901. memcpy(ckf, field, sizeof(*ckf));
  2902. INIT_LIST_HEAD(&ckf->ctrl.list);
  2903. return 0;
  2904. }
  2905. /* Find a keyset having the same size as the provided rule, where the keyset
  2906. * does not have a type id.
  2907. */
  2908. static int vcap_rule_get_untyped_keyset(struct vcap_rule_internal *ri,
  2909. struct vcap_keyset_list *matches)
  2910. {
  2911. struct vcap_control *vctrl = ri->vctrl;
  2912. enum vcap_type vt = ri->admin->vtype;
  2913. const struct vcap_set *keyfield_set;
  2914. int idx;
  2915. keyfield_set = vctrl->vcaps[vt].keyfield_set;
  2916. for (idx = 0; idx < vctrl->vcaps[vt].keyfield_set_size; ++idx) {
  2917. if (keyfield_set[idx].sw_per_item == ri->keyset_sw &&
  2918. keyfield_set[idx].type_id == (u8)-1) {
  2919. vcap_keyset_list_add(matches, idx);
  2920. return 0;
  2921. }
  2922. }
  2923. return -EINVAL;
  2924. }
  2925. /* Get the keysets that matches the rule key type/mask */
  2926. int vcap_rule_get_keysets(struct vcap_rule_internal *ri,
  2927. struct vcap_keyset_list *matches)
  2928. {
  2929. struct vcap_control *vctrl = ri->vctrl;
  2930. enum vcap_type vt = ri->admin->vtype;
  2931. const struct vcap_set *keyfield_set;
  2932. struct vcap_client_keyfield kf = {};
  2933. u32 value, mask;
  2934. int err, idx;
  2935. err = vcap_rule_get_key(&ri->data, VCAP_KF_TYPE, &kf);
  2936. if (err)
  2937. return vcap_rule_get_untyped_keyset(ri, matches);
  2938. if (kf.ctrl.type == VCAP_FIELD_BIT) {
  2939. value = kf.data.u1.value;
  2940. mask = kf.data.u1.mask;
  2941. } else if (kf.ctrl.type == VCAP_FIELD_U32) {
  2942. value = kf.data.u32.value;
  2943. mask = kf.data.u32.mask;
  2944. } else {
  2945. return -EINVAL;
  2946. }
  2947. keyfield_set = vctrl->vcaps[vt].keyfield_set;
  2948. for (idx = 0; idx < vctrl->vcaps[vt].keyfield_set_size; ++idx) {
  2949. if (keyfield_set[idx].sw_per_item != ri->keyset_sw)
  2950. continue;
  2951. if (keyfield_set[idx].type_id == (u8)-1) {
  2952. vcap_keyset_list_add(matches, idx);
  2953. continue;
  2954. }
  2955. if ((keyfield_set[idx].type_id & mask) == value)
  2956. vcap_keyset_list_add(matches, idx);
  2957. }
  2958. if (matches->cnt > 0)
  2959. return 0;
  2960. return -EINVAL;
  2961. }
  2962. /* Collect packet counts from all rules with the same cookie */
  2963. int vcap_get_rule_count_by_cookie(struct vcap_control *vctrl,
  2964. struct vcap_counter *ctr, u64 cookie)
  2965. {
  2966. struct vcap_rule_internal *ri;
  2967. struct vcap_counter temp = {};
  2968. struct vcap_admin *admin;
  2969. int err;
  2970. err = vcap_api_check(vctrl);
  2971. if (err)
  2972. return err;
  2973. /* Iterate all rules in each VCAP instance */
  2974. list_for_each_entry(admin, &vctrl->list, list) {
  2975. mutex_lock(&admin->lock);
  2976. list_for_each_entry(ri, &admin->rules, list) {
  2977. if (ri->data.cookie != cookie)
  2978. continue;
  2979. err = vcap_read_counter(ri, &temp);
  2980. if (err)
  2981. goto unlock;
  2982. ctr->value += temp.value;
  2983. /* Reset the rule counter */
  2984. temp.value = 0;
  2985. temp.sticky = 0;
  2986. err = vcap_write_counter(ri, &temp);
  2987. if (err)
  2988. goto unlock;
  2989. }
  2990. mutex_unlock(&admin->lock);
  2991. }
  2992. return err;
  2993. unlock:
  2994. mutex_unlock(&admin->lock);
  2995. return err;
  2996. }
  2997. EXPORT_SYMBOL_GPL(vcap_get_rule_count_by_cookie);
  2998. static int vcap_rule_mod_key(struct vcap_rule *rule,
  2999. enum vcap_key_field key,
  3000. enum vcap_field_type ftype,
  3001. struct vcap_client_keyfield_data *data)
  3002. {
  3003. struct vcap_client_keyfield *field;
  3004. field = vcap_find_keyfield(rule, key);
  3005. if (!field)
  3006. return vcap_rule_add_key(rule, key, ftype, data);
  3007. memcpy(&field->data, data, sizeof(field->data));
  3008. return 0;
  3009. }
  3010. /* Modify a 32 bit key field with value and mask in the rule */
  3011. int vcap_rule_mod_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
  3012. u32 value, u32 mask)
  3013. {
  3014. struct vcap_client_keyfield_data data;
  3015. data.u32.value = value;
  3016. data.u32.mask = mask;
  3017. return vcap_rule_mod_key(rule, key, VCAP_FIELD_U32, &data);
  3018. }
  3019. EXPORT_SYMBOL_GPL(vcap_rule_mod_key_u32);
  3020. /* Remove a key field with value and mask in the rule */
  3021. int vcap_rule_rem_key(struct vcap_rule *rule, enum vcap_key_field key)
  3022. {
  3023. struct vcap_rule_internal *ri = to_intrule(rule);
  3024. struct vcap_client_keyfield *field;
  3025. field = vcap_find_keyfield(rule, key);
  3026. if (!field) {
  3027. pr_err("%s:%d: key %s is not in the rule\n",
  3028. __func__, __LINE__, vcap_keyfield_name(ri->vctrl, key));
  3029. return -EINVAL;
  3030. }
  3031. /* Deallocate the key field */
  3032. list_del(&field->ctrl.list);
  3033. kfree(field);
  3034. return 0;
  3035. }
  3036. EXPORT_SYMBOL_GPL(vcap_rule_rem_key);
  3037. static int vcap_rule_mod_action(struct vcap_rule *rule,
  3038. enum vcap_action_field action,
  3039. enum vcap_field_type ftype,
  3040. struct vcap_client_actionfield_data *data)
  3041. {
  3042. struct vcap_client_actionfield *field;
  3043. field = vcap_find_actionfield(rule, action);
  3044. if (!field)
  3045. return vcap_rule_add_action(rule, action, ftype, data);
  3046. memcpy(&field->data, data, sizeof(field->data));
  3047. return 0;
  3048. }
  3049. /* Modify a 32 bit action field with value in the rule */
  3050. int vcap_rule_mod_action_u32(struct vcap_rule *rule,
  3051. enum vcap_action_field action,
  3052. u32 value)
  3053. {
  3054. struct vcap_client_actionfield_data data;
  3055. data.u32.value = value;
  3056. return vcap_rule_mod_action(rule, action, VCAP_FIELD_U32, &data);
  3057. }
  3058. EXPORT_SYMBOL_GPL(vcap_rule_mod_action_u32);
  3059. /* Drop keys in a keylist and any keys that are not supported by the keyset */
  3060. int vcap_filter_rule_keys(struct vcap_rule *rule,
  3061. enum vcap_key_field keylist[], int length,
  3062. bool drop_unsupported)
  3063. {
  3064. struct vcap_rule_internal *ri = to_intrule(rule);
  3065. struct vcap_client_keyfield *ckf, *next_ckf;
  3066. const struct vcap_field *fields;
  3067. enum vcap_key_field key;
  3068. int err = 0;
  3069. int idx;
  3070. if (length > 0) {
  3071. err = -EEXIST;
  3072. list_for_each_entry_safe(ckf, next_ckf,
  3073. &ri->data.keyfields, ctrl.list) {
  3074. key = ckf->ctrl.key;
  3075. for (idx = 0; idx < length; ++idx)
  3076. if (key == keylist[idx]) {
  3077. list_del(&ckf->ctrl.list);
  3078. kfree(ckf);
  3079. idx++;
  3080. err = 0;
  3081. }
  3082. }
  3083. }
  3084. if (drop_unsupported) {
  3085. err = -EEXIST;
  3086. fields = vcap_keyfields(ri->vctrl, ri->admin->vtype,
  3087. rule->keyset);
  3088. if (!fields)
  3089. return err;
  3090. list_for_each_entry_safe(ckf, next_ckf,
  3091. &ri->data.keyfields, ctrl.list) {
  3092. key = ckf->ctrl.key;
  3093. if (fields[key].width == 0) {
  3094. list_del(&ckf->ctrl.list);
  3095. kfree(ckf);
  3096. err = 0;
  3097. }
  3098. }
  3099. }
  3100. return err;
  3101. }
  3102. EXPORT_SYMBOL_GPL(vcap_filter_rule_keys);
  3103. /* Select the keyset from the list that results in the smallest rule size */
  3104. enum vcap_keyfield_set
  3105. vcap_select_min_rule_keyset(struct vcap_control *vctrl,
  3106. enum vcap_type vtype,
  3107. struct vcap_keyset_list *kslist)
  3108. {
  3109. enum vcap_keyfield_set ret = VCAP_KFS_NO_VALUE;
  3110. const struct vcap_set *kset;
  3111. int max = 100, idx;
  3112. for (idx = 0; idx < kslist->cnt; ++idx) {
  3113. kset = vcap_keyfieldset(vctrl, vtype, kslist->keysets[idx]);
  3114. if (!kset)
  3115. continue;
  3116. if (kset->sw_per_item >= max)
  3117. continue;
  3118. max = kset->sw_per_item;
  3119. ret = kslist->keysets[idx];
  3120. }
  3121. return ret;
  3122. }
  3123. EXPORT_SYMBOL_GPL(vcap_select_min_rule_keyset);
  3124. /* Make a full copy of an existing rule with a new rule id */
  3125. struct vcap_rule *vcap_copy_rule(struct vcap_rule *erule)
  3126. {
  3127. struct vcap_rule_internal *ri = to_intrule(erule);
  3128. struct vcap_client_actionfield *caf;
  3129. struct vcap_client_keyfield *ckf;
  3130. struct vcap_rule *rule;
  3131. int err;
  3132. err = vcap_api_check(ri->vctrl);
  3133. if (err)
  3134. return ERR_PTR(err);
  3135. rule = vcap_alloc_rule(ri->vctrl, ri->ndev, ri->data.vcap_chain_id,
  3136. ri->data.user, ri->data.priority, 0);
  3137. if (IS_ERR(rule))
  3138. return rule;
  3139. list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
  3140. /* Add a key duplicate in the new rule */
  3141. err = vcap_rule_add_key(rule,
  3142. ckf->ctrl.key,
  3143. ckf->ctrl.type,
  3144. &ckf->data);
  3145. if (err)
  3146. goto err;
  3147. }
  3148. list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
  3149. /* Add a action duplicate in the new rule */
  3150. err = vcap_rule_add_action(rule,
  3151. caf->ctrl.action,
  3152. caf->ctrl.type,
  3153. &caf->data);
  3154. if (err)
  3155. goto err;
  3156. }
  3157. return rule;
  3158. err:
  3159. vcap_free_rule(rule);
  3160. return ERR_PTR(err);
  3161. }
  3162. EXPORT_SYMBOL_GPL(vcap_copy_rule);
  3163. #ifdef CONFIG_VCAP_KUNIT_TEST
  3164. #include "vcap_api_kunit.c"
  3165. #endif