rocker_ofdpa.c 75 KB

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