rocker_ofdpa.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958
  1. /*
  2. * drivers/net/ethernet/rocker/rocker_ofdpa.c - Rocker switch OF-DPA-like
  3. * implementation
  4. * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com>
  5. * Copyright (c) 2014-2016 Jiri Pirko <jiri@mellanox.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/types.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/hashtable.h>
  16. #include <linux/crc32.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/inetdevice.h>
  19. #include <linux/if_vlan.h>
  20. #include <linux/if_bridge.h>
  21. #include <net/neighbour.h>
  22. #include <net/switchdev.h>
  23. #include <net/ip_fib.h>
  24. #include <net/arp.h>
  25. #include "rocker.h"
  26. #include "rocker_tlv.h"
  27. struct ofdpa_flow_tbl_key {
  28. u32 priority;
  29. enum rocker_of_dpa_table_id tbl_id;
  30. union {
  31. struct {
  32. u32 in_pport;
  33. u32 in_pport_mask;
  34. enum rocker_of_dpa_table_id goto_tbl;
  35. } ig_port;
  36. struct {
  37. u32 in_pport;
  38. __be16 vlan_id;
  39. __be16 vlan_id_mask;
  40. enum rocker_of_dpa_table_id goto_tbl;
  41. bool untagged;
  42. __be16 new_vlan_id;
  43. } vlan;
  44. struct {
  45. u32 in_pport;
  46. u32 in_pport_mask;
  47. __be16 eth_type;
  48. u8 eth_dst[ETH_ALEN];
  49. u8 eth_dst_mask[ETH_ALEN];
  50. __be16 vlan_id;
  51. __be16 vlan_id_mask;
  52. enum rocker_of_dpa_table_id goto_tbl;
  53. bool copy_to_cpu;
  54. } term_mac;
  55. struct {
  56. __be16 eth_type;
  57. __be32 dst4;
  58. __be32 dst4_mask;
  59. enum rocker_of_dpa_table_id goto_tbl;
  60. u32 group_id;
  61. } ucast_routing;
  62. struct {
  63. u8 eth_dst[ETH_ALEN];
  64. u8 eth_dst_mask[ETH_ALEN];
  65. int has_eth_dst;
  66. int has_eth_dst_mask;
  67. __be16 vlan_id;
  68. u32 tunnel_id;
  69. enum rocker_of_dpa_table_id goto_tbl;
  70. u32 group_id;
  71. bool copy_to_cpu;
  72. } bridge;
  73. struct {
  74. u32 in_pport;
  75. u32 in_pport_mask;
  76. u8 eth_src[ETH_ALEN];
  77. u8 eth_src_mask[ETH_ALEN];
  78. u8 eth_dst[ETH_ALEN];
  79. u8 eth_dst_mask[ETH_ALEN];
  80. __be16 eth_type;
  81. __be16 vlan_id;
  82. __be16 vlan_id_mask;
  83. u8 ip_proto;
  84. u8 ip_proto_mask;
  85. u8 ip_tos;
  86. u8 ip_tos_mask;
  87. u32 group_id;
  88. } acl;
  89. };
  90. };
  91. struct ofdpa_flow_tbl_entry {
  92. struct hlist_node entry;
  93. u32 cmd;
  94. u64 cookie;
  95. struct ofdpa_flow_tbl_key key;
  96. size_t key_len;
  97. u32 key_crc32; /* key */
  98. };
  99. struct ofdpa_group_tbl_entry {
  100. struct hlist_node entry;
  101. u32 cmd;
  102. u32 group_id; /* key */
  103. u16 group_count;
  104. u32 *group_ids;
  105. union {
  106. struct {
  107. u8 pop_vlan;
  108. } l2_interface;
  109. struct {
  110. u8 eth_src[ETH_ALEN];
  111. u8 eth_dst[ETH_ALEN];
  112. __be16 vlan_id;
  113. u32 group_id;
  114. } l2_rewrite;
  115. struct {
  116. u8 eth_src[ETH_ALEN];
  117. u8 eth_dst[ETH_ALEN];
  118. __be16 vlan_id;
  119. bool ttl_check;
  120. u32 group_id;
  121. } l3_unicast;
  122. };
  123. };
  124. struct ofdpa_fdb_tbl_entry {
  125. struct hlist_node entry;
  126. u32 key_crc32; /* key */
  127. bool learned;
  128. unsigned long touched;
  129. struct ofdpa_fdb_tbl_key {
  130. struct ofdpa_port *ofdpa_port;
  131. u8 addr[ETH_ALEN];
  132. __be16 vlan_id;
  133. } key;
  134. };
  135. struct ofdpa_internal_vlan_tbl_entry {
  136. struct hlist_node entry;
  137. int ifindex; /* key */
  138. u32 ref_count;
  139. __be16 vlan_id;
  140. };
  141. struct ofdpa_neigh_tbl_entry {
  142. struct hlist_node entry;
  143. __be32 ip_addr; /* key */
  144. struct net_device *dev;
  145. u32 ref_count;
  146. u32 index;
  147. u8 eth_dst[ETH_ALEN];
  148. bool ttl_check;
  149. };
  150. enum {
  151. OFDPA_CTRL_LINK_LOCAL_MCAST,
  152. OFDPA_CTRL_LOCAL_ARP,
  153. OFDPA_CTRL_IPV4_MCAST,
  154. OFDPA_CTRL_IPV6_MCAST,
  155. OFDPA_CTRL_DFLT_BRIDGING,
  156. OFDPA_CTRL_DFLT_OVS,
  157. OFDPA_CTRL_MAX,
  158. };
  159. #define OFDPA_INTERNAL_VLAN_ID_BASE 0x0f00
  160. #define OFDPA_N_INTERNAL_VLANS 255
  161. #define OFDPA_VLAN_BITMAP_LEN BITS_TO_LONGS(VLAN_N_VID)
  162. #define OFDPA_INTERNAL_VLAN_BITMAP_LEN BITS_TO_LONGS(OFDPA_N_INTERNAL_VLANS)
  163. #define OFDPA_UNTAGGED_VID 0
  164. struct ofdpa {
  165. struct rocker *rocker;
  166. DECLARE_HASHTABLE(flow_tbl, 16);
  167. spinlock_t flow_tbl_lock; /* for flow tbl accesses */
  168. u64 flow_tbl_next_cookie;
  169. DECLARE_HASHTABLE(group_tbl, 16);
  170. spinlock_t group_tbl_lock; /* for group tbl accesses */
  171. struct timer_list fdb_cleanup_timer;
  172. DECLARE_HASHTABLE(fdb_tbl, 16);
  173. spinlock_t fdb_tbl_lock; /* for fdb tbl accesses */
  174. unsigned long internal_vlan_bitmap[OFDPA_INTERNAL_VLAN_BITMAP_LEN];
  175. DECLARE_HASHTABLE(internal_vlan_tbl, 8);
  176. spinlock_t internal_vlan_tbl_lock; /* for vlan tbl accesses */
  177. DECLARE_HASHTABLE(neigh_tbl, 16);
  178. spinlock_t neigh_tbl_lock; /* for neigh tbl accesses */
  179. u32 neigh_tbl_next_index;
  180. unsigned long ageing_time;
  181. };
  182. struct ofdpa_port {
  183. struct ofdpa *ofdpa;
  184. struct rocker_port *rocker_port;
  185. struct net_device *dev;
  186. u32 pport;
  187. struct net_device *bridge_dev;
  188. __be16 internal_vlan_id;
  189. int stp_state;
  190. u32 brport_flags;
  191. unsigned long ageing_time;
  192. bool ctrls[OFDPA_CTRL_MAX];
  193. unsigned long vlan_bitmap[OFDPA_VLAN_BITMAP_LEN];
  194. };
  195. static const u8 zero_mac[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  196. static const u8 ff_mac[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  197. static const u8 ll_mac[ETH_ALEN] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
  198. static const u8 ll_mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 };
  199. static const u8 mcast_mac[ETH_ALEN] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
  200. static const u8 ipv4_mcast[ETH_ALEN] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
  201. static const u8 ipv4_mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 };
  202. static const u8 ipv6_mcast[ETH_ALEN] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
  203. static const u8 ipv6_mask[ETH_ALEN] = { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
  204. /* Rocker priority levels for flow table entries. Higher
  205. * priority match takes precedence over lower priority match.
  206. */
  207. enum {
  208. OFDPA_PRIORITY_UNKNOWN = 0,
  209. OFDPA_PRIORITY_IG_PORT = 1,
  210. OFDPA_PRIORITY_VLAN = 1,
  211. OFDPA_PRIORITY_TERM_MAC_UCAST = 0,
  212. OFDPA_PRIORITY_TERM_MAC_MCAST = 1,
  213. OFDPA_PRIORITY_BRIDGING_VLAN_DFLT_EXACT = 1,
  214. OFDPA_PRIORITY_BRIDGING_VLAN_DFLT_WILD = 2,
  215. OFDPA_PRIORITY_BRIDGING_VLAN = 3,
  216. OFDPA_PRIORITY_BRIDGING_TENANT_DFLT_EXACT = 1,
  217. OFDPA_PRIORITY_BRIDGING_TENANT_DFLT_WILD = 2,
  218. OFDPA_PRIORITY_BRIDGING_TENANT = 3,
  219. OFDPA_PRIORITY_ACL_CTRL = 3,
  220. OFDPA_PRIORITY_ACL_NORMAL = 2,
  221. OFDPA_PRIORITY_ACL_DFLT = 1,
  222. };
  223. static bool ofdpa_vlan_id_is_internal(__be16 vlan_id)
  224. {
  225. u16 start = OFDPA_INTERNAL_VLAN_ID_BASE;
  226. u16 end = 0xffe;
  227. u16 _vlan_id = ntohs(vlan_id);
  228. return (_vlan_id >= start && _vlan_id <= end);
  229. }
  230. static __be16 ofdpa_port_vid_to_vlan(const struct ofdpa_port *ofdpa_port,
  231. u16 vid, bool *pop_vlan)
  232. {
  233. __be16 vlan_id;
  234. if (pop_vlan)
  235. *pop_vlan = false;
  236. vlan_id = htons(vid);
  237. if (!vlan_id) {
  238. vlan_id = ofdpa_port->internal_vlan_id;
  239. if (pop_vlan)
  240. *pop_vlan = true;
  241. }
  242. return vlan_id;
  243. }
  244. static u16 ofdpa_port_vlan_to_vid(const struct ofdpa_port *ofdpa_port,
  245. __be16 vlan_id)
  246. {
  247. if (ofdpa_vlan_id_is_internal(vlan_id))
  248. return 0;
  249. return ntohs(vlan_id);
  250. }
  251. static bool ofdpa_port_is_slave(const struct ofdpa_port *ofdpa_port,
  252. const char *kind)
  253. {
  254. return ofdpa_port->bridge_dev &&
  255. !strcmp(ofdpa_port->bridge_dev->rtnl_link_ops->kind, kind);
  256. }
  257. static bool ofdpa_port_is_bridged(const struct ofdpa_port *ofdpa_port)
  258. {
  259. return ofdpa_port_is_slave(ofdpa_port, "bridge");
  260. }
  261. static bool ofdpa_port_is_ovsed(const struct ofdpa_port *ofdpa_port)
  262. {
  263. return ofdpa_port_is_slave(ofdpa_port, "openvswitch");
  264. }
  265. #define OFDPA_OP_FLAG_REMOVE BIT(0)
  266. #define OFDPA_OP_FLAG_NOWAIT BIT(1)
  267. #define OFDPA_OP_FLAG_LEARNED BIT(2)
  268. #define OFDPA_OP_FLAG_REFRESH BIT(3)
  269. static bool ofdpa_flags_nowait(int flags)
  270. {
  271. return flags & OFDPA_OP_FLAG_NOWAIT;
  272. }
  273. static void *__ofdpa_mem_alloc(struct switchdev_trans *trans, int flags,
  274. size_t size)
  275. {
  276. struct switchdev_trans_item *elem = NULL;
  277. gfp_t gfp_flags = (flags & OFDPA_OP_FLAG_NOWAIT) ?
  278. GFP_ATOMIC : GFP_KERNEL;
  279. /* If in transaction prepare phase, allocate the memory
  280. * and enqueue it on a transaction. If in transaction
  281. * commit phase, dequeue the memory from the transaction
  282. * rather than re-allocating the memory. The idea is the
  283. * driver code paths for prepare and commit are identical
  284. * so the memory allocated in the prepare phase is the
  285. * memory used in the commit phase.
  286. */
  287. if (!trans) {
  288. elem = kzalloc(size + sizeof(*elem), gfp_flags);
  289. } else if (switchdev_trans_ph_prepare(trans)) {
  290. elem = kzalloc(size + sizeof(*elem), gfp_flags);
  291. if (!elem)
  292. return NULL;
  293. switchdev_trans_item_enqueue(trans, elem, kfree, elem);
  294. } else {
  295. elem = switchdev_trans_item_dequeue(trans);
  296. }
  297. return elem ? elem + 1 : NULL;
  298. }
  299. static void *ofdpa_kzalloc(struct switchdev_trans *trans, int flags,
  300. size_t size)
  301. {
  302. return __ofdpa_mem_alloc(trans, flags, size);
  303. }
  304. static void *ofdpa_kcalloc(struct switchdev_trans *trans, int flags,
  305. size_t n, size_t size)
  306. {
  307. return __ofdpa_mem_alloc(trans, flags, n * size);
  308. }
  309. static void ofdpa_kfree(struct switchdev_trans *trans, const void *mem)
  310. {
  311. struct switchdev_trans_item *elem;
  312. /* Frees are ignored if in transaction prepare phase. The
  313. * memory remains on the per-port list until freed in the
  314. * commit phase.
  315. */
  316. if (switchdev_trans_ph_prepare(trans))
  317. return;
  318. elem = (struct switchdev_trans_item *) mem - 1;
  319. kfree(elem);
  320. }
  321. /*************************************************************
  322. * Flow, group, FDB, internal VLAN and neigh command prepares
  323. *************************************************************/
  324. static int
  325. ofdpa_cmd_flow_tbl_add_ig_port(struct rocker_desc_info *desc_info,
  326. const struct ofdpa_flow_tbl_entry *entry)
  327. {
  328. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
  329. entry->key.ig_port.in_pport))
  330. return -EMSGSIZE;
  331. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
  332. entry->key.ig_port.in_pport_mask))
  333. return -EMSGSIZE;
  334. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
  335. entry->key.ig_port.goto_tbl))
  336. return -EMSGSIZE;
  337. return 0;
  338. }
  339. static int
  340. ofdpa_cmd_flow_tbl_add_vlan(struct rocker_desc_info *desc_info,
  341. const struct ofdpa_flow_tbl_entry *entry)
  342. {
  343. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
  344. entry->key.vlan.in_pport))
  345. return -EMSGSIZE;
  346. if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
  347. entry->key.vlan.vlan_id))
  348. return -EMSGSIZE;
  349. if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
  350. entry->key.vlan.vlan_id_mask))
  351. return -EMSGSIZE;
  352. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
  353. entry->key.vlan.goto_tbl))
  354. return -EMSGSIZE;
  355. if (entry->key.vlan.untagged &&
  356. rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_NEW_VLAN_ID,
  357. entry->key.vlan.new_vlan_id))
  358. return -EMSGSIZE;
  359. return 0;
  360. }
  361. static int
  362. ofdpa_cmd_flow_tbl_add_term_mac(struct rocker_desc_info *desc_info,
  363. const struct ofdpa_flow_tbl_entry *entry)
  364. {
  365. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
  366. entry->key.term_mac.in_pport))
  367. return -EMSGSIZE;
  368. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
  369. entry->key.term_mac.in_pport_mask))
  370. return -EMSGSIZE;
  371. if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
  372. entry->key.term_mac.eth_type))
  373. return -EMSGSIZE;
  374. if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
  375. ETH_ALEN, entry->key.term_mac.eth_dst))
  376. return -EMSGSIZE;
  377. if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
  378. ETH_ALEN, entry->key.term_mac.eth_dst_mask))
  379. return -EMSGSIZE;
  380. if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
  381. entry->key.term_mac.vlan_id))
  382. return -EMSGSIZE;
  383. if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
  384. entry->key.term_mac.vlan_id_mask))
  385. return -EMSGSIZE;
  386. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
  387. entry->key.term_mac.goto_tbl))
  388. return -EMSGSIZE;
  389. if (entry->key.term_mac.copy_to_cpu &&
  390. rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_COPY_CPU_ACTION,
  391. entry->key.term_mac.copy_to_cpu))
  392. return -EMSGSIZE;
  393. return 0;
  394. }
  395. static int
  396. ofdpa_cmd_flow_tbl_add_ucast_routing(struct rocker_desc_info *desc_info,
  397. const struct ofdpa_flow_tbl_entry *entry)
  398. {
  399. if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
  400. entry->key.ucast_routing.eth_type))
  401. return -EMSGSIZE;
  402. if (rocker_tlv_put_be32(desc_info, ROCKER_TLV_OF_DPA_DST_IP,
  403. entry->key.ucast_routing.dst4))
  404. return -EMSGSIZE;
  405. if (rocker_tlv_put_be32(desc_info, ROCKER_TLV_OF_DPA_DST_IP_MASK,
  406. entry->key.ucast_routing.dst4_mask))
  407. return -EMSGSIZE;
  408. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
  409. entry->key.ucast_routing.goto_tbl))
  410. return -EMSGSIZE;
  411. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
  412. entry->key.ucast_routing.group_id))
  413. return -EMSGSIZE;
  414. return 0;
  415. }
  416. static int
  417. ofdpa_cmd_flow_tbl_add_bridge(struct rocker_desc_info *desc_info,
  418. const struct ofdpa_flow_tbl_entry *entry)
  419. {
  420. if (entry->key.bridge.has_eth_dst &&
  421. rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
  422. ETH_ALEN, entry->key.bridge.eth_dst))
  423. return -EMSGSIZE;
  424. if (entry->key.bridge.has_eth_dst_mask &&
  425. rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
  426. ETH_ALEN, entry->key.bridge.eth_dst_mask))
  427. return -EMSGSIZE;
  428. if (entry->key.bridge.vlan_id &&
  429. rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
  430. entry->key.bridge.vlan_id))
  431. return -EMSGSIZE;
  432. if (entry->key.bridge.tunnel_id &&
  433. rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_TUNNEL_ID,
  434. entry->key.bridge.tunnel_id))
  435. return -EMSGSIZE;
  436. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
  437. entry->key.bridge.goto_tbl))
  438. return -EMSGSIZE;
  439. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
  440. entry->key.bridge.group_id))
  441. return -EMSGSIZE;
  442. if (entry->key.bridge.copy_to_cpu &&
  443. rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_COPY_CPU_ACTION,
  444. entry->key.bridge.copy_to_cpu))
  445. return -EMSGSIZE;
  446. return 0;
  447. }
  448. static int
  449. ofdpa_cmd_flow_tbl_add_acl(struct rocker_desc_info *desc_info,
  450. const struct ofdpa_flow_tbl_entry *entry)
  451. {
  452. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
  453. entry->key.acl.in_pport))
  454. return -EMSGSIZE;
  455. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
  456. entry->key.acl.in_pport_mask))
  457. return -EMSGSIZE;
  458. if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
  459. ETH_ALEN, entry->key.acl.eth_src))
  460. return -EMSGSIZE;
  461. if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC_MASK,
  462. ETH_ALEN, entry->key.acl.eth_src_mask))
  463. return -EMSGSIZE;
  464. if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
  465. ETH_ALEN, entry->key.acl.eth_dst))
  466. return -EMSGSIZE;
  467. if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
  468. ETH_ALEN, entry->key.acl.eth_dst_mask))
  469. return -EMSGSIZE;
  470. if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
  471. entry->key.acl.eth_type))
  472. return -EMSGSIZE;
  473. if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
  474. entry->key.acl.vlan_id))
  475. return -EMSGSIZE;
  476. if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
  477. entry->key.acl.vlan_id_mask))
  478. return -EMSGSIZE;
  479. switch (ntohs(entry->key.acl.eth_type)) {
  480. case ETH_P_IP:
  481. case ETH_P_IPV6:
  482. if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_PROTO,
  483. entry->key.acl.ip_proto))
  484. return -EMSGSIZE;
  485. if (rocker_tlv_put_u8(desc_info,
  486. ROCKER_TLV_OF_DPA_IP_PROTO_MASK,
  487. entry->key.acl.ip_proto_mask))
  488. return -EMSGSIZE;
  489. if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_DSCP,
  490. entry->key.acl.ip_tos & 0x3f))
  491. return -EMSGSIZE;
  492. if (rocker_tlv_put_u8(desc_info,
  493. ROCKER_TLV_OF_DPA_IP_DSCP_MASK,
  494. entry->key.acl.ip_tos_mask & 0x3f))
  495. return -EMSGSIZE;
  496. if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_ECN,
  497. (entry->key.acl.ip_tos & 0xc0) >> 6))
  498. return -EMSGSIZE;
  499. if (rocker_tlv_put_u8(desc_info,
  500. ROCKER_TLV_OF_DPA_IP_ECN_MASK,
  501. (entry->key.acl.ip_tos_mask & 0xc0) >> 6))
  502. return -EMSGSIZE;
  503. break;
  504. }
  505. if (entry->key.acl.group_id != ROCKER_GROUP_NONE &&
  506. rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
  507. entry->key.acl.group_id))
  508. return -EMSGSIZE;
  509. return 0;
  510. }
  511. static int ofdpa_cmd_flow_tbl_add(const struct rocker_port *rocker_port,
  512. struct rocker_desc_info *desc_info,
  513. void *priv)
  514. {
  515. const struct ofdpa_flow_tbl_entry *entry = priv;
  516. struct rocker_tlv *cmd_info;
  517. int err = 0;
  518. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
  519. return -EMSGSIZE;
  520. cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
  521. if (!cmd_info)
  522. return -EMSGSIZE;
  523. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_TABLE_ID,
  524. entry->key.tbl_id))
  525. return -EMSGSIZE;
  526. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_PRIORITY,
  527. entry->key.priority))
  528. return -EMSGSIZE;
  529. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_HARDTIME, 0))
  530. return -EMSGSIZE;
  531. if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_OF_DPA_COOKIE,
  532. entry->cookie))
  533. return -EMSGSIZE;
  534. switch (entry->key.tbl_id) {
  535. case ROCKER_OF_DPA_TABLE_ID_INGRESS_PORT:
  536. err = ofdpa_cmd_flow_tbl_add_ig_port(desc_info, entry);
  537. break;
  538. case ROCKER_OF_DPA_TABLE_ID_VLAN:
  539. err = ofdpa_cmd_flow_tbl_add_vlan(desc_info, entry);
  540. break;
  541. case ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC:
  542. err = ofdpa_cmd_flow_tbl_add_term_mac(desc_info, entry);
  543. break;
  544. case ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING:
  545. err = ofdpa_cmd_flow_tbl_add_ucast_routing(desc_info, entry);
  546. break;
  547. case ROCKER_OF_DPA_TABLE_ID_BRIDGING:
  548. err = ofdpa_cmd_flow_tbl_add_bridge(desc_info, entry);
  549. break;
  550. case ROCKER_OF_DPA_TABLE_ID_ACL_POLICY:
  551. err = ofdpa_cmd_flow_tbl_add_acl(desc_info, entry);
  552. break;
  553. default:
  554. err = -ENOTSUPP;
  555. break;
  556. }
  557. if (err)
  558. return err;
  559. rocker_tlv_nest_end(desc_info, cmd_info);
  560. return 0;
  561. }
  562. static int ofdpa_cmd_flow_tbl_del(const struct rocker_port *rocker_port,
  563. struct rocker_desc_info *desc_info,
  564. void *priv)
  565. {
  566. const struct ofdpa_flow_tbl_entry *entry = priv;
  567. struct rocker_tlv *cmd_info;
  568. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
  569. return -EMSGSIZE;
  570. cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
  571. if (!cmd_info)
  572. return -EMSGSIZE;
  573. if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_OF_DPA_COOKIE,
  574. entry->cookie))
  575. return -EMSGSIZE;
  576. rocker_tlv_nest_end(desc_info, cmd_info);
  577. return 0;
  578. }
  579. static int
  580. ofdpa_cmd_group_tbl_add_l2_interface(struct rocker_desc_info *desc_info,
  581. struct ofdpa_group_tbl_entry *entry)
  582. {
  583. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_OUT_PPORT,
  584. ROCKER_GROUP_PORT_GET(entry->group_id)))
  585. return -EMSGSIZE;
  586. if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_POP_VLAN,
  587. entry->l2_interface.pop_vlan))
  588. return -EMSGSIZE;
  589. return 0;
  590. }
  591. static int
  592. ofdpa_cmd_group_tbl_add_l2_rewrite(struct rocker_desc_info *desc_info,
  593. const struct ofdpa_group_tbl_entry *entry)
  594. {
  595. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID_LOWER,
  596. entry->l2_rewrite.group_id))
  597. return -EMSGSIZE;
  598. if (!is_zero_ether_addr(entry->l2_rewrite.eth_src) &&
  599. rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
  600. ETH_ALEN, entry->l2_rewrite.eth_src))
  601. return -EMSGSIZE;
  602. if (!is_zero_ether_addr(entry->l2_rewrite.eth_dst) &&
  603. rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
  604. ETH_ALEN, entry->l2_rewrite.eth_dst))
  605. return -EMSGSIZE;
  606. if (entry->l2_rewrite.vlan_id &&
  607. rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
  608. entry->l2_rewrite.vlan_id))
  609. return -EMSGSIZE;
  610. return 0;
  611. }
  612. static int
  613. ofdpa_cmd_group_tbl_add_group_ids(struct rocker_desc_info *desc_info,
  614. const struct ofdpa_group_tbl_entry *entry)
  615. {
  616. int i;
  617. struct rocker_tlv *group_ids;
  618. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GROUP_COUNT,
  619. entry->group_count))
  620. return -EMSGSIZE;
  621. group_ids = rocker_tlv_nest_start(desc_info,
  622. ROCKER_TLV_OF_DPA_GROUP_IDS);
  623. if (!group_ids)
  624. return -EMSGSIZE;
  625. for (i = 0; i < entry->group_count; i++)
  626. /* Note TLV array is 1-based */
  627. if (rocker_tlv_put_u32(desc_info, i + 1, entry->group_ids[i]))
  628. return -EMSGSIZE;
  629. rocker_tlv_nest_end(desc_info, group_ids);
  630. return 0;
  631. }
  632. static int
  633. ofdpa_cmd_group_tbl_add_l3_unicast(struct rocker_desc_info *desc_info,
  634. const struct ofdpa_group_tbl_entry *entry)
  635. {
  636. if (!is_zero_ether_addr(entry->l3_unicast.eth_src) &&
  637. rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
  638. ETH_ALEN, entry->l3_unicast.eth_src))
  639. return -EMSGSIZE;
  640. if (!is_zero_ether_addr(entry->l3_unicast.eth_dst) &&
  641. rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
  642. ETH_ALEN, entry->l3_unicast.eth_dst))
  643. return -EMSGSIZE;
  644. if (entry->l3_unicast.vlan_id &&
  645. rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
  646. entry->l3_unicast.vlan_id))
  647. return -EMSGSIZE;
  648. if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_TTL_CHECK,
  649. entry->l3_unicast.ttl_check))
  650. return -EMSGSIZE;
  651. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID_LOWER,
  652. entry->l3_unicast.group_id))
  653. return -EMSGSIZE;
  654. return 0;
  655. }
  656. static int ofdpa_cmd_group_tbl_add(const struct rocker_port *rocker_port,
  657. struct rocker_desc_info *desc_info,
  658. void *priv)
  659. {
  660. struct ofdpa_group_tbl_entry *entry = priv;
  661. struct rocker_tlv *cmd_info;
  662. int err = 0;
  663. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
  664. return -EMSGSIZE;
  665. cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
  666. if (!cmd_info)
  667. return -EMSGSIZE;
  668. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
  669. entry->group_id))
  670. return -EMSGSIZE;
  671. switch (ROCKER_GROUP_TYPE_GET(entry->group_id)) {
  672. case ROCKER_OF_DPA_GROUP_TYPE_L2_INTERFACE:
  673. err = ofdpa_cmd_group_tbl_add_l2_interface(desc_info, entry);
  674. break;
  675. case ROCKER_OF_DPA_GROUP_TYPE_L2_REWRITE:
  676. err = ofdpa_cmd_group_tbl_add_l2_rewrite(desc_info, entry);
  677. break;
  678. case ROCKER_OF_DPA_GROUP_TYPE_L2_FLOOD:
  679. case ROCKER_OF_DPA_GROUP_TYPE_L2_MCAST:
  680. err = ofdpa_cmd_group_tbl_add_group_ids(desc_info, entry);
  681. break;
  682. case ROCKER_OF_DPA_GROUP_TYPE_L3_UCAST:
  683. err = ofdpa_cmd_group_tbl_add_l3_unicast(desc_info, entry);
  684. break;
  685. default:
  686. err = -ENOTSUPP;
  687. break;
  688. }
  689. if (err)
  690. return err;
  691. rocker_tlv_nest_end(desc_info, cmd_info);
  692. return 0;
  693. }
  694. static int ofdpa_cmd_group_tbl_del(const struct rocker_port *rocker_port,
  695. struct rocker_desc_info *desc_info,
  696. void *priv)
  697. {
  698. const struct ofdpa_group_tbl_entry *entry = priv;
  699. struct rocker_tlv *cmd_info;
  700. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
  701. return -EMSGSIZE;
  702. cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
  703. if (!cmd_info)
  704. return -EMSGSIZE;
  705. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
  706. entry->group_id))
  707. return -EMSGSIZE;
  708. rocker_tlv_nest_end(desc_info, cmd_info);
  709. return 0;
  710. }
  711. /***************************************************
  712. * Flow, group, FDB, internal VLAN and neigh tables
  713. ***************************************************/
  714. static struct ofdpa_flow_tbl_entry *
  715. ofdpa_flow_tbl_find(const struct ofdpa *ofdpa,
  716. const struct ofdpa_flow_tbl_entry *match)
  717. {
  718. struct ofdpa_flow_tbl_entry *found;
  719. size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
  720. hash_for_each_possible(ofdpa->flow_tbl, found,
  721. entry, match->key_crc32) {
  722. if (memcmp(&found->key, &match->key, key_len) == 0)
  723. return found;
  724. }
  725. return NULL;
  726. }
  727. static int ofdpa_flow_tbl_add(struct ofdpa_port *ofdpa_port,
  728. struct switchdev_trans *trans, int flags,
  729. struct ofdpa_flow_tbl_entry *match)
  730. {
  731. struct ofdpa *ofdpa = ofdpa_port->ofdpa;
  732. struct ofdpa_flow_tbl_entry *found;
  733. size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
  734. unsigned long lock_flags;
  735. match->key_crc32 = crc32(~0, &match->key, key_len);
  736. spin_lock_irqsave(&ofdpa->flow_tbl_lock, lock_flags);
  737. found = ofdpa_flow_tbl_find(ofdpa, match);
  738. if (found) {
  739. match->cookie = found->cookie;
  740. if (!switchdev_trans_ph_prepare(trans))
  741. hash_del(&found->entry);
  742. ofdpa_kfree(trans, found);
  743. found = match;
  744. found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_MOD;
  745. } else {
  746. found = match;
  747. found->cookie = ofdpa->flow_tbl_next_cookie++;
  748. found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_ADD;
  749. }
  750. if (!switchdev_trans_ph_prepare(trans))
  751. hash_add(ofdpa->flow_tbl, &found->entry, found->key_crc32);
  752. spin_unlock_irqrestore(&ofdpa->flow_tbl_lock, lock_flags);
  753. if (!switchdev_trans_ph_prepare(trans))
  754. return rocker_cmd_exec(ofdpa_port->rocker_port,
  755. ofdpa_flags_nowait(flags),
  756. ofdpa_cmd_flow_tbl_add,
  757. found, NULL, NULL);
  758. return 0;
  759. }
  760. static int ofdpa_flow_tbl_del(struct ofdpa_port *ofdpa_port,
  761. struct switchdev_trans *trans, int flags,
  762. struct ofdpa_flow_tbl_entry *match)
  763. {
  764. struct ofdpa *ofdpa = ofdpa_port->ofdpa;
  765. struct ofdpa_flow_tbl_entry *found;
  766. size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
  767. unsigned long lock_flags;
  768. int err = 0;
  769. match->key_crc32 = crc32(~0, &match->key, key_len);
  770. spin_lock_irqsave(&ofdpa->flow_tbl_lock, lock_flags);
  771. found = ofdpa_flow_tbl_find(ofdpa, match);
  772. if (found) {
  773. if (!switchdev_trans_ph_prepare(trans))
  774. hash_del(&found->entry);
  775. found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_DEL;
  776. }
  777. spin_unlock_irqrestore(&ofdpa->flow_tbl_lock, lock_flags);
  778. ofdpa_kfree(trans, match);
  779. if (found) {
  780. if (!switchdev_trans_ph_prepare(trans))
  781. err = rocker_cmd_exec(ofdpa_port->rocker_port,
  782. ofdpa_flags_nowait(flags),
  783. ofdpa_cmd_flow_tbl_del,
  784. found, NULL, NULL);
  785. ofdpa_kfree(trans, found);
  786. }
  787. return err;
  788. }
  789. static int ofdpa_flow_tbl_do(struct ofdpa_port *ofdpa_port,
  790. struct switchdev_trans *trans, int flags,
  791. struct ofdpa_flow_tbl_entry *entry)
  792. {
  793. if (flags & OFDPA_OP_FLAG_REMOVE)
  794. return ofdpa_flow_tbl_del(ofdpa_port, trans, flags, entry);
  795. else
  796. return ofdpa_flow_tbl_add(ofdpa_port, trans, flags, entry);
  797. }
  798. static int ofdpa_flow_tbl_ig_port(struct ofdpa_port *ofdpa_port,
  799. struct switchdev_trans *trans, int flags,
  800. u32 in_pport, u32 in_pport_mask,
  801. enum rocker_of_dpa_table_id goto_tbl)
  802. {
  803. struct ofdpa_flow_tbl_entry *entry;
  804. entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
  805. if (!entry)
  806. return -ENOMEM;
  807. entry->key.priority = OFDPA_PRIORITY_IG_PORT;
  808. entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_INGRESS_PORT;
  809. entry->key.ig_port.in_pport = in_pport;
  810. entry->key.ig_port.in_pport_mask = in_pport_mask;
  811. entry->key.ig_port.goto_tbl = goto_tbl;
  812. return ofdpa_flow_tbl_do(ofdpa_port, trans, flags, entry);
  813. }
  814. static int ofdpa_flow_tbl_vlan(struct ofdpa_port *ofdpa_port,
  815. struct switchdev_trans *trans, int flags,
  816. u32 in_pport, __be16 vlan_id,
  817. __be16 vlan_id_mask,
  818. enum rocker_of_dpa_table_id goto_tbl,
  819. bool untagged, __be16 new_vlan_id)
  820. {
  821. struct ofdpa_flow_tbl_entry *entry;
  822. entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
  823. if (!entry)
  824. return -ENOMEM;
  825. entry->key.priority = OFDPA_PRIORITY_VLAN;
  826. entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_VLAN;
  827. entry->key.vlan.in_pport = in_pport;
  828. entry->key.vlan.vlan_id = vlan_id;
  829. entry->key.vlan.vlan_id_mask = vlan_id_mask;
  830. entry->key.vlan.goto_tbl = goto_tbl;
  831. entry->key.vlan.untagged = untagged;
  832. entry->key.vlan.new_vlan_id = new_vlan_id;
  833. return ofdpa_flow_tbl_do(ofdpa_port, trans, flags, entry);
  834. }
  835. static int ofdpa_flow_tbl_term_mac(struct ofdpa_port *ofdpa_port,
  836. struct switchdev_trans *trans,
  837. u32 in_pport, u32 in_pport_mask,
  838. __be16 eth_type, const u8 *eth_dst,
  839. const u8 *eth_dst_mask, __be16 vlan_id,
  840. __be16 vlan_id_mask, bool copy_to_cpu,
  841. int flags)
  842. {
  843. struct ofdpa_flow_tbl_entry *entry;
  844. entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
  845. if (!entry)
  846. return -ENOMEM;
  847. if (is_multicast_ether_addr(eth_dst)) {
  848. entry->key.priority = OFDPA_PRIORITY_TERM_MAC_MCAST;
  849. entry->key.term_mac.goto_tbl =
  850. ROCKER_OF_DPA_TABLE_ID_MULTICAST_ROUTING;
  851. } else {
  852. entry->key.priority = OFDPA_PRIORITY_TERM_MAC_UCAST;
  853. entry->key.term_mac.goto_tbl =
  854. ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING;
  855. }
  856. entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC;
  857. entry->key.term_mac.in_pport = in_pport;
  858. entry->key.term_mac.in_pport_mask = in_pport_mask;
  859. entry->key.term_mac.eth_type = eth_type;
  860. ether_addr_copy(entry->key.term_mac.eth_dst, eth_dst);
  861. ether_addr_copy(entry->key.term_mac.eth_dst_mask, eth_dst_mask);
  862. entry->key.term_mac.vlan_id = vlan_id;
  863. entry->key.term_mac.vlan_id_mask = vlan_id_mask;
  864. entry->key.term_mac.copy_to_cpu = copy_to_cpu;
  865. return ofdpa_flow_tbl_do(ofdpa_port, trans, flags, entry);
  866. }
  867. static int ofdpa_flow_tbl_bridge(struct ofdpa_port *ofdpa_port,
  868. struct switchdev_trans *trans, int flags,
  869. const u8 *eth_dst, const u8 *eth_dst_mask,
  870. __be16 vlan_id, u32 tunnel_id,
  871. enum rocker_of_dpa_table_id goto_tbl,
  872. u32 group_id, bool copy_to_cpu)
  873. {
  874. struct ofdpa_flow_tbl_entry *entry;
  875. u32 priority;
  876. bool vlan_bridging = !!vlan_id;
  877. bool dflt = !eth_dst || (eth_dst && eth_dst_mask);
  878. bool wild = false;
  879. entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
  880. if (!entry)
  881. return -ENOMEM;
  882. entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_BRIDGING;
  883. if (eth_dst) {
  884. entry->key.bridge.has_eth_dst = 1;
  885. ether_addr_copy(entry->key.bridge.eth_dst, eth_dst);
  886. }
  887. if (eth_dst_mask) {
  888. entry->key.bridge.has_eth_dst_mask = 1;
  889. ether_addr_copy(entry->key.bridge.eth_dst_mask, eth_dst_mask);
  890. if (!ether_addr_equal(eth_dst_mask, ff_mac))
  891. wild = true;
  892. }
  893. priority = OFDPA_PRIORITY_UNKNOWN;
  894. if (vlan_bridging && dflt && wild)
  895. priority = OFDPA_PRIORITY_BRIDGING_VLAN_DFLT_WILD;
  896. else if (vlan_bridging && dflt && !wild)
  897. priority = OFDPA_PRIORITY_BRIDGING_VLAN_DFLT_EXACT;
  898. else if (vlan_bridging && !dflt)
  899. priority = OFDPA_PRIORITY_BRIDGING_VLAN;
  900. else if (!vlan_bridging && dflt && wild)
  901. priority = OFDPA_PRIORITY_BRIDGING_TENANT_DFLT_WILD;
  902. else if (!vlan_bridging && dflt && !wild)
  903. priority = OFDPA_PRIORITY_BRIDGING_TENANT_DFLT_EXACT;
  904. else if (!vlan_bridging && !dflt)
  905. priority = OFDPA_PRIORITY_BRIDGING_TENANT;
  906. entry->key.priority = priority;
  907. entry->key.bridge.vlan_id = vlan_id;
  908. entry->key.bridge.tunnel_id = tunnel_id;
  909. entry->key.bridge.goto_tbl = goto_tbl;
  910. entry->key.bridge.group_id = group_id;
  911. entry->key.bridge.copy_to_cpu = copy_to_cpu;
  912. return ofdpa_flow_tbl_do(ofdpa_port, trans, flags, entry);
  913. }
  914. static int ofdpa_flow_tbl_ucast4_routing(struct ofdpa_port *ofdpa_port,
  915. struct switchdev_trans *trans,
  916. __be16 eth_type, __be32 dst,
  917. __be32 dst_mask, u32 priority,
  918. enum rocker_of_dpa_table_id goto_tbl,
  919. u32 group_id, int flags)
  920. {
  921. struct ofdpa_flow_tbl_entry *entry;
  922. entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
  923. if (!entry)
  924. return -ENOMEM;
  925. entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING;
  926. entry->key.priority = priority;
  927. entry->key.ucast_routing.eth_type = eth_type;
  928. entry->key.ucast_routing.dst4 = dst;
  929. entry->key.ucast_routing.dst4_mask = dst_mask;
  930. entry->key.ucast_routing.goto_tbl = goto_tbl;
  931. entry->key.ucast_routing.group_id = group_id;
  932. entry->key_len = offsetof(struct ofdpa_flow_tbl_key,
  933. ucast_routing.group_id);
  934. return ofdpa_flow_tbl_do(ofdpa_port, trans, flags, entry);
  935. }
  936. static int ofdpa_flow_tbl_acl(struct ofdpa_port *ofdpa_port,
  937. struct switchdev_trans *trans, int flags,
  938. u32 in_pport, u32 in_pport_mask,
  939. const u8 *eth_src, const u8 *eth_src_mask,
  940. const u8 *eth_dst, const u8 *eth_dst_mask,
  941. __be16 eth_type, __be16 vlan_id,
  942. __be16 vlan_id_mask, u8 ip_proto,
  943. u8 ip_proto_mask, u8 ip_tos, u8 ip_tos_mask,
  944. u32 group_id)
  945. {
  946. u32 priority;
  947. struct ofdpa_flow_tbl_entry *entry;
  948. entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
  949. if (!entry)
  950. return -ENOMEM;
  951. priority = OFDPA_PRIORITY_ACL_NORMAL;
  952. if (eth_dst && eth_dst_mask) {
  953. if (ether_addr_equal(eth_dst_mask, mcast_mac))
  954. priority = OFDPA_PRIORITY_ACL_DFLT;
  955. else if (is_link_local_ether_addr(eth_dst))
  956. priority = OFDPA_PRIORITY_ACL_CTRL;
  957. }
  958. entry->key.priority = priority;
  959. entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
  960. entry->key.acl.in_pport = in_pport;
  961. entry->key.acl.in_pport_mask = in_pport_mask;
  962. if (eth_src)
  963. ether_addr_copy(entry->key.acl.eth_src, eth_src);
  964. if (eth_src_mask)
  965. ether_addr_copy(entry->key.acl.eth_src_mask, eth_src_mask);
  966. if (eth_dst)
  967. ether_addr_copy(entry->key.acl.eth_dst, eth_dst);
  968. if (eth_dst_mask)
  969. ether_addr_copy(entry->key.acl.eth_dst_mask, eth_dst_mask);
  970. entry->key.acl.eth_type = eth_type;
  971. entry->key.acl.vlan_id = vlan_id;
  972. entry->key.acl.vlan_id_mask = vlan_id_mask;
  973. entry->key.acl.ip_proto = ip_proto;
  974. entry->key.acl.ip_proto_mask = ip_proto_mask;
  975. entry->key.acl.ip_tos = ip_tos;
  976. entry->key.acl.ip_tos_mask = ip_tos_mask;
  977. entry->key.acl.group_id = group_id;
  978. return ofdpa_flow_tbl_do(ofdpa_port, trans, flags, entry);
  979. }
  980. static struct ofdpa_group_tbl_entry *
  981. ofdpa_group_tbl_find(const struct ofdpa *ofdpa,
  982. const struct ofdpa_group_tbl_entry *match)
  983. {
  984. struct ofdpa_group_tbl_entry *found;
  985. hash_for_each_possible(ofdpa->group_tbl, found,
  986. entry, match->group_id) {
  987. if (found->group_id == match->group_id)
  988. return found;
  989. }
  990. return NULL;
  991. }
  992. static void ofdpa_group_tbl_entry_free(struct switchdev_trans *trans,
  993. struct ofdpa_group_tbl_entry *entry)
  994. {
  995. switch (ROCKER_GROUP_TYPE_GET(entry->group_id)) {
  996. case ROCKER_OF_DPA_GROUP_TYPE_L2_FLOOD:
  997. case ROCKER_OF_DPA_GROUP_TYPE_L2_MCAST:
  998. ofdpa_kfree(trans, entry->group_ids);
  999. break;
  1000. default:
  1001. break;
  1002. }
  1003. ofdpa_kfree(trans, entry);
  1004. }
  1005. static int ofdpa_group_tbl_add(struct ofdpa_port *ofdpa_port,
  1006. struct switchdev_trans *trans, int flags,
  1007. struct ofdpa_group_tbl_entry *match)
  1008. {
  1009. struct ofdpa *ofdpa = ofdpa_port->ofdpa;
  1010. struct ofdpa_group_tbl_entry *found;
  1011. unsigned long lock_flags;
  1012. spin_lock_irqsave(&ofdpa->group_tbl_lock, lock_flags);
  1013. found = ofdpa_group_tbl_find(ofdpa, match);
  1014. if (found) {
  1015. if (!switchdev_trans_ph_prepare(trans))
  1016. hash_del(&found->entry);
  1017. ofdpa_group_tbl_entry_free(trans, found);
  1018. found = match;
  1019. found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_MOD;
  1020. } else {
  1021. found = match;
  1022. found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_ADD;
  1023. }
  1024. if (!switchdev_trans_ph_prepare(trans))
  1025. hash_add(ofdpa->group_tbl, &found->entry, found->group_id);
  1026. spin_unlock_irqrestore(&ofdpa->group_tbl_lock, lock_flags);
  1027. if (!switchdev_trans_ph_prepare(trans))
  1028. return rocker_cmd_exec(ofdpa_port->rocker_port,
  1029. ofdpa_flags_nowait(flags),
  1030. ofdpa_cmd_group_tbl_add,
  1031. found, NULL, NULL);
  1032. return 0;
  1033. }
  1034. static int ofdpa_group_tbl_del(struct ofdpa_port *ofdpa_port,
  1035. struct switchdev_trans *trans, int flags,
  1036. struct ofdpa_group_tbl_entry *match)
  1037. {
  1038. struct ofdpa *ofdpa = ofdpa_port->ofdpa;
  1039. struct ofdpa_group_tbl_entry *found;
  1040. unsigned long lock_flags;
  1041. int err = 0;
  1042. spin_lock_irqsave(&ofdpa->group_tbl_lock, lock_flags);
  1043. found = ofdpa_group_tbl_find(ofdpa, match);
  1044. if (found) {
  1045. if (!switchdev_trans_ph_prepare(trans))
  1046. hash_del(&found->entry);
  1047. found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_DEL;
  1048. }
  1049. spin_unlock_irqrestore(&ofdpa->group_tbl_lock, lock_flags);
  1050. ofdpa_group_tbl_entry_free(trans, match);
  1051. if (found) {
  1052. if (!switchdev_trans_ph_prepare(trans))
  1053. err = rocker_cmd_exec(ofdpa_port->rocker_port,
  1054. ofdpa_flags_nowait(flags),
  1055. ofdpa_cmd_group_tbl_del,
  1056. found, NULL, NULL);
  1057. ofdpa_group_tbl_entry_free(trans, found);
  1058. }
  1059. return err;
  1060. }
  1061. static int ofdpa_group_tbl_do(struct ofdpa_port *ofdpa_port,
  1062. struct switchdev_trans *trans, int flags,
  1063. struct ofdpa_group_tbl_entry *entry)
  1064. {
  1065. if (flags & OFDPA_OP_FLAG_REMOVE)
  1066. return ofdpa_group_tbl_del(ofdpa_port, trans, flags, entry);
  1067. else
  1068. return ofdpa_group_tbl_add(ofdpa_port, trans, flags, entry);
  1069. }
  1070. static int ofdpa_group_l2_interface(struct ofdpa_port *ofdpa_port,
  1071. struct switchdev_trans *trans, int flags,
  1072. __be16 vlan_id, u32 out_pport,
  1073. int pop_vlan)
  1074. {
  1075. struct ofdpa_group_tbl_entry *entry;
  1076. entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
  1077. if (!entry)
  1078. return -ENOMEM;
  1079. entry->group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
  1080. entry->l2_interface.pop_vlan = pop_vlan;
  1081. return ofdpa_group_tbl_do(ofdpa_port, trans, flags, entry);
  1082. }
  1083. static int ofdpa_group_l2_fan_out(struct ofdpa_port *ofdpa_port,
  1084. struct switchdev_trans *trans,
  1085. int flags, u8 group_count,
  1086. const u32 *group_ids, u32 group_id)
  1087. {
  1088. struct ofdpa_group_tbl_entry *entry;
  1089. entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
  1090. if (!entry)
  1091. return -ENOMEM;
  1092. entry->group_id = group_id;
  1093. entry->group_count = group_count;
  1094. entry->group_ids = ofdpa_kcalloc(trans, flags,
  1095. group_count, sizeof(u32));
  1096. if (!entry->group_ids) {
  1097. ofdpa_kfree(trans, entry);
  1098. return -ENOMEM;
  1099. }
  1100. memcpy(entry->group_ids, group_ids, group_count * sizeof(u32));
  1101. return ofdpa_group_tbl_do(ofdpa_port, trans, flags, entry);
  1102. }
  1103. static int ofdpa_group_l2_flood(struct ofdpa_port *ofdpa_port,
  1104. struct switchdev_trans *trans, int flags,
  1105. __be16 vlan_id, u8 group_count,
  1106. const u32 *group_ids, u32 group_id)
  1107. {
  1108. return ofdpa_group_l2_fan_out(ofdpa_port, trans, flags,
  1109. group_count, group_ids,
  1110. group_id);
  1111. }
  1112. static int ofdpa_group_l3_unicast(struct ofdpa_port *ofdpa_port,
  1113. struct switchdev_trans *trans, int flags,
  1114. u32 index, const u8 *src_mac, const u8 *dst_mac,
  1115. __be16 vlan_id, bool ttl_check, u32 pport)
  1116. {
  1117. struct ofdpa_group_tbl_entry *entry;
  1118. entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
  1119. if (!entry)
  1120. return -ENOMEM;
  1121. entry->group_id = ROCKER_GROUP_L3_UNICAST(index);
  1122. if (src_mac)
  1123. ether_addr_copy(entry->l3_unicast.eth_src, src_mac);
  1124. if (dst_mac)
  1125. ether_addr_copy(entry->l3_unicast.eth_dst, dst_mac);
  1126. entry->l3_unicast.vlan_id = vlan_id;
  1127. entry->l3_unicast.ttl_check = ttl_check;
  1128. entry->l3_unicast.group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, pport);
  1129. return ofdpa_group_tbl_do(ofdpa_port, trans, flags, entry);
  1130. }
  1131. static struct ofdpa_neigh_tbl_entry *
  1132. ofdpa_neigh_tbl_find(const struct ofdpa *ofdpa, __be32 ip_addr)
  1133. {
  1134. struct ofdpa_neigh_tbl_entry *found;
  1135. hash_for_each_possible(ofdpa->neigh_tbl, found,
  1136. entry, be32_to_cpu(ip_addr))
  1137. if (found->ip_addr == ip_addr)
  1138. return found;
  1139. return NULL;
  1140. }
  1141. static void ofdpa_neigh_add(struct ofdpa *ofdpa,
  1142. struct switchdev_trans *trans,
  1143. struct ofdpa_neigh_tbl_entry *entry)
  1144. {
  1145. if (!switchdev_trans_ph_commit(trans))
  1146. entry->index = ofdpa->neigh_tbl_next_index++;
  1147. if (switchdev_trans_ph_prepare(trans))
  1148. return;
  1149. entry->ref_count++;
  1150. hash_add(ofdpa->neigh_tbl, &entry->entry,
  1151. be32_to_cpu(entry->ip_addr));
  1152. }
  1153. static void ofdpa_neigh_del(struct switchdev_trans *trans,
  1154. struct ofdpa_neigh_tbl_entry *entry)
  1155. {
  1156. if (switchdev_trans_ph_prepare(trans))
  1157. return;
  1158. if (--entry->ref_count == 0) {
  1159. hash_del(&entry->entry);
  1160. ofdpa_kfree(trans, entry);
  1161. }
  1162. }
  1163. static void ofdpa_neigh_update(struct ofdpa_neigh_tbl_entry *entry,
  1164. struct switchdev_trans *trans,
  1165. const u8 *eth_dst, bool ttl_check)
  1166. {
  1167. if (eth_dst) {
  1168. ether_addr_copy(entry->eth_dst, eth_dst);
  1169. entry->ttl_check = ttl_check;
  1170. } else if (!switchdev_trans_ph_prepare(trans)) {
  1171. entry->ref_count++;
  1172. }
  1173. }
  1174. static int ofdpa_port_ipv4_neigh(struct ofdpa_port *ofdpa_port,
  1175. struct switchdev_trans *trans,
  1176. int flags, __be32 ip_addr, const u8 *eth_dst)
  1177. {
  1178. struct ofdpa *ofdpa = ofdpa_port->ofdpa;
  1179. struct ofdpa_neigh_tbl_entry *entry;
  1180. struct ofdpa_neigh_tbl_entry *found;
  1181. unsigned long lock_flags;
  1182. __be16 eth_type = htons(ETH_P_IP);
  1183. enum rocker_of_dpa_table_id goto_tbl =
  1184. ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
  1185. u32 group_id;
  1186. u32 priority = 0;
  1187. bool adding = !(flags & OFDPA_OP_FLAG_REMOVE);
  1188. bool updating;
  1189. bool removing;
  1190. int err = 0;
  1191. entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
  1192. if (!entry)
  1193. return -ENOMEM;
  1194. spin_lock_irqsave(&ofdpa->neigh_tbl_lock, lock_flags);
  1195. found = ofdpa_neigh_tbl_find(ofdpa, ip_addr);
  1196. updating = found && adding;
  1197. removing = found && !adding;
  1198. adding = !found && adding;
  1199. if (adding) {
  1200. entry->ip_addr = ip_addr;
  1201. entry->dev = ofdpa_port->dev;
  1202. ether_addr_copy(entry->eth_dst, eth_dst);
  1203. entry->ttl_check = true;
  1204. ofdpa_neigh_add(ofdpa, trans, entry);
  1205. } else if (removing) {
  1206. memcpy(entry, found, sizeof(*entry));
  1207. ofdpa_neigh_del(trans, found);
  1208. } else if (updating) {
  1209. ofdpa_neigh_update(found, trans, eth_dst, true);
  1210. memcpy(entry, found, sizeof(*entry));
  1211. } else {
  1212. err = -ENOENT;
  1213. }
  1214. spin_unlock_irqrestore(&ofdpa->neigh_tbl_lock, lock_flags);
  1215. if (err)
  1216. goto err_out;
  1217. /* For each active neighbor, we have an L3 unicast group and
  1218. * a /32 route to the neighbor, which uses the L3 unicast
  1219. * group. The L3 unicast group can also be referred to by
  1220. * other routes' nexthops.
  1221. */
  1222. err = ofdpa_group_l3_unicast(ofdpa_port, trans, flags,
  1223. entry->index,
  1224. ofdpa_port->dev->dev_addr,
  1225. entry->eth_dst,
  1226. ofdpa_port->internal_vlan_id,
  1227. entry->ttl_check,
  1228. ofdpa_port->pport);
  1229. if (err) {
  1230. netdev_err(ofdpa_port->dev, "Error (%d) L3 unicast group index %d\n",
  1231. err, entry->index);
  1232. goto err_out;
  1233. }
  1234. if (adding || removing) {
  1235. group_id = ROCKER_GROUP_L3_UNICAST(entry->index);
  1236. err = ofdpa_flow_tbl_ucast4_routing(ofdpa_port, trans,
  1237. eth_type, ip_addr,
  1238. inet_make_mask(32),
  1239. priority, goto_tbl,
  1240. group_id, flags);
  1241. if (err)
  1242. netdev_err(ofdpa_port->dev, "Error (%d) /32 unicast route %pI4 group 0x%08x\n",
  1243. err, &entry->ip_addr, group_id);
  1244. }
  1245. err_out:
  1246. if (!adding)
  1247. ofdpa_kfree(trans, entry);
  1248. return err;
  1249. }
  1250. static int ofdpa_port_ipv4_resolve(struct ofdpa_port *ofdpa_port,
  1251. struct switchdev_trans *trans,
  1252. __be32 ip_addr)
  1253. {
  1254. struct net_device *dev = ofdpa_port->dev;
  1255. struct neighbour *n = __ipv4_neigh_lookup(dev, (__force u32)ip_addr);
  1256. int err = 0;
  1257. if (!n) {
  1258. n = neigh_create(&arp_tbl, &ip_addr, dev);
  1259. if (IS_ERR(n))
  1260. return PTR_ERR(n);
  1261. }
  1262. /* If the neigh is already resolved, then go ahead and
  1263. * install the entry, otherwise start the ARP process to
  1264. * resolve the neigh.
  1265. */
  1266. if (n->nud_state & NUD_VALID)
  1267. err = ofdpa_port_ipv4_neigh(ofdpa_port, trans, 0,
  1268. ip_addr, n->ha);
  1269. else
  1270. neigh_event_send(n, NULL);
  1271. neigh_release(n);
  1272. return err;
  1273. }
  1274. static int ofdpa_port_ipv4_nh(struct ofdpa_port *ofdpa_port,
  1275. struct switchdev_trans *trans, int flags,
  1276. __be32 ip_addr, u32 *index)
  1277. {
  1278. struct ofdpa *ofdpa = ofdpa_port->ofdpa;
  1279. struct ofdpa_neigh_tbl_entry *entry;
  1280. struct ofdpa_neigh_tbl_entry *found;
  1281. unsigned long lock_flags;
  1282. bool adding = !(flags & OFDPA_OP_FLAG_REMOVE);
  1283. bool updating;
  1284. bool removing;
  1285. bool resolved = true;
  1286. int err = 0;
  1287. entry = ofdpa_kzalloc(trans, flags, sizeof(*entry));
  1288. if (!entry)
  1289. return -ENOMEM;
  1290. spin_lock_irqsave(&ofdpa->neigh_tbl_lock, lock_flags);
  1291. found = ofdpa_neigh_tbl_find(ofdpa, ip_addr);
  1292. if (found)
  1293. *index = found->index;
  1294. updating = found && adding;
  1295. removing = found && !adding;
  1296. adding = !found && adding;
  1297. if (adding) {
  1298. entry->ip_addr = ip_addr;
  1299. entry->dev = ofdpa_port->dev;
  1300. ofdpa_neigh_add(ofdpa, trans, entry);
  1301. *index = entry->index;
  1302. resolved = false;
  1303. } else if (removing) {
  1304. ofdpa_neigh_del(trans, found);
  1305. } else if (updating) {
  1306. ofdpa_neigh_update(found, trans, NULL, false);
  1307. resolved = !is_zero_ether_addr(found->eth_dst);
  1308. } else {
  1309. err = -ENOENT;
  1310. }
  1311. spin_unlock_irqrestore(&ofdpa->neigh_tbl_lock, lock_flags);
  1312. if (!adding)
  1313. ofdpa_kfree(trans, entry);
  1314. if (err)
  1315. return err;
  1316. /* Resolved means neigh ip_addr is resolved to neigh mac. */
  1317. if (!resolved)
  1318. err = ofdpa_port_ipv4_resolve(ofdpa_port, trans, ip_addr);
  1319. return err;
  1320. }
  1321. static struct ofdpa_port *ofdpa_port_get(const struct ofdpa *ofdpa,
  1322. int port_index)
  1323. {
  1324. struct rocker_port *rocker_port;
  1325. rocker_port = ofdpa->rocker->ports[port_index];
  1326. return rocker_port ? rocker_port->wpriv : NULL;
  1327. }
  1328. static int ofdpa_port_vlan_flood_group(struct ofdpa_port *ofdpa_port,
  1329. struct switchdev_trans *trans,
  1330. int flags, __be16 vlan_id)
  1331. {
  1332. struct ofdpa_port *p;
  1333. const struct ofdpa *ofdpa = ofdpa_port->ofdpa;
  1334. unsigned int port_count = ofdpa->rocker->port_count;
  1335. u32 group_id = ROCKER_GROUP_L2_FLOOD(vlan_id, 0);
  1336. u32 *group_ids;
  1337. u8 group_count = 0;
  1338. int err = 0;
  1339. int i;
  1340. group_ids = ofdpa_kcalloc(trans, flags, port_count, sizeof(u32));
  1341. if (!group_ids)
  1342. return -ENOMEM;
  1343. /* Adjust the flood group for this VLAN. The flood group
  1344. * references an L2 interface group for each port in this
  1345. * VLAN.
  1346. */
  1347. for (i = 0; i < port_count; i++) {
  1348. p = ofdpa_port_get(ofdpa, i);
  1349. if (!p)
  1350. continue;
  1351. if (!ofdpa_port_is_bridged(p))
  1352. continue;
  1353. if (test_bit(ntohs(vlan_id), p->vlan_bitmap)) {
  1354. group_ids[group_count++] =
  1355. ROCKER_GROUP_L2_INTERFACE(vlan_id, p->pport);
  1356. }
  1357. }
  1358. /* If there are no bridged ports in this VLAN, we're done */
  1359. if (group_count == 0)
  1360. goto no_ports_in_vlan;
  1361. err = ofdpa_group_l2_flood(ofdpa_port, trans, flags, vlan_id,
  1362. group_count, group_ids, group_id);
  1363. if (err)
  1364. netdev_err(ofdpa_port->dev, "Error (%d) port VLAN l2 flood group\n", err);
  1365. no_ports_in_vlan:
  1366. ofdpa_kfree(trans, group_ids);
  1367. return err;
  1368. }
  1369. static int ofdpa_port_vlan_l2_groups(struct ofdpa_port *ofdpa_port,
  1370. struct switchdev_trans *trans, int flags,
  1371. __be16 vlan_id, bool pop_vlan)
  1372. {
  1373. const struct ofdpa *ofdpa = ofdpa_port->ofdpa;
  1374. unsigned int port_count = ofdpa->rocker->port_count;
  1375. struct ofdpa_port *p;
  1376. bool adding = !(flags & OFDPA_OP_FLAG_REMOVE);
  1377. u32 out_pport;
  1378. int ref = 0;
  1379. int err;
  1380. int i;
  1381. /* An L2 interface group for this port in this VLAN, but
  1382. * only when port STP state is LEARNING|FORWARDING.
  1383. */
  1384. if (ofdpa_port->stp_state == BR_STATE_LEARNING ||
  1385. ofdpa_port->stp_state == BR_STATE_FORWARDING) {
  1386. out_pport = ofdpa_port->pport;
  1387. err = ofdpa_group_l2_interface(ofdpa_port, trans, flags,
  1388. vlan_id, out_pport, pop_vlan);
  1389. if (err) {
  1390. netdev_err(ofdpa_port->dev, "Error (%d) port VLAN l2 group for pport %d\n",
  1391. err, out_pport);
  1392. return err;
  1393. }
  1394. }
  1395. /* An L2 interface group for this VLAN to CPU port.
  1396. * Add when first port joins this VLAN and destroy when
  1397. * last port leaves this VLAN.
  1398. */
  1399. for (i = 0; i < port_count; i++) {
  1400. p = ofdpa_port_get(ofdpa, i);
  1401. if (p && test_bit(ntohs(vlan_id), p->vlan_bitmap))
  1402. ref++;
  1403. }
  1404. if ((!adding || ref != 1) && (adding || ref != 0))
  1405. return 0;
  1406. out_pport = 0;
  1407. err = ofdpa_group_l2_interface(ofdpa_port, trans, flags,
  1408. vlan_id, out_pport, pop_vlan);
  1409. if (err) {
  1410. netdev_err(ofdpa_port->dev, "Error (%d) port VLAN l2 group for CPU port\n", err);
  1411. return err;
  1412. }
  1413. return 0;
  1414. }
  1415. static struct ofdpa_ctrl {
  1416. const u8 *eth_dst;
  1417. const u8 *eth_dst_mask;
  1418. __be16 eth_type;
  1419. bool acl;
  1420. bool bridge;
  1421. bool term;
  1422. bool copy_to_cpu;
  1423. } ofdpa_ctrls[] = {
  1424. [OFDPA_CTRL_LINK_LOCAL_MCAST] = {
  1425. /* pass link local multicast pkts up to CPU for filtering */
  1426. .eth_dst = ll_mac,
  1427. .eth_dst_mask = ll_mask,
  1428. .acl = true,
  1429. },
  1430. [OFDPA_CTRL_LOCAL_ARP] = {
  1431. /* pass local ARP pkts up to CPU */
  1432. .eth_dst = zero_mac,
  1433. .eth_dst_mask = zero_mac,
  1434. .eth_type = htons(ETH_P_ARP),
  1435. .acl = true,
  1436. },
  1437. [OFDPA_CTRL_IPV4_MCAST] = {
  1438. /* pass IPv4 mcast pkts up to CPU, RFC 1112 */
  1439. .eth_dst = ipv4_mcast,
  1440. .eth_dst_mask = ipv4_mask,
  1441. .eth_type = htons(ETH_P_IP),
  1442. .term = true,
  1443. .copy_to_cpu = true,
  1444. },
  1445. [OFDPA_CTRL_IPV6_MCAST] = {
  1446. /* pass IPv6 mcast pkts up to CPU, RFC 2464 */
  1447. .eth_dst = ipv6_mcast,
  1448. .eth_dst_mask = ipv6_mask,
  1449. .eth_type = htons(ETH_P_IPV6),
  1450. .term = true,
  1451. .copy_to_cpu = true,
  1452. },
  1453. [OFDPA_CTRL_DFLT_BRIDGING] = {
  1454. /* flood any pkts on vlan */
  1455. .bridge = true,
  1456. .copy_to_cpu = true,
  1457. },
  1458. [OFDPA_CTRL_DFLT_OVS] = {
  1459. /* pass all pkts up to CPU */
  1460. .eth_dst = zero_mac,
  1461. .eth_dst_mask = zero_mac,
  1462. .acl = true,
  1463. },
  1464. };
  1465. static int ofdpa_port_ctrl_vlan_acl(struct ofdpa_port *ofdpa_port,
  1466. struct switchdev_trans *trans, int flags,
  1467. const struct ofdpa_ctrl *ctrl, __be16 vlan_id)
  1468. {
  1469. u32 in_pport = ofdpa_port->pport;
  1470. u32 in_pport_mask = 0xffffffff;
  1471. u32 out_pport = 0;
  1472. const u8 *eth_src = NULL;
  1473. const u8 *eth_src_mask = NULL;
  1474. __be16 vlan_id_mask = htons(0xffff);
  1475. u8 ip_proto = 0;
  1476. u8 ip_proto_mask = 0;
  1477. u8 ip_tos = 0;
  1478. u8 ip_tos_mask = 0;
  1479. u32 group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
  1480. int err;
  1481. err = ofdpa_flow_tbl_acl(ofdpa_port, trans, flags,
  1482. in_pport, in_pport_mask,
  1483. eth_src, eth_src_mask,
  1484. ctrl->eth_dst, ctrl->eth_dst_mask,
  1485. ctrl->eth_type,
  1486. vlan_id, vlan_id_mask,
  1487. ip_proto, ip_proto_mask,
  1488. ip_tos, ip_tos_mask,
  1489. group_id);
  1490. if (err)
  1491. netdev_err(ofdpa_port->dev, "Error (%d) ctrl ACL\n", err);
  1492. return err;
  1493. }
  1494. static int ofdpa_port_ctrl_vlan_bridge(struct ofdpa_port *ofdpa_port,
  1495. struct switchdev_trans *trans,
  1496. int flags,
  1497. const struct ofdpa_ctrl *ctrl,
  1498. __be16 vlan_id)
  1499. {
  1500. enum rocker_of_dpa_table_id goto_tbl =
  1501. ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
  1502. u32 group_id = ROCKER_GROUP_L2_FLOOD(vlan_id, 0);
  1503. u32 tunnel_id = 0;
  1504. int err;
  1505. if (!ofdpa_port_is_bridged(ofdpa_port))
  1506. return 0;
  1507. err = ofdpa_flow_tbl_bridge(ofdpa_port, trans, flags,
  1508. ctrl->eth_dst, ctrl->eth_dst_mask,
  1509. vlan_id, tunnel_id,
  1510. goto_tbl, group_id, ctrl->copy_to_cpu);
  1511. if (err)
  1512. netdev_err(ofdpa_port->dev, "Error (%d) ctrl FLOOD\n", err);
  1513. return err;
  1514. }
  1515. static int ofdpa_port_ctrl_vlan_term(struct ofdpa_port *ofdpa_port,
  1516. struct switchdev_trans *trans, int flags,
  1517. const struct ofdpa_ctrl *ctrl, __be16 vlan_id)
  1518. {
  1519. u32 in_pport_mask = 0xffffffff;
  1520. __be16 vlan_id_mask = htons(0xffff);
  1521. int err;
  1522. if (ntohs(vlan_id) == 0)
  1523. vlan_id = ofdpa_port->internal_vlan_id;
  1524. err = ofdpa_flow_tbl_term_mac(ofdpa_port, trans,
  1525. ofdpa_port->pport, in_pport_mask,
  1526. ctrl->eth_type, ctrl->eth_dst,
  1527. ctrl->eth_dst_mask, vlan_id,
  1528. vlan_id_mask, ctrl->copy_to_cpu,
  1529. flags);
  1530. if (err)
  1531. netdev_err(ofdpa_port->dev, "Error (%d) ctrl term\n", err);
  1532. return err;
  1533. }
  1534. static int ofdpa_port_ctrl_vlan(struct ofdpa_port *ofdpa_port,
  1535. struct switchdev_trans *trans, int flags,
  1536. const struct ofdpa_ctrl *ctrl, __be16 vlan_id)
  1537. {
  1538. if (ctrl->acl)
  1539. return ofdpa_port_ctrl_vlan_acl(ofdpa_port, trans, flags,
  1540. ctrl, vlan_id);
  1541. if (ctrl->bridge)
  1542. return ofdpa_port_ctrl_vlan_bridge(ofdpa_port, trans, flags,
  1543. ctrl, vlan_id);
  1544. if (ctrl->term)
  1545. return ofdpa_port_ctrl_vlan_term(ofdpa_port, trans, flags,
  1546. ctrl, vlan_id);
  1547. return -EOPNOTSUPP;
  1548. }
  1549. static int ofdpa_port_ctrl_vlan_add(struct ofdpa_port *ofdpa_port,
  1550. struct switchdev_trans *trans, int flags,
  1551. __be16 vlan_id)
  1552. {
  1553. int err = 0;
  1554. int i;
  1555. for (i = 0; i < OFDPA_CTRL_MAX; i++) {
  1556. if (ofdpa_port->ctrls[i]) {
  1557. err = ofdpa_port_ctrl_vlan(ofdpa_port, trans, flags,
  1558. &ofdpa_ctrls[i], vlan_id);
  1559. if (err)
  1560. return err;
  1561. }
  1562. }
  1563. return err;
  1564. }
  1565. static int ofdpa_port_ctrl(struct ofdpa_port *ofdpa_port,
  1566. struct switchdev_trans *trans, int flags,
  1567. const struct ofdpa_ctrl *ctrl)
  1568. {
  1569. u16 vid;
  1570. int err = 0;
  1571. for (vid = 1; vid < VLAN_N_VID; vid++) {
  1572. if (!test_bit(vid, ofdpa_port->vlan_bitmap))
  1573. continue;
  1574. err = ofdpa_port_ctrl_vlan(ofdpa_port, trans, flags,
  1575. ctrl, htons(vid));
  1576. if (err)
  1577. break;
  1578. }
  1579. return err;
  1580. }
  1581. static int ofdpa_port_vlan(struct ofdpa_port *ofdpa_port,
  1582. struct switchdev_trans *trans, int flags, u16 vid)
  1583. {
  1584. enum rocker_of_dpa_table_id goto_tbl =
  1585. ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC;
  1586. u32 in_pport = ofdpa_port->pport;
  1587. __be16 vlan_id = htons(vid);
  1588. __be16 vlan_id_mask = htons(0xffff);
  1589. __be16 internal_vlan_id;
  1590. bool untagged;
  1591. bool adding = !(flags & OFDPA_OP_FLAG_REMOVE);
  1592. int err;
  1593. internal_vlan_id = ofdpa_port_vid_to_vlan(ofdpa_port, vid, &untagged);
  1594. if (adding &&
  1595. test_bit(ntohs(internal_vlan_id), ofdpa_port->vlan_bitmap))
  1596. return 0; /* already added */
  1597. else if (!adding &&
  1598. !test_bit(ntohs(internal_vlan_id), ofdpa_port->vlan_bitmap))
  1599. return 0; /* already removed */
  1600. change_bit(ntohs(internal_vlan_id), ofdpa_port->vlan_bitmap);
  1601. if (adding) {
  1602. err = ofdpa_port_ctrl_vlan_add(ofdpa_port, trans, flags,
  1603. internal_vlan_id);
  1604. if (err) {
  1605. netdev_err(ofdpa_port->dev, "Error (%d) port ctrl vlan add\n", err);
  1606. goto err_out;
  1607. }
  1608. }
  1609. err = ofdpa_port_vlan_l2_groups(ofdpa_port, trans, flags,
  1610. internal_vlan_id, untagged);
  1611. if (err) {
  1612. netdev_err(ofdpa_port->dev, "Error (%d) port VLAN l2 groups\n", err);
  1613. goto err_out;
  1614. }
  1615. err = ofdpa_port_vlan_flood_group(ofdpa_port, trans, flags,
  1616. internal_vlan_id);
  1617. if (err) {
  1618. netdev_err(ofdpa_port->dev, "Error (%d) port VLAN l2 flood group\n", err);
  1619. goto err_out;
  1620. }
  1621. err = ofdpa_flow_tbl_vlan(ofdpa_port, trans, flags,
  1622. in_pport, vlan_id, vlan_id_mask,
  1623. goto_tbl, untagged, internal_vlan_id);
  1624. if (err)
  1625. netdev_err(ofdpa_port->dev, "Error (%d) port VLAN table\n", err);
  1626. err_out:
  1627. if (switchdev_trans_ph_prepare(trans))
  1628. change_bit(ntohs(internal_vlan_id), ofdpa_port->vlan_bitmap);
  1629. return err;
  1630. }
  1631. static int ofdpa_port_ig_tbl(struct ofdpa_port *ofdpa_port,
  1632. struct switchdev_trans *trans, int flags)
  1633. {
  1634. enum rocker_of_dpa_table_id goto_tbl;
  1635. u32 in_pport;
  1636. u32 in_pport_mask;
  1637. int err;
  1638. /* Normal Ethernet Frames. Matches pkts from any local physical
  1639. * ports. Goto VLAN tbl.
  1640. */
  1641. in_pport = 0;
  1642. in_pport_mask = 0xffff0000;
  1643. goto_tbl = ROCKER_OF_DPA_TABLE_ID_VLAN;
  1644. err = ofdpa_flow_tbl_ig_port(ofdpa_port, trans, flags,
  1645. in_pport, in_pport_mask,
  1646. goto_tbl);
  1647. if (err)
  1648. netdev_err(ofdpa_port->dev, "Error (%d) ingress port table entry\n", err);
  1649. return err;
  1650. }
  1651. struct ofdpa_fdb_learn_work {
  1652. struct work_struct work;
  1653. struct ofdpa_port *ofdpa_port;
  1654. struct switchdev_trans *trans;
  1655. int flags;
  1656. u8 addr[ETH_ALEN];
  1657. u16 vid;
  1658. };
  1659. static void ofdpa_port_fdb_learn_work(struct work_struct *work)
  1660. {
  1661. const struct ofdpa_fdb_learn_work *lw =
  1662. container_of(work, struct ofdpa_fdb_learn_work, work);
  1663. bool removing = (lw->flags & OFDPA_OP_FLAG_REMOVE);
  1664. bool learned = (lw->flags & OFDPA_OP_FLAG_LEARNED);
  1665. struct switchdev_notifier_fdb_info info;
  1666. info.addr = lw->addr;
  1667. info.vid = lw->vid;
  1668. rtnl_lock();
  1669. if (learned && removing)
  1670. call_switchdev_notifiers(SWITCHDEV_FDB_DEL,
  1671. lw->ofdpa_port->dev, &info.info);
  1672. else if (learned && !removing)
  1673. call_switchdev_notifiers(SWITCHDEV_FDB_ADD,
  1674. lw->ofdpa_port->dev, &info.info);
  1675. rtnl_unlock();
  1676. ofdpa_kfree(lw->trans, work);
  1677. }
  1678. static int ofdpa_port_fdb_learn(struct ofdpa_port *ofdpa_port,
  1679. struct switchdev_trans *trans, int flags,
  1680. const u8 *addr, __be16 vlan_id)
  1681. {
  1682. struct ofdpa_fdb_learn_work *lw;
  1683. enum rocker_of_dpa_table_id goto_tbl =
  1684. ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
  1685. u32 out_pport = ofdpa_port->pport;
  1686. u32 tunnel_id = 0;
  1687. u32 group_id = ROCKER_GROUP_NONE;
  1688. bool syncing = !!(ofdpa_port->brport_flags & BR_LEARNING_SYNC);
  1689. bool copy_to_cpu = false;
  1690. int err;
  1691. if (ofdpa_port_is_bridged(ofdpa_port))
  1692. group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
  1693. if (!(flags & OFDPA_OP_FLAG_REFRESH)) {
  1694. err = ofdpa_flow_tbl_bridge(ofdpa_port, trans, flags, addr,
  1695. NULL, vlan_id, tunnel_id, goto_tbl,
  1696. group_id, copy_to_cpu);
  1697. if (err)
  1698. return err;
  1699. }
  1700. if (!syncing)
  1701. return 0;
  1702. if (!ofdpa_port_is_bridged(ofdpa_port))
  1703. return 0;
  1704. lw = ofdpa_kzalloc(trans, flags, sizeof(*lw));
  1705. if (!lw)
  1706. return -ENOMEM;
  1707. INIT_WORK(&lw->work, ofdpa_port_fdb_learn_work);
  1708. lw->ofdpa_port = ofdpa_port;
  1709. lw->trans = trans;
  1710. lw->flags = flags;
  1711. ether_addr_copy(lw->addr, addr);
  1712. lw->vid = ofdpa_port_vlan_to_vid(ofdpa_port, vlan_id);
  1713. if (switchdev_trans_ph_prepare(trans))
  1714. ofdpa_kfree(trans, lw);
  1715. else
  1716. schedule_work(&lw->work);
  1717. return 0;
  1718. }
  1719. static struct ofdpa_fdb_tbl_entry *
  1720. ofdpa_fdb_tbl_find(const struct ofdpa *ofdpa,
  1721. const struct ofdpa_fdb_tbl_entry *match)
  1722. {
  1723. struct ofdpa_fdb_tbl_entry *found;
  1724. hash_for_each_possible(ofdpa->fdb_tbl, found, entry, match->key_crc32)
  1725. if (memcmp(&found->key, &match->key, sizeof(found->key)) == 0)
  1726. return found;
  1727. return NULL;
  1728. }
  1729. static int ofdpa_port_fdb(struct ofdpa_port *ofdpa_port,
  1730. struct switchdev_trans *trans,
  1731. const unsigned char *addr,
  1732. __be16 vlan_id, int flags)
  1733. {
  1734. struct ofdpa *ofdpa = ofdpa_port->ofdpa;
  1735. struct ofdpa_fdb_tbl_entry *fdb;
  1736. struct ofdpa_fdb_tbl_entry *found;
  1737. bool removing = (flags & OFDPA_OP_FLAG_REMOVE);
  1738. unsigned long lock_flags;
  1739. fdb = ofdpa_kzalloc(trans, flags, sizeof(*fdb));
  1740. if (!fdb)
  1741. return -ENOMEM;
  1742. fdb->learned = (flags & OFDPA_OP_FLAG_LEARNED);
  1743. fdb->touched = jiffies;
  1744. fdb->key.ofdpa_port = ofdpa_port;
  1745. ether_addr_copy(fdb->key.addr, addr);
  1746. fdb->key.vlan_id = vlan_id;
  1747. fdb->key_crc32 = crc32(~0, &fdb->key, sizeof(fdb->key));
  1748. spin_lock_irqsave(&ofdpa->fdb_tbl_lock, lock_flags);
  1749. found = ofdpa_fdb_tbl_find(ofdpa, fdb);
  1750. if (found) {
  1751. found->touched = jiffies;
  1752. if (removing) {
  1753. ofdpa_kfree(trans, fdb);
  1754. if (!switchdev_trans_ph_prepare(trans))
  1755. hash_del(&found->entry);
  1756. }
  1757. } else if (!removing) {
  1758. if (!switchdev_trans_ph_prepare(trans))
  1759. hash_add(ofdpa->fdb_tbl, &fdb->entry,
  1760. fdb->key_crc32);
  1761. }
  1762. spin_unlock_irqrestore(&ofdpa->fdb_tbl_lock, lock_flags);
  1763. /* Check if adding and already exists, or removing and can't find */
  1764. if (!found != !removing) {
  1765. ofdpa_kfree(trans, fdb);
  1766. if (!found && removing)
  1767. return 0;
  1768. /* Refreshing existing to update aging timers */
  1769. flags |= OFDPA_OP_FLAG_REFRESH;
  1770. }
  1771. return ofdpa_port_fdb_learn(ofdpa_port, trans, flags, addr, vlan_id);
  1772. }
  1773. static int ofdpa_port_fdb_flush(struct ofdpa_port *ofdpa_port,
  1774. struct switchdev_trans *trans, int flags)
  1775. {
  1776. struct ofdpa *ofdpa = ofdpa_port->ofdpa;
  1777. struct ofdpa_fdb_tbl_entry *found;
  1778. unsigned long lock_flags;
  1779. struct hlist_node *tmp;
  1780. int bkt;
  1781. int err = 0;
  1782. if (ofdpa_port->stp_state == BR_STATE_LEARNING ||
  1783. ofdpa_port->stp_state == BR_STATE_FORWARDING)
  1784. return 0;
  1785. flags |= OFDPA_OP_FLAG_NOWAIT | OFDPA_OP_FLAG_REMOVE;
  1786. spin_lock_irqsave(&ofdpa->fdb_tbl_lock, lock_flags);
  1787. hash_for_each_safe(ofdpa->fdb_tbl, bkt, tmp, found, entry) {
  1788. if (found->key.ofdpa_port != ofdpa_port)
  1789. continue;
  1790. if (!found->learned)
  1791. continue;
  1792. err = ofdpa_port_fdb_learn(ofdpa_port, trans, flags,
  1793. found->key.addr,
  1794. found->key.vlan_id);
  1795. if (err)
  1796. goto err_out;
  1797. if (!switchdev_trans_ph_prepare(trans))
  1798. hash_del(&found->entry);
  1799. }
  1800. err_out:
  1801. spin_unlock_irqrestore(&ofdpa->fdb_tbl_lock, lock_flags);
  1802. return err;
  1803. }
  1804. static void ofdpa_fdb_cleanup(unsigned long data)
  1805. {
  1806. struct ofdpa *ofdpa = (struct ofdpa *)data;
  1807. struct ofdpa_port *ofdpa_port;
  1808. struct ofdpa_fdb_tbl_entry *entry;
  1809. struct hlist_node *tmp;
  1810. unsigned long next_timer = jiffies + ofdpa->ageing_time;
  1811. unsigned long expires;
  1812. unsigned long lock_flags;
  1813. int flags = OFDPA_OP_FLAG_NOWAIT | OFDPA_OP_FLAG_REMOVE |
  1814. OFDPA_OP_FLAG_LEARNED;
  1815. int bkt;
  1816. spin_lock_irqsave(&ofdpa->fdb_tbl_lock, lock_flags);
  1817. hash_for_each_safe(ofdpa->fdb_tbl, bkt, tmp, entry, entry) {
  1818. if (!entry->learned)
  1819. continue;
  1820. ofdpa_port = entry->key.ofdpa_port;
  1821. expires = entry->touched + ofdpa_port->ageing_time;
  1822. if (time_before_eq(expires, jiffies)) {
  1823. ofdpa_port_fdb_learn(ofdpa_port, NULL,
  1824. flags, entry->key.addr,
  1825. entry->key.vlan_id);
  1826. hash_del(&entry->entry);
  1827. } else if (time_before(expires, next_timer)) {
  1828. next_timer = expires;
  1829. }
  1830. }
  1831. spin_unlock_irqrestore(&ofdpa->fdb_tbl_lock, lock_flags);
  1832. mod_timer(&ofdpa->fdb_cleanup_timer, round_jiffies_up(next_timer));
  1833. }
  1834. static int ofdpa_port_router_mac(struct ofdpa_port *ofdpa_port,
  1835. struct switchdev_trans *trans, int flags,
  1836. __be16 vlan_id)
  1837. {
  1838. u32 in_pport_mask = 0xffffffff;
  1839. __be16 eth_type;
  1840. const u8 *dst_mac_mask = ff_mac;
  1841. __be16 vlan_id_mask = htons(0xffff);
  1842. bool copy_to_cpu = false;
  1843. int err;
  1844. if (ntohs(vlan_id) == 0)
  1845. vlan_id = ofdpa_port->internal_vlan_id;
  1846. eth_type = htons(ETH_P_IP);
  1847. err = ofdpa_flow_tbl_term_mac(ofdpa_port, trans,
  1848. ofdpa_port->pport, in_pport_mask,
  1849. eth_type, ofdpa_port->dev->dev_addr,
  1850. dst_mac_mask, vlan_id, vlan_id_mask,
  1851. copy_to_cpu, flags);
  1852. if (err)
  1853. return err;
  1854. eth_type = htons(ETH_P_IPV6);
  1855. err = ofdpa_flow_tbl_term_mac(ofdpa_port, trans,
  1856. ofdpa_port->pport, in_pport_mask,
  1857. eth_type, ofdpa_port->dev->dev_addr,
  1858. dst_mac_mask, vlan_id, vlan_id_mask,
  1859. copy_to_cpu, flags);
  1860. return err;
  1861. }
  1862. static int ofdpa_port_fwding(struct ofdpa_port *ofdpa_port,
  1863. struct switchdev_trans *trans, int flags)
  1864. {
  1865. bool pop_vlan;
  1866. u32 out_pport;
  1867. __be16 vlan_id;
  1868. u16 vid;
  1869. int err;
  1870. /* Port will be forwarding-enabled if its STP state is LEARNING
  1871. * or FORWARDING. Traffic from CPU can still egress, regardless of
  1872. * port STP state. Use L2 interface group on port VLANs as a way
  1873. * to toggle port forwarding: if forwarding is disabled, L2
  1874. * interface group will not exist.
  1875. */
  1876. if (ofdpa_port->stp_state != BR_STATE_LEARNING &&
  1877. ofdpa_port->stp_state != BR_STATE_FORWARDING)
  1878. flags |= OFDPA_OP_FLAG_REMOVE;
  1879. out_pport = ofdpa_port->pport;
  1880. for (vid = 1; vid < VLAN_N_VID; vid++) {
  1881. if (!test_bit(vid, ofdpa_port->vlan_bitmap))
  1882. continue;
  1883. vlan_id = htons(vid);
  1884. pop_vlan = ofdpa_vlan_id_is_internal(vlan_id);
  1885. err = ofdpa_group_l2_interface(ofdpa_port, trans, flags,
  1886. vlan_id, out_pport, pop_vlan);
  1887. if (err) {
  1888. netdev_err(ofdpa_port->dev, "Error (%d) port VLAN l2 group for pport %d\n",
  1889. err, out_pport);
  1890. return err;
  1891. }
  1892. }
  1893. return 0;
  1894. }
  1895. static int ofdpa_port_stp_update(struct ofdpa_port *ofdpa_port,
  1896. struct switchdev_trans *trans,
  1897. int flags, u8 state)
  1898. {
  1899. bool want[OFDPA_CTRL_MAX] = { 0, };
  1900. bool prev_ctrls[OFDPA_CTRL_MAX];
  1901. u8 uninitialized_var(prev_state);
  1902. int err;
  1903. int i;
  1904. if (switchdev_trans_ph_prepare(trans)) {
  1905. memcpy(prev_ctrls, ofdpa_port->ctrls, sizeof(prev_ctrls));
  1906. prev_state = ofdpa_port->stp_state;
  1907. }
  1908. if (ofdpa_port->stp_state == state)
  1909. return 0;
  1910. ofdpa_port->stp_state = state;
  1911. switch (state) {
  1912. case BR_STATE_DISABLED:
  1913. /* port is completely disabled */
  1914. break;
  1915. case BR_STATE_LISTENING:
  1916. case BR_STATE_BLOCKING:
  1917. want[OFDPA_CTRL_LINK_LOCAL_MCAST] = true;
  1918. break;
  1919. case BR_STATE_LEARNING:
  1920. case BR_STATE_FORWARDING:
  1921. if (!ofdpa_port_is_ovsed(ofdpa_port))
  1922. want[OFDPA_CTRL_LINK_LOCAL_MCAST] = true;
  1923. want[OFDPA_CTRL_IPV4_MCAST] = true;
  1924. want[OFDPA_CTRL_IPV6_MCAST] = true;
  1925. if (ofdpa_port_is_bridged(ofdpa_port))
  1926. want[OFDPA_CTRL_DFLT_BRIDGING] = true;
  1927. else if (ofdpa_port_is_ovsed(ofdpa_port))
  1928. want[OFDPA_CTRL_DFLT_OVS] = true;
  1929. else
  1930. want[OFDPA_CTRL_LOCAL_ARP] = true;
  1931. break;
  1932. }
  1933. for (i = 0; i < OFDPA_CTRL_MAX; i++) {
  1934. if (want[i] != ofdpa_port->ctrls[i]) {
  1935. int ctrl_flags = flags |
  1936. (want[i] ? 0 : OFDPA_OP_FLAG_REMOVE);
  1937. err = ofdpa_port_ctrl(ofdpa_port, trans, ctrl_flags,
  1938. &ofdpa_ctrls[i]);
  1939. if (err)
  1940. goto err_out;
  1941. ofdpa_port->ctrls[i] = want[i];
  1942. }
  1943. }
  1944. err = ofdpa_port_fdb_flush(ofdpa_port, trans, flags);
  1945. if (err)
  1946. goto err_out;
  1947. err = ofdpa_port_fwding(ofdpa_port, trans, flags);
  1948. err_out:
  1949. if (switchdev_trans_ph_prepare(trans)) {
  1950. memcpy(ofdpa_port->ctrls, prev_ctrls, sizeof(prev_ctrls));
  1951. ofdpa_port->stp_state = prev_state;
  1952. }
  1953. return err;
  1954. }
  1955. static int ofdpa_port_fwd_enable(struct ofdpa_port *ofdpa_port, int flags)
  1956. {
  1957. if (ofdpa_port_is_bridged(ofdpa_port))
  1958. /* bridge STP will enable port */
  1959. return 0;
  1960. /* port is not bridged, so simulate going to FORWARDING state */
  1961. return ofdpa_port_stp_update(ofdpa_port, NULL, flags,
  1962. BR_STATE_FORWARDING);
  1963. }
  1964. static int ofdpa_port_fwd_disable(struct ofdpa_port *ofdpa_port, int flags)
  1965. {
  1966. if (ofdpa_port_is_bridged(ofdpa_port))
  1967. /* bridge STP will disable port */
  1968. return 0;
  1969. /* port is not bridged, so simulate going to DISABLED state */
  1970. return ofdpa_port_stp_update(ofdpa_port, NULL, flags,
  1971. BR_STATE_DISABLED);
  1972. }
  1973. static int ofdpa_port_vlan_add(struct ofdpa_port *ofdpa_port,
  1974. struct switchdev_trans *trans,
  1975. u16 vid, u16 flags)
  1976. {
  1977. int err;
  1978. /* XXX deal with flags for PVID and untagged */
  1979. err = ofdpa_port_vlan(ofdpa_port, trans, 0, vid);
  1980. if (err)
  1981. return err;
  1982. err = ofdpa_port_router_mac(ofdpa_port, trans, 0, htons(vid));
  1983. if (err)
  1984. ofdpa_port_vlan(ofdpa_port, trans,
  1985. OFDPA_OP_FLAG_REMOVE, vid);
  1986. return err;
  1987. }
  1988. static int ofdpa_port_vlan_del(struct ofdpa_port *ofdpa_port,
  1989. u16 vid, u16 flags)
  1990. {
  1991. int err;
  1992. err = ofdpa_port_router_mac(ofdpa_port, NULL,
  1993. OFDPA_OP_FLAG_REMOVE, htons(vid));
  1994. if (err)
  1995. return err;
  1996. return ofdpa_port_vlan(ofdpa_port, NULL,
  1997. OFDPA_OP_FLAG_REMOVE, vid);
  1998. }
  1999. static struct ofdpa_internal_vlan_tbl_entry *
  2000. ofdpa_internal_vlan_tbl_find(const struct ofdpa *ofdpa, int ifindex)
  2001. {
  2002. struct ofdpa_internal_vlan_tbl_entry *found;
  2003. hash_for_each_possible(ofdpa->internal_vlan_tbl, found,
  2004. entry, ifindex) {
  2005. if (found->ifindex == ifindex)
  2006. return found;
  2007. }
  2008. return NULL;
  2009. }
  2010. static __be16 ofdpa_port_internal_vlan_id_get(struct ofdpa_port *ofdpa_port,
  2011. int ifindex)
  2012. {
  2013. struct ofdpa *ofdpa = ofdpa_port->ofdpa;
  2014. struct ofdpa_internal_vlan_tbl_entry *entry;
  2015. struct ofdpa_internal_vlan_tbl_entry *found;
  2016. unsigned long lock_flags;
  2017. int i;
  2018. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  2019. if (!entry)
  2020. return 0;
  2021. entry->ifindex = ifindex;
  2022. spin_lock_irqsave(&ofdpa->internal_vlan_tbl_lock, lock_flags);
  2023. found = ofdpa_internal_vlan_tbl_find(ofdpa, ifindex);
  2024. if (found) {
  2025. kfree(entry);
  2026. goto found;
  2027. }
  2028. found = entry;
  2029. hash_add(ofdpa->internal_vlan_tbl, &found->entry, found->ifindex);
  2030. for (i = 0; i < OFDPA_N_INTERNAL_VLANS; i++) {
  2031. if (test_and_set_bit(i, ofdpa->internal_vlan_bitmap))
  2032. continue;
  2033. found->vlan_id = htons(OFDPA_INTERNAL_VLAN_ID_BASE + i);
  2034. goto found;
  2035. }
  2036. netdev_err(ofdpa_port->dev, "Out of internal VLAN IDs\n");
  2037. found:
  2038. found->ref_count++;
  2039. spin_unlock_irqrestore(&ofdpa->internal_vlan_tbl_lock, lock_flags);
  2040. return found->vlan_id;
  2041. }
  2042. static int ofdpa_port_fib_ipv4(struct ofdpa_port *ofdpa_port,
  2043. struct switchdev_trans *trans, __be32 dst,
  2044. int dst_len, const struct fib_info *fi,
  2045. u32 tb_id, int flags)
  2046. {
  2047. const struct fib_nh *nh;
  2048. __be16 eth_type = htons(ETH_P_IP);
  2049. __be32 dst_mask = inet_make_mask(dst_len);
  2050. __be16 internal_vlan_id = ofdpa_port->internal_vlan_id;
  2051. u32 priority = fi->fib_priority;
  2052. enum rocker_of_dpa_table_id goto_tbl =
  2053. ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
  2054. u32 group_id;
  2055. bool nh_on_port;
  2056. bool has_gw;
  2057. u32 index;
  2058. int err;
  2059. /* XXX support ECMP */
  2060. nh = fi->fib_nh;
  2061. nh_on_port = (fi->fib_dev == ofdpa_port->dev);
  2062. has_gw = !!nh->nh_gw;
  2063. if (has_gw && nh_on_port) {
  2064. err = ofdpa_port_ipv4_nh(ofdpa_port, trans, flags,
  2065. nh->nh_gw, &index);
  2066. if (err)
  2067. return err;
  2068. group_id = ROCKER_GROUP_L3_UNICAST(index);
  2069. } else {
  2070. /* Send to CPU for processing */
  2071. group_id = ROCKER_GROUP_L2_INTERFACE(internal_vlan_id, 0);
  2072. }
  2073. err = ofdpa_flow_tbl_ucast4_routing(ofdpa_port, trans, eth_type, dst,
  2074. dst_mask, priority, goto_tbl,
  2075. group_id, flags);
  2076. if (err)
  2077. netdev_err(ofdpa_port->dev, "Error (%d) IPv4 route %pI4\n",
  2078. err, &dst);
  2079. return err;
  2080. }
  2081. static void
  2082. ofdpa_port_internal_vlan_id_put(const struct ofdpa_port *ofdpa_port,
  2083. int ifindex)
  2084. {
  2085. struct ofdpa *ofdpa = ofdpa_port->ofdpa;
  2086. struct ofdpa_internal_vlan_tbl_entry *found;
  2087. unsigned long lock_flags;
  2088. unsigned long bit;
  2089. spin_lock_irqsave(&ofdpa->internal_vlan_tbl_lock, lock_flags);
  2090. found = ofdpa_internal_vlan_tbl_find(ofdpa, ifindex);
  2091. if (!found) {
  2092. netdev_err(ofdpa_port->dev,
  2093. "ifindex (%d) not found in internal VLAN tbl\n",
  2094. ifindex);
  2095. goto not_found;
  2096. }
  2097. if (--found->ref_count <= 0) {
  2098. bit = ntohs(found->vlan_id) - OFDPA_INTERNAL_VLAN_ID_BASE;
  2099. clear_bit(bit, ofdpa->internal_vlan_bitmap);
  2100. hash_del(&found->entry);
  2101. kfree(found);
  2102. }
  2103. not_found:
  2104. spin_unlock_irqrestore(&ofdpa->internal_vlan_tbl_lock, lock_flags);
  2105. }
  2106. /**********************************
  2107. * Rocker world ops implementation
  2108. **********************************/
  2109. static int ofdpa_init(struct rocker *rocker)
  2110. {
  2111. struct ofdpa *ofdpa = rocker->wpriv;
  2112. ofdpa->rocker = rocker;
  2113. hash_init(ofdpa->flow_tbl);
  2114. spin_lock_init(&ofdpa->flow_tbl_lock);
  2115. hash_init(ofdpa->group_tbl);
  2116. spin_lock_init(&ofdpa->group_tbl_lock);
  2117. hash_init(ofdpa->fdb_tbl);
  2118. spin_lock_init(&ofdpa->fdb_tbl_lock);
  2119. hash_init(ofdpa->internal_vlan_tbl);
  2120. spin_lock_init(&ofdpa->internal_vlan_tbl_lock);
  2121. hash_init(ofdpa->neigh_tbl);
  2122. spin_lock_init(&ofdpa->neigh_tbl_lock);
  2123. setup_timer(&ofdpa->fdb_cleanup_timer, ofdpa_fdb_cleanup,
  2124. (unsigned long) ofdpa);
  2125. mod_timer(&ofdpa->fdb_cleanup_timer, jiffies);
  2126. ofdpa->ageing_time = BR_DEFAULT_AGEING_TIME;
  2127. return 0;
  2128. }
  2129. static void ofdpa_fini(struct rocker *rocker)
  2130. {
  2131. struct ofdpa *ofdpa = rocker->wpriv;
  2132. unsigned long flags;
  2133. struct ofdpa_flow_tbl_entry *flow_entry;
  2134. struct ofdpa_group_tbl_entry *group_entry;
  2135. struct ofdpa_fdb_tbl_entry *fdb_entry;
  2136. struct ofdpa_internal_vlan_tbl_entry *internal_vlan_entry;
  2137. struct ofdpa_neigh_tbl_entry *neigh_entry;
  2138. struct hlist_node *tmp;
  2139. int bkt;
  2140. del_timer_sync(&ofdpa->fdb_cleanup_timer);
  2141. spin_lock_irqsave(&ofdpa->flow_tbl_lock, flags);
  2142. hash_for_each_safe(ofdpa->flow_tbl, bkt, tmp, flow_entry, entry)
  2143. hash_del(&flow_entry->entry);
  2144. spin_unlock_irqrestore(&ofdpa->flow_tbl_lock, flags);
  2145. spin_lock_irqsave(&ofdpa->group_tbl_lock, flags);
  2146. hash_for_each_safe(ofdpa->group_tbl, bkt, tmp, group_entry, entry)
  2147. hash_del(&group_entry->entry);
  2148. spin_unlock_irqrestore(&ofdpa->group_tbl_lock, flags);
  2149. spin_lock_irqsave(&ofdpa->fdb_tbl_lock, flags);
  2150. hash_for_each_safe(ofdpa->fdb_tbl, bkt, tmp, fdb_entry, entry)
  2151. hash_del(&fdb_entry->entry);
  2152. spin_unlock_irqrestore(&ofdpa->fdb_tbl_lock, flags);
  2153. spin_lock_irqsave(&ofdpa->internal_vlan_tbl_lock, flags);
  2154. hash_for_each_safe(ofdpa->internal_vlan_tbl, bkt,
  2155. tmp, internal_vlan_entry, entry)
  2156. hash_del(&internal_vlan_entry->entry);
  2157. spin_unlock_irqrestore(&ofdpa->internal_vlan_tbl_lock, flags);
  2158. spin_lock_irqsave(&ofdpa->neigh_tbl_lock, flags);
  2159. hash_for_each_safe(ofdpa->neigh_tbl, bkt, tmp, neigh_entry, entry)
  2160. hash_del(&neigh_entry->entry);
  2161. spin_unlock_irqrestore(&ofdpa->neigh_tbl_lock, flags);
  2162. }
  2163. static int ofdpa_port_pre_init(struct rocker_port *rocker_port)
  2164. {
  2165. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2166. ofdpa_port->ofdpa = rocker_port->rocker->wpriv;
  2167. ofdpa_port->rocker_port = rocker_port;
  2168. ofdpa_port->dev = rocker_port->dev;
  2169. ofdpa_port->pport = rocker_port->pport;
  2170. ofdpa_port->brport_flags = BR_LEARNING | BR_LEARNING_SYNC;
  2171. ofdpa_port->ageing_time = BR_DEFAULT_AGEING_TIME;
  2172. return 0;
  2173. }
  2174. static int ofdpa_port_init(struct rocker_port *rocker_port)
  2175. {
  2176. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2177. int err;
  2178. switchdev_port_fwd_mark_set(ofdpa_port->dev, NULL, false);
  2179. rocker_port_set_learning(rocker_port,
  2180. !!(ofdpa_port->brport_flags & BR_LEARNING));
  2181. err = ofdpa_port_ig_tbl(ofdpa_port, NULL, 0);
  2182. if (err) {
  2183. netdev_err(ofdpa_port->dev, "install ig port table failed\n");
  2184. return err;
  2185. }
  2186. ofdpa_port->internal_vlan_id =
  2187. ofdpa_port_internal_vlan_id_get(ofdpa_port,
  2188. ofdpa_port->dev->ifindex);
  2189. err = ofdpa_port_vlan_add(ofdpa_port, NULL, OFDPA_UNTAGGED_VID, 0);
  2190. if (err) {
  2191. netdev_err(ofdpa_port->dev, "install untagged VLAN failed\n");
  2192. goto err_untagged_vlan;
  2193. }
  2194. return 0;
  2195. err_untagged_vlan:
  2196. ofdpa_port_ig_tbl(ofdpa_port, NULL, OFDPA_OP_FLAG_REMOVE);
  2197. return err;
  2198. }
  2199. static void ofdpa_port_fini(struct rocker_port *rocker_port)
  2200. {
  2201. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2202. ofdpa_port_ig_tbl(ofdpa_port, NULL, OFDPA_OP_FLAG_REMOVE);
  2203. }
  2204. static int ofdpa_port_open(struct rocker_port *rocker_port)
  2205. {
  2206. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2207. return ofdpa_port_fwd_enable(ofdpa_port, 0);
  2208. }
  2209. static void ofdpa_port_stop(struct rocker_port *rocker_port)
  2210. {
  2211. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2212. ofdpa_port_fwd_disable(ofdpa_port, OFDPA_OP_FLAG_NOWAIT);
  2213. }
  2214. static int ofdpa_port_attr_stp_state_set(struct rocker_port *rocker_port,
  2215. u8 state,
  2216. struct switchdev_trans *trans)
  2217. {
  2218. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2219. return ofdpa_port_stp_update(ofdpa_port, trans, 0, state);
  2220. }
  2221. static int ofdpa_port_attr_bridge_flags_set(struct rocker_port *rocker_port,
  2222. unsigned long brport_flags,
  2223. struct switchdev_trans *trans)
  2224. {
  2225. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2226. unsigned long orig_flags;
  2227. int err = 0;
  2228. orig_flags = ofdpa_port->brport_flags;
  2229. ofdpa_port->brport_flags = brport_flags;
  2230. if ((orig_flags ^ ofdpa_port->brport_flags) & BR_LEARNING &&
  2231. !switchdev_trans_ph_prepare(trans))
  2232. err = rocker_port_set_learning(ofdpa_port->rocker_port,
  2233. !!(ofdpa_port->brport_flags & BR_LEARNING));
  2234. if (switchdev_trans_ph_prepare(trans))
  2235. ofdpa_port->brport_flags = orig_flags;
  2236. return err;
  2237. }
  2238. static int
  2239. ofdpa_port_attr_bridge_flags_get(const struct rocker_port *rocker_port,
  2240. unsigned long *p_brport_flags)
  2241. {
  2242. const struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2243. *p_brport_flags = ofdpa_port->brport_flags;
  2244. return 0;
  2245. }
  2246. static int
  2247. ofdpa_port_attr_bridge_ageing_time_set(struct rocker_port *rocker_port,
  2248. u32 ageing_time,
  2249. struct switchdev_trans *trans)
  2250. {
  2251. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2252. struct ofdpa *ofdpa = ofdpa_port->ofdpa;
  2253. if (!switchdev_trans_ph_prepare(trans)) {
  2254. ofdpa_port->ageing_time = clock_t_to_jiffies(ageing_time);
  2255. if (ofdpa_port->ageing_time < ofdpa->ageing_time)
  2256. ofdpa->ageing_time = ofdpa_port->ageing_time;
  2257. mod_timer(&ofdpa_port->ofdpa->fdb_cleanup_timer, jiffies);
  2258. }
  2259. return 0;
  2260. }
  2261. static int ofdpa_port_obj_vlan_add(struct rocker_port *rocker_port,
  2262. const struct switchdev_obj_port_vlan *vlan,
  2263. struct switchdev_trans *trans)
  2264. {
  2265. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2266. u16 vid;
  2267. int err;
  2268. for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
  2269. err = ofdpa_port_vlan_add(ofdpa_port, trans, vid, vlan->flags);
  2270. if (err)
  2271. return err;
  2272. }
  2273. return 0;
  2274. }
  2275. static int ofdpa_port_obj_vlan_del(struct rocker_port *rocker_port,
  2276. const struct switchdev_obj_port_vlan *vlan)
  2277. {
  2278. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2279. u16 vid;
  2280. int err;
  2281. for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
  2282. err = ofdpa_port_vlan_del(ofdpa_port, vid, vlan->flags);
  2283. if (err)
  2284. return err;
  2285. }
  2286. return 0;
  2287. }
  2288. static int ofdpa_port_obj_vlan_dump(const struct rocker_port *rocker_port,
  2289. struct switchdev_obj_port_vlan *vlan,
  2290. switchdev_obj_dump_cb_t *cb)
  2291. {
  2292. const struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2293. u16 vid;
  2294. int err = 0;
  2295. for (vid = 1; vid < VLAN_N_VID; vid++) {
  2296. if (!test_bit(vid, ofdpa_port->vlan_bitmap))
  2297. continue;
  2298. vlan->flags = 0;
  2299. if (ofdpa_vlan_id_is_internal(htons(vid)))
  2300. vlan->flags |= BRIDGE_VLAN_INFO_PVID;
  2301. vlan->vid_begin = vlan->vid_end = vid;
  2302. err = cb(&vlan->obj);
  2303. if (err)
  2304. break;
  2305. }
  2306. return err;
  2307. }
  2308. static int ofdpa_port_obj_fib4_add(struct rocker_port *rocker_port,
  2309. const struct switchdev_obj_ipv4_fib *fib4,
  2310. struct switchdev_trans *trans)
  2311. {
  2312. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2313. return ofdpa_port_fib_ipv4(ofdpa_port, trans,
  2314. htonl(fib4->dst), fib4->dst_len,
  2315. fib4->fi, fib4->tb_id, 0);
  2316. }
  2317. static int ofdpa_port_obj_fib4_del(struct rocker_port *rocker_port,
  2318. const struct switchdev_obj_ipv4_fib *fib4)
  2319. {
  2320. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2321. return ofdpa_port_fib_ipv4(ofdpa_port, NULL,
  2322. htonl(fib4->dst), fib4->dst_len,
  2323. fib4->fi, fib4->tb_id,
  2324. OFDPA_OP_FLAG_REMOVE);
  2325. }
  2326. static int ofdpa_port_obj_fdb_add(struct rocker_port *rocker_port,
  2327. const struct switchdev_obj_port_fdb *fdb,
  2328. struct switchdev_trans *trans)
  2329. {
  2330. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2331. __be16 vlan_id = ofdpa_port_vid_to_vlan(ofdpa_port, fdb->vid, NULL);
  2332. if (!ofdpa_port_is_bridged(ofdpa_port))
  2333. return -EINVAL;
  2334. return ofdpa_port_fdb(ofdpa_port, trans, fdb->addr, vlan_id, 0);
  2335. }
  2336. static int ofdpa_port_obj_fdb_del(struct rocker_port *rocker_port,
  2337. const struct switchdev_obj_port_fdb *fdb)
  2338. {
  2339. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2340. __be16 vlan_id = ofdpa_port_vid_to_vlan(ofdpa_port, fdb->vid, NULL);
  2341. int flags = OFDPA_OP_FLAG_REMOVE;
  2342. if (!ofdpa_port_is_bridged(ofdpa_port))
  2343. return -EINVAL;
  2344. return ofdpa_port_fdb(ofdpa_port, NULL, fdb->addr, vlan_id, flags);
  2345. }
  2346. static int ofdpa_port_obj_fdb_dump(const struct rocker_port *rocker_port,
  2347. struct switchdev_obj_port_fdb *fdb,
  2348. switchdev_obj_dump_cb_t *cb)
  2349. {
  2350. const struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2351. struct ofdpa *ofdpa = ofdpa_port->ofdpa;
  2352. struct ofdpa_fdb_tbl_entry *found;
  2353. struct hlist_node *tmp;
  2354. unsigned long lock_flags;
  2355. int bkt;
  2356. int err = 0;
  2357. spin_lock_irqsave(&ofdpa->fdb_tbl_lock, lock_flags);
  2358. hash_for_each_safe(ofdpa->fdb_tbl, bkt, tmp, found, entry) {
  2359. if (found->key.ofdpa_port != ofdpa_port)
  2360. continue;
  2361. ether_addr_copy(fdb->addr, found->key.addr);
  2362. fdb->ndm_state = NUD_REACHABLE;
  2363. fdb->vid = ofdpa_port_vlan_to_vid(ofdpa_port,
  2364. found->key.vlan_id);
  2365. err = cb(&fdb->obj);
  2366. if (err)
  2367. break;
  2368. }
  2369. spin_unlock_irqrestore(&ofdpa->fdb_tbl_lock, lock_flags);
  2370. return err;
  2371. }
  2372. static int ofdpa_port_bridge_join(struct ofdpa_port *ofdpa_port,
  2373. struct net_device *bridge)
  2374. {
  2375. int err;
  2376. /* Port is joining bridge, so the internal VLAN for the
  2377. * port is going to change to the bridge internal VLAN.
  2378. * Let's remove untagged VLAN (vid=0) from port and
  2379. * re-add once internal VLAN has changed.
  2380. */
  2381. err = ofdpa_port_vlan_del(ofdpa_port, OFDPA_UNTAGGED_VID, 0);
  2382. if (err)
  2383. return err;
  2384. ofdpa_port_internal_vlan_id_put(ofdpa_port,
  2385. ofdpa_port->dev->ifindex);
  2386. ofdpa_port->internal_vlan_id =
  2387. ofdpa_port_internal_vlan_id_get(ofdpa_port, bridge->ifindex);
  2388. ofdpa_port->bridge_dev = bridge;
  2389. switchdev_port_fwd_mark_set(ofdpa_port->dev, bridge, true);
  2390. return ofdpa_port_vlan_add(ofdpa_port, NULL, OFDPA_UNTAGGED_VID, 0);
  2391. }
  2392. static int ofdpa_port_bridge_leave(struct ofdpa_port *ofdpa_port)
  2393. {
  2394. int err;
  2395. err = ofdpa_port_vlan_del(ofdpa_port, OFDPA_UNTAGGED_VID, 0);
  2396. if (err)
  2397. return err;
  2398. ofdpa_port_internal_vlan_id_put(ofdpa_port,
  2399. ofdpa_port->bridge_dev->ifindex);
  2400. ofdpa_port->internal_vlan_id =
  2401. ofdpa_port_internal_vlan_id_get(ofdpa_port,
  2402. ofdpa_port->dev->ifindex);
  2403. switchdev_port_fwd_mark_set(ofdpa_port->dev, ofdpa_port->bridge_dev,
  2404. false);
  2405. ofdpa_port->bridge_dev = NULL;
  2406. err = ofdpa_port_vlan_add(ofdpa_port, NULL, OFDPA_UNTAGGED_VID, 0);
  2407. if (err)
  2408. return err;
  2409. if (ofdpa_port->dev->flags & IFF_UP)
  2410. err = ofdpa_port_fwd_enable(ofdpa_port, 0);
  2411. return err;
  2412. }
  2413. static int ofdpa_port_ovs_changed(struct ofdpa_port *ofdpa_port,
  2414. struct net_device *master)
  2415. {
  2416. int err;
  2417. ofdpa_port->bridge_dev = master;
  2418. err = ofdpa_port_fwd_disable(ofdpa_port, 0);
  2419. if (err)
  2420. return err;
  2421. err = ofdpa_port_fwd_enable(ofdpa_port, 0);
  2422. return err;
  2423. }
  2424. static int ofdpa_port_master_linked(struct rocker_port *rocker_port,
  2425. struct net_device *master)
  2426. {
  2427. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2428. int err = 0;
  2429. if (netif_is_bridge_master(master))
  2430. err = ofdpa_port_bridge_join(ofdpa_port, master);
  2431. else if (netif_is_ovs_master(master))
  2432. err = ofdpa_port_ovs_changed(ofdpa_port, master);
  2433. return err;
  2434. }
  2435. static int ofdpa_port_master_unlinked(struct rocker_port *rocker_port,
  2436. struct net_device *master)
  2437. {
  2438. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2439. int err = 0;
  2440. if (ofdpa_port_is_bridged(ofdpa_port))
  2441. err = ofdpa_port_bridge_leave(ofdpa_port);
  2442. else if (ofdpa_port_is_ovsed(ofdpa_port))
  2443. err = ofdpa_port_ovs_changed(ofdpa_port, NULL);
  2444. return err;
  2445. }
  2446. static int ofdpa_port_neigh_update(struct rocker_port *rocker_port,
  2447. struct neighbour *n)
  2448. {
  2449. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2450. int flags = (n->nud_state & NUD_VALID ? 0 : OFDPA_OP_FLAG_REMOVE) |
  2451. OFDPA_OP_FLAG_NOWAIT;
  2452. __be32 ip_addr = *(__be32 *) n->primary_key;
  2453. return ofdpa_port_ipv4_neigh(ofdpa_port, NULL, flags, ip_addr, n->ha);
  2454. }
  2455. static int ofdpa_port_neigh_destroy(struct rocker_port *rocker_port,
  2456. struct neighbour *n)
  2457. {
  2458. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2459. int flags = OFDPA_OP_FLAG_REMOVE | OFDPA_OP_FLAG_NOWAIT;
  2460. __be32 ip_addr = *(__be32 *) n->primary_key;
  2461. return ofdpa_port_ipv4_neigh(ofdpa_port, NULL, flags, ip_addr, n->ha);
  2462. }
  2463. static int ofdpa_port_ev_mac_vlan_seen(struct rocker_port *rocker_port,
  2464. const unsigned char *addr,
  2465. __be16 vlan_id)
  2466. {
  2467. struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
  2468. int flags = OFDPA_OP_FLAG_NOWAIT | OFDPA_OP_FLAG_LEARNED;
  2469. if (ofdpa_port->stp_state != BR_STATE_LEARNING &&
  2470. ofdpa_port->stp_state != BR_STATE_FORWARDING)
  2471. return 0;
  2472. return ofdpa_port_fdb(ofdpa_port, NULL, addr, vlan_id, flags);
  2473. }
  2474. struct rocker_world_ops rocker_ofdpa_ops = {
  2475. .kind = "ofdpa",
  2476. .priv_size = sizeof(struct ofdpa),
  2477. .port_priv_size = sizeof(struct ofdpa_port),
  2478. .mode = ROCKER_PORT_MODE_OF_DPA,
  2479. .init = ofdpa_init,
  2480. .fini = ofdpa_fini,
  2481. .port_pre_init = ofdpa_port_pre_init,
  2482. .port_init = ofdpa_port_init,
  2483. .port_fini = ofdpa_port_fini,
  2484. .port_open = ofdpa_port_open,
  2485. .port_stop = ofdpa_port_stop,
  2486. .port_attr_stp_state_set = ofdpa_port_attr_stp_state_set,
  2487. .port_attr_bridge_flags_set = ofdpa_port_attr_bridge_flags_set,
  2488. .port_attr_bridge_flags_get = ofdpa_port_attr_bridge_flags_get,
  2489. .port_attr_bridge_ageing_time_set = ofdpa_port_attr_bridge_ageing_time_set,
  2490. .port_obj_vlan_add = ofdpa_port_obj_vlan_add,
  2491. .port_obj_vlan_del = ofdpa_port_obj_vlan_del,
  2492. .port_obj_vlan_dump = ofdpa_port_obj_vlan_dump,
  2493. .port_obj_fib4_add = ofdpa_port_obj_fib4_add,
  2494. .port_obj_fib4_del = ofdpa_port_obj_fib4_del,
  2495. .port_obj_fdb_add = ofdpa_port_obj_fdb_add,
  2496. .port_obj_fdb_del = ofdpa_port_obj_fdb_del,
  2497. .port_obj_fdb_dump = ofdpa_port_obj_fdb_dump,
  2498. .port_master_linked = ofdpa_port_master_linked,
  2499. .port_master_unlinked = ofdpa_port_master_unlinked,
  2500. .port_neigh_update = ofdpa_port_neigh_update,
  2501. .port_neigh_destroy = ofdpa_port_neigh_destroy,
  2502. .port_ev_mac_vlan_seen = ofdpa_port_ev_mac_vlan_seen,
  2503. };