ice_tc_lib.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2019-2021, Intel Corporation. */
  3. #include "ice.h"
  4. #include "ice_tc_lib.h"
  5. #include "ice_fltr.h"
  6. #include "ice_lib.h"
  7. #include "ice_protocol_type.h"
  8. #define ICE_TC_METADATA_LKUP_IDX 0
  9. /**
  10. * ice_tc_count_lkups - determine lookup count for switch filter
  11. * @flags: TC-flower flags
  12. * @headers: Pointer to TC flower filter header structure
  13. * @fltr: Pointer to outer TC filter structure
  14. *
  15. * Determine lookup count based on TC flower input for switch filter.
  16. */
  17. static int
  18. ice_tc_count_lkups(u32 flags, struct ice_tc_flower_lyr_2_4_hdrs *headers,
  19. struct ice_tc_flower_fltr *fltr)
  20. {
  21. int lkups_cnt = 1; /* 0th lookup is metadata */
  22. /* Always add metadata as the 0th lookup. Included elements:
  23. * - Direction flag (always present)
  24. * - ICE_TC_FLWR_FIELD_VLAN_TPID (present if specified)
  25. * - Tunnel flag (present if tunnel)
  26. */
  27. if (fltr->direction == ICE_ESWITCH_FLTR_EGRESS)
  28. lkups_cnt++;
  29. if (flags & ICE_TC_FLWR_FIELD_TENANT_ID)
  30. lkups_cnt++;
  31. if (flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC)
  32. lkups_cnt++;
  33. if (flags & ICE_TC_FLWR_FIELD_GTP_OPTS)
  34. lkups_cnt++;
  35. if (flags & ICE_TC_FLWR_FIELD_PFCP_OPTS)
  36. lkups_cnt++;
  37. if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
  38. ICE_TC_FLWR_FIELD_ENC_DEST_IPV4 |
  39. ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
  40. ICE_TC_FLWR_FIELD_ENC_DEST_IPV6))
  41. lkups_cnt++;
  42. if (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
  43. ICE_TC_FLWR_FIELD_ENC_IP_TTL))
  44. lkups_cnt++;
  45. if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT)
  46. lkups_cnt++;
  47. if (flags & ICE_TC_FLWR_FIELD_ETH_TYPE_ID)
  48. lkups_cnt++;
  49. /* are MAC fields specified? */
  50. if (flags & (ICE_TC_FLWR_FIELD_DST_MAC | ICE_TC_FLWR_FIELD_SRC_MAC))
  51. lkups_cnt++;
  52. /* is VLAN specified? */
  53. if (flags & (ICE_TC_FLWR_FIELD_VLAN | ICE_TC_FLWR_FIELD_VLAN_PRIO))
  54. lkups_cnt++;
  55. /* is CVLAN specified? */
  56. if (flags & (ICE_TC_FLWR_FIELD_CVLAN | ICE_TC_FLWR_FIELD_CVLAN_PRIO))
  57. lkups_cnt++;
  58. /* are PPPoE options specified? */
  59. if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
  60. ICE_TC_FLWR_FIELD_PPP_PROTO))
  61. lkups_cnt++;
  62. /* are IPv[4|6] fields specified? */
  63. if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 | ICE_TC_FLWR_FIELD_SRC_IPV4 |
  64. ICE_TC_FLWR_FIELD_DEST_IPV6 | ICE_TC_FLWR_FIELD_SRC_IPV6))
  65. lkups_cnt++;
  66. if (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))
  67. lkups_cnt++;
  68. /* are L2TPv3 options specified? */
  69. if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID)
  70. lkups_cnt++;
  71. /* is L4 (TCP/UDP/any other L4 protocol fields) specified? */
  72. if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
  73. ICE_TC_FLWR_FIELD_SRC_L4_PORT))
  74. lkups_cnt++;
  75. return lkups_cnt;
  76. }
  77. static enum ice_protocol_type ice_proto_type_from_mac(bool inner)
  78. {
  79. return inner ? ICE_MAC_IL : ICE_MAC_OFOS;
  80. }
  81. static enum ice_protocol_type ice_proto_type_from_etype(bool inner)
  82. {
  83. return inner ? ICE_ETYPE_IL : ICE_ETYPE_OL;
  84. }
  85. static enum ice_protocol_type ice_proto_type_from_ipv4(bool inner)
  86. {
  87. return inner ? ICE_IPV4_IL : ICE_IPV4_OFOS;
  88. }
  89. static enum ice_protocol_type ice_proto_type_from_ipv6(bool inner)
  90. {
  91. return inner ? ICE_IPV6_IL : ICE_IPV6_OFOS;
  92. }
  93. static enum ice_protocol_type ice_proto_type_from_l4_port(u16 ip_proto)
  94. {
  95. switch (ip_proto) {
  96. case IPPROTO_TCP:
  97. return ICE_TCP_IL;
  98. case IPPROTO_UDP:
  99. return ICE_UDP_ILOS;
  100. }
  101. return 0;
  102. }
  103. static enum ice_protocol_type
  104. ice_proto_type_from_tunnel(enum ice_tunnel_type type)
  105. {
  106. switch (type) {
  107. case TNL_VXLAN:
  108. return ICE_VXLAN;
  109. case TNL_GENEVE:
  110. return ICE_GENEVE;
  111. case TNL_GRETAP:
  112. return ICE_NVGRE;
  113. case TNL_GTPU:
  114. /* NO_PAY profiles will not work with GTP-U */
  115. return ICE_GTP;
  116. case TNL_GTPC:
  117. return ICE_GTP_NO_PAY;
  118. case TNL_PFCP:
  119. return ICE_PFCP;
  120. default:
  121. return 0;
  122. }
  123. }
  124. static enum ice_sw_tunnel_type
  125. ice_sw_type_from_tunnel(enum ice_tunnel_type type)
  126. {
  127. switch (type) {
  128. case TNL_VXLAN:
  129. return ICE_SW_TUN_VXLAN;
  130. case TNL_GENEVE:
  131. return ICE_SW_TUN_GENEVE;
  132. case TNL_GRETAP:
  133. return ICE_SW_TUN_NVGRE;
  134. case TNL_GTPU:
  135. return ICE_SW_TUN_GTPU;
  136. case TNL_GTPC:
  137. return ICE_SW_TUN_GTPC;
  138. case TNL_PFCP:
  139. return ICE_SW_TUN_PFCP;
  140. default:
  141. return ICE_NON_TUN;
  142. }
  143. }
  144. static u16 ice_check_supported_vlan_tpid(u16 vlan_tpid)
  145. {
  146. switch (vlan_tpid) {
  147. case ETH_P_8021Q:
  148. case ETH_P_8021AD:
  149. case ETH_P_QINQ1:
  150. return vlan_tpid;
  151. default:
  152. return 0;
  153. }
  154. }
  155. static int
  156. ice_tc_fill_tunnel_outer(u32 flags, struct ice_tc_flower_fltr *fltr,
  157. struct ice_adv_lkup_elem *list, int i)
  158. {
  159. struct ice_tc_flower_lyr_2_4_hdrs *hdr = &fltr->outer_headers;
  160. if (flags & ICE_TC_FLWR_FIELD_TENANT_ID) {
  161. u32 tenant_id;
  162. list[i].type = ice_proto_type_from_tunnel(fltr->tunnel_type);
  163. switch (fltr->tunnel_type) {
  164. case TNL_VXLAN:
  165. case TNL_GENEVE:
  166. tenant_id = be32_to_cpu(fltr->tenant_id) << 8;
  167. list[i].h_u.tnl_hdr.vni = cpu_to_be32(tenant_id);
  168. memcpy(&list[i].m_u.tnl_hdr.vni, "\xff\xff\xff\x00", 4);
  169. i++;
  170. break;
  171. case TNL_GRETAP:
  172. list[i].h_u.nvgre_hdr.tni_flow = fltr->tenant_id;
  173. memcpy(&list[i].m_u.nvgre_hdr.tni_flow,
  174. "\xff\xff\xff\xff", 4);
  175. i++;
  176. break;
  177. case TNL_GTPC:
  178. case TNL_GTPU:
  179. list[i].h_u.gtp_hdr.teid = fltr->tenant_id;
  180. memcpy(&list[i].m_u.gtp_hdr.teid,
  181. "\xff\xff\xff\xff", 4);
  182. i++;
  183. break;
  184. default:
  185. break;
  186. }
  187. }
  188. if (flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC) {
  189. list[i].type = ice_proto_type_from_mac(false);
  190. ether_addr_copy(list[i].h_u.eth_hdr.dst_addr,
  191. hdr->l2_key.dst_mac);
  192. ether_addr_copy(list[i].m_u.eth_hdr.dst_addr,
  193. hdr->l2_mask.dst_mac);
  194. i++;
  195. }
  196. if (flags & ICE_TC_FLWR_FIELD_GTP_OPTS) {
  197. list[i].type = ice_proto_type_from_tunnel(fltr->tunnel_type);
  198. if (fltr->gtp_pdu_info_masks.pdu_type) {
  199. list[i].h_u.gtp_hdr.pdu_type =
  200. fltr->gtp_pdu_info_keys.pdu_type << 4;
  201. memcpy(&list[i].m_u.gtp_hdr.pdu_type, "\xf0", 1);
  202. }
  203. if (fltr->gtp_pdu_info_masks.qfi) {
  204. list[i].h_u.gtp_hdr.qfi = fltr->gtp_pdu_info_keys.qfi;
  205. memcpy(&list[i].m_u.gtp_hdr.qfi, "\x3f", 1);
  206. }
  207. i++;
  208. }
  209. if (flags & ICE_TC_FLWR_FIELD_PFCP_OPTS) {
  210. struct ice_pfcp_hdr *hdr_h, *hdr_m;
  211. hdr_h = &list[i].h_u.pfcp_hdr;
  212. hdr_m = &list[i].m_u.pfcp_hdr;
  213. list[i].type = ICE_PFCP;
  214. hdr_h->flags = fltr->pfcp_meta_keys.type;
  215. hdr_m->flags = fltr->pfcp_meta_masks.type & 0x01;
  216. hdr_h->seid = fltr->pfcp_meta_keys.seid;
  217. hdr_m->seid = fltr->pfcp_meta_masks.seid;
  218. i++;
  219. }
  220. if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
  221. ICE_TC_FLWR_FIELD_ENC_DEST_IPV4)) {
  222. list[i].type = ice_proto_type_from_ipv4(false);
  223. if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_IPV4) {
  224. list[i].h_u.ipv4_hdr.src_addr = hdr->l3_key.src_ipv4;
  225. list[i].m_u.ipv4_hdr.src_addr = hdr->l3_mask.src_ipv4;
  226. }
  227. if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_IPV4) {
  228. list[i].h_u.ipv4_hdr.dst_addr = hdr->l3_key.dst_ipv4;
  229. list[i].m_u.ipv4_hdr.dst_addr = hdr->l3_mask.dst_ipv4;
  230. }
  231. i++;
  232. }
  233. if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
  234. ICE_TC_FLWR_FIELD_ENC_DEST_IPV6)) {
  235. list[i].type = ice_proto_type_from_ipv6(false);
  236. if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_IPV6) {
  237. memcpy(&list[i].h_u.ipv6_hdr.src_addr,
  238. &hdr->l3_key.src_ipv6_addr,
  239. sizeof(hdr->l3_key.src_ipv6_addr));
  240. memcpy(&list[i].m_u.ipv6_hdr.src_addr,
  241. &hdr->l3_mask.src_ipv6_addr,
  242. sizeof(hdr->l3_mask.src_ipv6_addr));
  243. }
  244. if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_IPV6) {
  245. memcpy(&list[i].h_u.ipv6_hdr.dst_addr,
  246. &hdr->l3_key.dst_ipv6_addr,
  247. sizeof(hdr->l3_key.dst_ipv6_addr));
  248. memcpy(&list[i].m_u.ipv6_hdr.dst_addr,
  249. &hdr->l3_mask.dst_ipv6_addr,
  250. sizeof(hdr->l3_mask.dst_ipv6_addr));
  251. }
  252. i++;
  253. }
  254. if (fltr->inner_headers.l2_key.n_proto == htons(ETH_P_IP) &&
  255. (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
  256. ICE_TC_FLWR_FIELD_ENC_IP_TTL))) {
  257. list[i].type = ice_proto_type_from_ipv4(false);
  258. if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TOS) {
  259. list[i].h_u.ipv4_hdr.tos = hdr->l3_key.tos;
  260. list[i].m_u.ipv4_hdr.tos = hdr->l3_mask.tos;
  261. }
  262. if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TTL) {
  263. list[i].h_u.ipv4_hdr.time_to_live = hdr->l3_key.ttl;
  264. list[i].m_u.ipv4_hdr.time_to_live = hdr->l3_mask.ttl;
  265. }
  266. i++;
  267. }
  268. if (fltr->inner_headers.l2_key.n_proto == htons(ETH_P_IPV6) &&
  269. (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
  270. ICE_TC_FLWR_FIELD_ENC_IP_TTL))) {
  271. struct ice_ipv6_hdr *hdr_h, *hdr_m;
  272. hdr_h = &list[i].h_u.ipv6_hdr;
  273. hdr_m = &list[i].m_u.ipv6_hdr;
  274. list[i].type = ice_proto_type_from_ipv6(false);
  275. if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TOS) {
  276. be32p_replace_bits(&hdr_h->be_ver_tc_flow,
  277. hdr->l3_key.tos,
  278. ICE_IPV6_HDR_TC_MASK);
  279. be32p_replace_bits(&hdr_m->be_ver_tc_flow,
  280. hdr->l3_mask.tos,
  281. ICE_IPV6_HDR_TC_MASK);
  282. }
  283. if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TTL) {
  284. hdr_h->hop_limit = hdr->l3_key.ttl;
  285. hdr_m->hop_limit = hdr->l3_mask.ttl;
  286. }
  287. i++;
  288. }
  289. if ((flags & ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT) &&
  290. hdr->l3_key.ip_proto == IPPROTO_UDP) {
  291. list[i].type = ICE_UDP_OF;
  292. list[i].h_u.l4_hdr.dst_port = hdr->l4_key.dst_port;
  293. list[i].m_u.l4_hdr.dst_port = hdr->l4_mask.dst_port;
  294. i++;
  295. }
  296. /* always fill matching on tunneled packets in metadata */
  297. ice_rule_add_tunnel_metadata(&list[ICE_TC_METADATA_LKUP_IDX]);
  298. return i;
  299. }
  300. /**
  301. * ice_tc_fill_rules - fill filter rules based on TC fltr
  302. * @hw: pointer to HW structure
  303. * @flags: tc flower field flags
  304. * @tc_fltr: pointer to TC flower filter
  305. * @list: list of advance rule elements
  306. * @rule_info: pointer to information about rule
  307. * @l4_proto: pointer to information such as L4 proto type
  308. *
  309. * Fill ice_adv_lkup_elem list based on TC flower flags and
  310. * TC flower headers. This list should be used to add
  311. * advance filter in hardware.
  312. */
  313. static int
  314. ice_tc_fill_rules(struct ice_hw *hw, u32 flags,
  315. struct ice_tc_flower_fltr *tc_fltr,
  316. struct ice_adv_lkup_elem *list,
  317. struct ice_adv_rule_info *rule_info,
  318. u16 *l4_proto)
  319. {
  320. struct ice_tc_flower_lyr_2_4_hdrs *headers = &tc_fltr->outer_headers;
  321. bool inner = false;
  322. u16 vlan_tpid = 0;
  323. int i = 1; /* 0th lookup is metadata */
  324. rule_info->vlan_type = vlan_tpid;
  325. /* Always add direction metadata */
  326. ice_rule_add_direction_metadata(&list[ICE_TC_METADATA_LKUP_IDX]);
  327. if (tc_fltr->direction == ICE_ESWITCH_FLTR_EGRESS) {
  328. ice_rule_add_src_vsi_metadata(&list[i]);
  329. i++;
  330. }
  331. rule_info->tun_type = ice_sw_type_from_tunnel(tc_fltr->tunnel_type);
  332. if (tc_fltr->tunnel_type != TNL_LAST) {
  333. i = ice_tc_fill_tunnel_outer(flags, tc_fltr, list, i);
  334. /* PFCP is considered non-tunneled - don't swap headers. */
  335. if (tc_fltr->tunnel_type != TNL_PFCP) {
  336. headers = &tc_fltr->inner_headers;
  337. inner = true;
  338. }
  339. }
  340. if (flags & ICE_TC_FLWR_FIELD_ETH_TYPE_ID) {
  341. list[i].type = ice_proto_type_from_etype(inner);
  342. list[i].h_u.ethertype.ethtype_id = headers->l2_key.n_proto;
  343. list[i].m_u.ethertype.ethtype_id = headers->l2_mask.n_proto;
  344. i++;
  345. }
  346. if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
  347. ICE_TC_FLWR_FIELD_SRC_MAC)) {
  348. struct ice_tc_l2_hdr *l2_key, *l2_mask;
  349. l2_key = &headers->l2_key;
  350. l2_mask = &headers->l2_mask;
  351. list[i].type = ice_proto_type_from_mac(inner);
  352. if (flags & ICE_TC_FLWR_FIELD_DST_MAC) {
  353. ether_addr_copy(list[i].h_u.eth_hdr.dst_addr,
  354. l2_key->dst_mac);
  355. ether_addr_copy(list[i].m_u.eth_hdr.dst_addr,
  356. l2_mask->dst_mac);
  357. }
  358. if (flags & ICE_TC_FLWR_FIELD_SRC_MAC) {
  359. ether_addr_copy(list[i].h_u.eth_hdr.src_addr,
  360. l2_key->src_mac);
  361. ether_addr_copy(list[i].m_u.eth_hdr.src_addr,
  362. l2_mask->src_mac);
  363. }
  364. i++;
  365. }
  366. /* copy VLAN info */
  367. if (flags & (ICE_TC_FLWR_FIELD_VLAN | ICE_TC_FLWR_FIELD_VLAN_PRIO)) {
  368. if (flags & ICE_TC_FLWR_FIELD_CVLAN)
  369. list[i].type = ICE_VLAN_EX;
  370. else
  371. list[i].type = ICE_VLAN_OFOS;
  372. if (flags & ICE_TC_FLWR_FIELD_VLAN) {
  373. list[i].h_u.vlan_hdr.vlan = headers->vlan_hdr.vlan_id;
  374. list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0x0FFF);
  375. }
  376. if (flags & ICE_TC_FLWR_FIELD_VLAN_PRIO) {
  377. if (flags & ICE_TC_FLWR_FIELD_VLAN) {
  378. list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xEFFF);
  379. } else {
  380. list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xE000);
  381. list[i].h_u.vlan_hdr.vlan = 0;
  382. }
  383. list[i].h_u.vlan_hdr.vlan |=
  384. headers->vlan_hdr.vlan_prio;
  385. }
  386. i++;
  387. }
  388. if (flags & ICE_TC_FLWR_FIELD_VLAN_TPID) {
  389. vlan_tpid = be16_to_cpu(headers->vlan_hdr.vlan_tpid);
  390. rule_info->vlan_type =
  391. ice_check_supported_vlan_tpid(vlan_tpid);
  392. ice_rule_add_vlan_metadata(&list[ICE_TC_METADATA_LKUP_IDX]);
  393. }
  394. if (flags & (ICE_TC_FLWR_FIELD_CVLAN | ICE_TC_FLWR_FIELD_CVLAN_PRIO)) {
  395. list[i].type = ICE_VLAN_IN;
  396. if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
  397. list[i].h_u.vlan_hdr.vlan = headers->cvlan_hdr.vlan_id;
  398. list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0x0FFF);
  399. }
  400. if (flags & ICE_TC_FLWR_FIELD_CVLAN_PRIO) {
  401. if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
  402. list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xEFFF);
  403. } else {
  404. list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xE000);
  405. list[i].h_u.vlan_hdr.vlan = 0;
  406. }
  407. list[i].h_u.vlan_hdr.vlan |=
  408. headers->cvlan_hdr.vlan_prio;
  409. }
  410. i++;
  411. }
  412. if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
  413. ICE_TC_FLWR_FIELD_PPP_PROTO)) {
  414. struct ice_pppoe_hdr *vals, *masks;
  415. vals = &list[i].h_u.pppoe_hdr;
  416. masks = &list[i].m_u.pppoe_hdr;
  417. list[i].type = ICE_PPPOE;
  418. if (flags & ICE_TC_FLWR_FIELD_PPPOE_SESSID) {
  419. vals->session_id = headers->pppoe_hdr.session_id;
  420. masks->session_id = cpu_to_be16(0xFFFF);
  421. }
  422. if (flags & ICE_TC_FLWR_FIELD_PPP_PROTO) {
  423. vals->ppp_prot_id = headers->pppoe_hdr.ppp_proto;
  424. masks->ppp_prot_id = cpu_to_be16(0xFFFF);
  425. }
  426. i++;
  427. }
  428. /* copy L3 (IPv[4|6]: src, dest) address */
  429. if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 |
  430. ICE_TC_FLWR_FIELD_SRC_IPV4)) {
  431. struct ice_tc_l3_hdr *l3_key, *l3_mask;
  432. list[i].type = ice_proto_type_from_ipv4(inner);
  433. l3_key = &headers->l3_key;
  434. l3_mask = &headers->l3_mask;
  435. if (flags & ICE_TC_FLWR_FIELD_DEST_IPV4) {
  436. list[i].h_u.ipv4_hdr.dst_addr = l3_key->dst_ipv4;
  437. list[i].m_u.ipv4_hdr.dst_addr = l3_mask->dst_ipv4;
  438. }
  439. if (flags & ICE_TC_FLWR_FIELD_SRC_IPV4) {
  440. list[i].h_u.ipv4_hdr.src_addr = l3_key->src_ipv4;
  441. list[i].m_u.ipv4_hdr.src_addr = l3_mask->src_ipv4;
  442. }
  443. i++;
  444. } else if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV6 |
  445. ICE_TC_FLWR_FIELD_SRC_IPV6)) {
  446. struct ice_ipv6_hdr *ipv6_hdr, *ipv6_mask;
  447. struct ice_tc_l3_hdr *l3_key, *l3_mask;
  448. list[i].type = ice_proto_type_from_ipv6(inner);
  449. ipv6_hdr = &list[i].h_u.ipv6_hdr;
  450. ipv6_mask = &list[i].m_u.ipv6_hdr;
  451. l3_key = &headers->l3_key;
  452. l3_mask = &headers->l3_mask;
  453. if (flags & ICE_TC_FLWR_FIELD_DEST_IPV6) {
  454. memcpy(&ipv6_hdr->dst_addr, &l3_key->dst_ipv6_addr,
  455. sizeof(l3_key->dst_ipv6_addr));
  456. memcpy(&ipv6_mask->dst_addr, &l3_mask->dst_ipv6_addr,
  457. sizeof(l3_mask->dst_ipv6_addr));
  458. }
  459. if (flags & ICE_TC_FLWR_FIELD_SRC_IPV6) {
  460. memcpy(&ipv6_hdr->src_addr, &l3_key->src_ipv6_addr,
  461. sizeof(l3_key->src_ipv6_addr));
  462. memcpy(&ipv6_mask->src_addr, &l3_mask->src_ipv6_addr,
  463. sizeof(l3_mask->src_ipv6_addr));
  464. }
  465. i++;
  466. }
  467. if (headers->l2_key.n_proto == htons(ETH_P_IP) &&
  468. (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
  469. list[i].type = ice_proto_type_from_ipv4(inner);
  470. if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
  471. list[i].h_u.ipv4_hdr.tos = headers->l3_key.tos;
  472. list[i].m_u.ipv4_hdr.tos = headers->l3_mask.tos;
  473. }
  474. if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
  475. list[i].h_u.ipv4_hdr.time_to_live =
  476. headers->l3_key.ttl;
  477. list[i].m_u.ipv4_hdr.time_to_live =
  478. headers->l3_mask.ttl;
  479. }
  480. i++;
  481. }
  482. if (headers->l2_key.n_proto == htons(ETH_P_IPV6) &&
  483. (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
  484. struct ice_ipv6_hdr *hdr_h, *hdr_m;
  485. hdr_h = &list[i].h_u.ipv6_hdr;
  486. hdr_m = &list[i].m_u.ipv6_hdr;
  487. list[i].type = ice_proto_type_from_ipv6(inner);
  488. if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
  489. be32p_replace_bits(&hdr_h->be_ver_tc_flow,
  490. headers->l3_key.tos,
  491. ICE_IPV6_HDR_TC_MASK);
  492. be32p_replace_bits(&hdr_m->be_ver_tc_flow,
  493. headers->l3_mask.tos,
  494. ICE_IPV6_HDR_TC_MASK);
  495. }
  496. if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
  497. hdr_h->hop_limit = headers->l3_key.ttl;
  498. hdr_m->hop_limit = headers->l3_mask.ttl;
  499. }
  500. i++;
  501. }
  502. if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID) {
  503. list[i].type = ICE_L2TPV3;
  504. list[i].h_u.l2tpv3_sess_hdr.session_id =
  505. headers->l2tpv3_hdr.session_id;
  506. list[i].m_u.l2tpv3_sess_hdr.session_id =
  507. cpu_to_be32(0xFFFFFFFF);
  508. i++;
  509. }
  510. /* copy L4 (src, dest) port */
  511. if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
  512. ICE_TC_FLWR_FIELD_SRC_L4_PORT)) {
  513. struct ice_tc_l4_hdr *l4_key, *l4_mask;
  514. list[i].type = ice_proto_type_from_l4_port(headers->l3_key.ip_proto);
  515. l4_key = &headers->l4_key;
  516. l4_mask = &headers->l4_mask;
  517. if (flags & ICE_TC_FLWR_FIELD_DEST_L4_PORT) {
  518. list[i].h_u.l4_hdr.dst_port = l4_key->dst_port;
  519. list[i].m_u.l4_hdr.dst_port = l4_mask->dst_port;
  520. }
  521. if (flags & ICE_TC_FLWR_FIELD_SRC_L4_PORT) {
  522. list[i].h_u.l4_hdr.src_port = l4_key->src_port;
  523. list[i].m_u.l4_hdr.src_port = l4_mask->src_port;
  524. }
  525. i++;
  526. }
  527. return i;
  528. }
  529. /**
  530. * ice_tc_tun_get_type - get the tunnel type
  531. * @tunnel_dev: ptr to tunnel device
  532. *
  533. * This function detects appropriate tunnel_type if specified device is
  534. * tunnel device such as VXLAN/Geneve
  535. */
  536. static int ice_tc_tun_get_type(struct net_device *tunnel_dev)
  537. {
  538. if (netif_is_vxlan(tunnel_dev))
  539. return TNL_VXLAN;
  540. if (netif_is_geneve(tunnel_dev))
  541. return TNL_GENEVE;
  542. if (netif_is_gretap(tunnel_dev) ||
  543. netif_is_ip6gretap(tunnel_dev))
  544. return TNL_GRETAP;
  545. /* Assume GTP-U by default in case of GTP netdev.
  546. * GTP-C may be selected later, based on enc_dst_port.
  547. */
  548. if (netif_is_gtp(tunnel_dev))
  549. return TNL_GTPU;
  550. if (netif_is_pfcp(tunnel_dev))
  551. return TNL_PFCP;
  552. return TNL_LAST;
  553. }
  554. bool ice_is_tunnel_supported(struct net_device *dev)
  555. {
  556. return ice_tc_tun_get_type(dev) != TNL_LAST;
  557. }
  558. static bool ice_tc_is_dev_uplink(struct net_device *dev)
  559. {
  560. return netif_is_ice(dev) || ice_is_tunnel_supported(dev);
  561. }
  562. static int ice_tc_setup_action(struct net_device *filter_dev,
  563. struct ice_tc_flower_fltr *fltr,
  564. struct net_device *target_dev,
  565. enum ice_sw_fwd_act_type action)
  566. {
  567. struct ice_repr *repr;
  568. if (action != ICE_FWD_TO_VSI && action != ICE_MIRROR_PACKET) {
  569. NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported action to setup provided");
  570. return -EINVAL;
  571. }
  572. fltr->action.fltr_act = action;
  573. if (ice_is_port_repr_netdev(filter_dev) &&
  574. ice_is_port_repr_netdev(target_dev)) {
  575. repr = ice_netdev_to_repr(target_dev);
  576. fltr->dest_vsi = repr->src_vsi;
  577. fltr->direction = ICE_ESWITCH_FLTR_EGRESS;
  578. } else if (ice_is_port_repr_netdev(filter_dev) &&
  579. ice_tc_is_dev_uplink(target_dev)) {
  580. repr = ice_netdev_to_repr(filter_dev);
  581. fltr->dest_vsi = repr->src_vsi->back->eswitch.uplink_vsi;
  582. fltr->direction = ICE_ESWITCH_FLTR_EGRESS;
  583. } else if (ice_tc_is_dev_uplink(filter_dev) &&
  584. ice_is_port_repr_netdev(target_dev)) {
  585. repr = ice_netdev_to_repr(target_dev);
  586. fltr->dest_vsi = repr->src_vsi;
  587. fltr->direction = ICE_ESWITCH_FLTR_INGRESS;
  588. } else {
  589. NL_SET_ERR_MSG_MOD(fltr->extack,
  590. "Unsupported netdevice in switchdev mode");
  591. return -EINVAL;
  592. }
  593. return 0;
  594. }
  595. static int
  596. ice_tc_setup_drop_action(struct net_device *filter_dev,
  597. struct ice_tc_flower_fltr *fltr)
  598. {
  599. fltr->action.fltr_act = ICE_DROP_PACKET;
  600. if (ice_is_port_repr_netdev(filter_dev)) {
  601. fltr->direction = ICE_ESWITCH_FLTR_EGRESS;
  602. } else if (ice_tc_is_dev_uplink(filter_dev)) {
  603. fltr->direction = ICE_ESWITCH_FLTR_INGRESS;
  604. } else {
  605. NL_SET_ERR_MSG_MOD(fltr->extack,
  606. "Unsupported netdevice in switchdev mode");
  607. return -EINVAL;
  608. }
  609. return 0;
  610. }
  611. static int ice_eswitch_tc_parse_action(struct net_device *filter_dev,
  612. struct ice_tc_flower_fltr *fltr,
  613. struct flow_action_entry *act)
  614. {
  615. int err;
  616. switch (act->id) {
  617. case FLOW_ACTION_DROP:
  618. err = ice_tc_setup_drop_action(filter_dev, fltr);
  619. if (err)
  620. return err;
  621. break;
  622. case FLOW_ACTION_REDIRECT:
  623. err = ice_tc_setup_action(filter_dev, fltr,
  624. act->dev, ICE_FWD_TO_VSI);
  625. if (err)
  626. return err;
  627. break;
  628. case FLOW_ACTION_MIRRED:
  629. err = ice_tc_setup_action(filter_dev, fltr,
  630. act->dev, ICE_MIRROR_PACKET);
  631. if (err)
  632. return err;
  633. break;
  634. default:
  635. NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported action in switchdev mode");
  636. return -EINVAL;
  637. }
  638. return 0;
  639. }
  640. static int
  641. ice_eswitch_add_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
  642. {
  643. struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
  644. struct ice_adv_rule_info rule_info = { 0 };
  645. struct ice_rule_query_data rule_added;
  646. struct ice_hw *hw = &vsi->back->hw;
  647. struct ice_adv_lkup_elem *list;
  648. u32 flags = fltr->flags;
  649. int lkups_cnt;
  650. int ret;
  651. int i;
  652. if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT) {
  653. NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported encap field(s)");
  654. return -EOPNOTSUPP;
  655. }
  656. lkups_cnt = ice_tc_count_lkups(flags, headers, fltr);
  657. list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
  658. if (!list)
  659. return -ENOMEM;
  660. i = ice_tc_fill_rules(hw, flags, fltr, list, &rule_info, NULL);
  661. if (i != lkups_cnt) {
  662. ret = -EINVAL;
  663. goto exit;
  664. }
  665. rule_info.sw_act.fltr_act = fltr->action.fltr_act;
  666. if (fltr->action.fltr_act != ICE_DROP_PACKET)
  667. rule_info.sw_act.vsi_handle = fltr->dest_vsi->idx;
  668. /* For now, making priority to be highest, and it also becomes
  669. * the priority for recipe which will get created as a result of
  670. * new extraction sequence based on input set.
  671. * Priority '7' is max val for switch recipe, higher the number
  672. * results into order of switch rule evaluation.
  673. */
  674. rule_info.priority = 7;
  675. rule_info.flags_info.act_valid = true;
  676. if (fltr->direction == ICE_ESWITCH_FLTR_INGRESS) {
  677. /* Uplink to VF */
  678. rule_info.sw_act.flag |= ICE_FLTR_RX;
  679. rule_info.sw_act.src = hw->pf_id;
  680. rule_info.flags_info.act = ICE_SINGLE_ACT_LB_ENABLE;
  681. } else if (fltr->direction == ICE_ESWITCH_FLTR_EGRESS &&
  682. fltr->dest_vsi == vsi->back->eswitch.uplink_vsi) {
  683. /* VF to Uplink */
  684. rule_info.sw_act.flag |= ICE_FLTR_TX;
  685. rule_info.sw_act.src = vsi->idx;
  686. rule_info.flags_info.act = ICE_SINGLE_ACT_LAN_ENABLE;
  687. /* This is a specific case. The destination VSI index is
  688. * overwritten by the source VSI index. This type of filter
  689. * should allow the packet to go to the LAN, not to the
  690. * VSI passed here. It should set LAN_EN bit only. However,
  691. * the VSI must be a valid one. Setting source VSI index
  692. * here is safe. Even if the result from switch is set LAN_EN
  693. * and LB_EN (which normally will pass the packet to this VSI)
  694. * packet won't be seen on the VSI, because local loopback is
  695. * turned off.
  696. */
  697. rule_info.sw_act.vsi_handle = vsi->idx;
  698. } else {
  699. /* VF to VF */
  700. rule_info.sw_act.flag |= ICE_FLTR_TX;
  701. rule_info.sw_act.src = vsi->idx;
  702. rule_info.flags_info.act = ICE_SINGLE_ACT_LB_ENABLE;
  703. }
  704. /* specify the cookie as filter_rule_id */
  705. rule_info.fltr_rule_id = fltr->cookie;
  706. rule_info.src_vsi = vsi->idx;
  707. ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
  708. if (ret == -EEXIST) {
  709. NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter because it already exist");
  710. ret = -EINVAL;
  711. goto exit;
  712. } else if (ret) {
  713. NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter due to error");
  714. goto exit;
  715. }
  716. /* store the output params, which are needed later for removing
  717. * advanced switch filter
  718. */
  719. fltr->rid = rule_added.rid;
  720. fltr->rule_id = rule_added.rule_id;
  721. fltr->dest_vsi_handle = rule_added.vsi_handle;
  722. exit:
  723. kfree(list);
  724. return ret;
  725. }
  726. /**
  727. * ice_locate_vsi_using_queue - locate VSI using queue (forward to queue action)
  728. * @vsi: Pointer to VSI
  729. * @queue: Queue index
  730. *
  731. * Locate the VSI using specified "queue". When ADQ is not enabled,
  732. * always return input VSI, otherwise locate corresponding
  733. * VSI based on per channel "offset" and "qcount"
  734. */
  735. struct ice_vsi *
  736. ice_locate_vsi_using_queue(struct ice_vsi *vsi, int queue)
  737. {
  738. int num_tc, tc;
  739. /* if ADQ is not active, passed VSI is the candidate VSI */
  740. if (!ice_is_adq_active(vsi->back))
  741. return vsi;
  742. /* Locate the VSI (it could still be main PF VSI or CHNL_VSI depending
  743. * upon queue number)
  744. */
  745. num_tc = vsi->mqprio_qopt.qopt.num_tc;
  746. for (tc = 0; tc < num_tc; tc++) {
  747. int qcount = vsi->mqprio_qopt.qopt.count[tc];
  748. int offset = vsi->mqprio_qopt.qopt.offset[tc];
  749. if (queue >= offset && queue < offset + qcount) {
  750. /* for non-ADQ TCs, passed VSI is the candidate VSI */
  751. if (tc < ICE_CHNL_START_TC)
  752. return vsi;
  753. else
  754. return vsi->tc_map_vsi[tc];
  755. }
  756. }
  757. return NULL;
  758. }
  759. static struct ice_rx_ring *
  760. ice_locate_rx_ring_using_queue(struct ice_vsi *vsi,
  761. struct ice_tc_flower_fltr *tc_fltr)
  762. {
  763. u16 queue = tc_fltr->action.fwd.q.queue;
  764. return queue < vsi->num_rxq ? vsi->rx_rings[queue] : NULL;
  765. }
  766. /**
  767. * ice_tc_forward_action - Determine destination VSI and queue for the action
  768. * @vsi: Pointer to VSI
  769. * @tc_fltr: Pointer to TC flower filter structure
  770. *
  771. * Validates the tc forward action and determines the destination VSI and queue
  772. * for the forward action.
  773. */
  774. static struct ice_vsi *
  775. ice_tc_forward_action(struct ice_vsi *vsi, struct ice_tc_flower_fltr *tc_fltr)
  776. {
  777. struct ice_rx_ring *ring = NULL;
  778. struct ice_vsi *dest_vsi = NULL;
  779. struct ice_pf *pf = vsi->back;
  780. struct device *dev;
  781. u32 tc_class;
  782. int q;
  783. dev = ice_pf_to_dev(pf);
  784. /* Get the destination VSI and/or destination queue and validate them */
  785. switch (tc_fltr->action.fltr_act) {
  786. case ICE_FWD_TO_VSI:
  787. tc_class = tc_fltr->action.fwd.tc.tc_class;
  788. /* Select the destination VSI */
  789. if (tc_class < ICE_CHNL_START_TC) {
  790. NL_SET_ERR_MSG_MOD(tc_fltr->extack,
  791. "Unable to add filter because of unsupported destination");
  792. return ERR_PTR(-EOPNOTSUPP);
  793. }
  794. /* Locate ADQ VSI depending on hw_tc number */
  795. dest_vsi = vsi->tc_map_vsi[tc_class];
  796. break;
  797. case ICE_FWD_TO_Q:
  798. /* Locate the Rx queue */
  799. ring = ice_locate_rx_ring_using_queue(vsi, tc_fltr);
  800. if (!ring) {
  801. dev_err(dev,
  802. "Unable to locate Rx queue for action fwd_to_queue: %u\n",
  803. tc_fltr->action.fwd.q.queue);
  804. return ERR_PTR(-EINVAL);
  805. }
  806. /* Determine destination VSI even though the action is
  807. * FWD_TO_QUEUE, because QUEUE is associated with VSI
  808. */
  809. q = tc_fltr->action.fwd.q.queue;
  810. dest_vsi = ice_locate_vsi_using_queue(vsi, q);
  811. break;
  812. default:
  813. dev_err(dev,
  814. "Unable to add filter because of unsupported action %u (supported actions: fwd to tc, fwd to queue)\n",
  815. tc_fltr->action.fltr_act);
  816. return ERR_PTR(-EINVAL);
  817. }
  818. /* Must have valid dest_vsi (it could be main VSI or ADQ VSI) */
  819. if (!dest_vsi) {
  820. dev_err(dev,
  821. "Unable to add filter because specified destination VSI doesn't exist\n");
  822. return ERR_PTR(-EINVAL);
  823. }
  824. return dest_vsi;
  825. }
  826. /**
  827. * ice_add_tc_flower_adv_fltr - add appropriate filter rules
  828. * @vsi: Pointer to VSI
  829. * @tc_fltr: Pointer to TC flower filter structure
  830. *
  831. * based on filter parameters using Advance recipes supported
  832. * by OS package.
  833. */
  834. static int
  835. ice_add_tc_flower_adv_fltr(struct ice_vsi *vsi,
  836. struct ice_tc_flower_fltr *tc_fltr)
  837. {
  838. struct ice_tc_flower_lyr_2_4_hdrs *headers = &tc_fltr->outer_headers;
  839. struct ice_adv_rule_info rule_info = {0};
  840. struct ice_rule_query_data rule_added;
  841. struct ice_adv_lkup_elem *list;
  842. struct ice_pf *pf = vsi->back;
  843. struct ice_hw *hw = &pf->hw;
  844. u32 flags = tc_fltr->flags;
  845. struct ice_vsi *dest_vsi;
  846. struct device *dev;
  847. u16 lkups_cnt = 0;
  848. u16 l4_proto = 0;
  849. int ret = 0;
  850. u16 i = 0;
  851. dev = ice_pf_to_dev(pf);
  852. if (ice_is_safe_mode(pf)) {
  853. NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unable to add filter because driver is in safe mode");
  854. return -EOPNOTSUPP;
  855. }
  856. if (!flags || (flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV4 |
  857. ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
  858. ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
  859. ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
  860. ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT))) {
  861. NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unsupported encap field(s)");
  862. return -EOPNOTSUPP;
  863. }
  864. /* validate forwarding action VSI and queue */
  865. if (ice_is_forward_action(tc_fltr->action.fltr_act)) {
  866. dest_vsi = ice_tc_forward_action(vsi, tc_fltr);
  867. if (IS_ERR(dest_vsi))
  868. return PTR_ERR(dest_vsi);
  869. }
  870. lkups_cnt = ice_tc_count_lkups(flags, headers, tc_fltr);
  871. list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
  872. if (!list)
  873. return -ENOMEM;
  874. i = ice_tc_fill_rules(hw, flags, tc_fltr, list, &rule_info, &l4_proto);
  875. if (i != lkups_cnt) {
  876. ret = -EINVAL;
  877. goto exit;
  878. }
  879. rule_info.sw_act.fltr_act = tc_fltr->action.fltr_act;
  880. /* specify the cookie as filter_rule_id */
  881. rule_info.fltr_rule_id = tc_fltr->cookie;
  882. switch (tc_fltr->action.fltr_act) {
  883. case ICE_FWD_TO_VSI:
  884. rule_info.sw_act.vsi_handle = dest_vsi->idx;
  885. rule_info.priority = ICE_SWITCH_FLTR_PRIO_VSI;
  886. rule_info.sw_act.src = hw->pf_id;
  887. dev_dbg(dev, "add switch rule for TC:%u vsi_idx:%u, lkups_cnt:%u\n",
  888. tc_fltr->action.fwd.tc.tc_class,
  889. rule_info.sw_act.vsi_handle, lkups_cnt);
  890. break;
  891. case ICE_FWD_TO_Q:
  892. /* HW queue number in global space */
  893. rule_info.sw_act.fwd_id.q_id = tc_fltr->action.fwd.q.hw_queue;
  894. rule_info.sw_act.vsi_handle = dest_vsi->idx;
  895. rule_info.priority = ICE_SWITCH_FLTR_PRIO_QUEUE;
  896. rule_info.sw_act.src = hw->pf_id;
  897. dev_dbg(dev, "add switch rule action to forward to queue:%u (HW queue %u), lkups_cnt:%u\n",
  898. tc_fltr->action.fwd.q.queue,
  899. tc_fltr->action.fwd.q.hw_queue, lkups_cnt);
  900. break;
  901. case ICE_DROP_PACKET:
  902. rule_info.sw_act.flag |= ICE_FLTR_RX;
  903. rule_info.sw_act.src = hw->pf_id;
  904. rule_info.priority = ICE_SWITCH_FLTR_PRIO_VSI;
  905. break;
  906. default:
  907. ret = -EOPNOTSUPP;
  908. goto exit;
  909. }
  910. ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
  911. if (ret == -EEXIST) {
  912. NL_SET_ERR_MSG_MOD(tc_fltr->extack,
  913. "Unable to add filter because it already exist");
  914. ret = -EINVAL;
  915. goto exit;
  916. } else if (ret) {
  917. NL_SET_ERR_MSG_MOD(tc_fltr->extack,
  918. "Unable to add filter due to error");
  919. goto exit;
  920. }
  921. /* store the output params, which are needed later for removing
  922. * advanced switch filter
  923. */
  924. tc_fltr->rid = rule_added.rid;
  925. tc_fltr->rule_id = rule_added.rule_id;
  926. tc_fltr->dest_vsi_handle = rule_added.vsi_handle;
  927. if (tc_fltr->action.fltr_act == ICE_FWD_TO_VSI ||
  928. tc_fltr->action.fltr_act == ICE_FWD_TO_Q) {
  929. tc_fltr->dest_vsi = dest_vsi;
  930. /* keep track of advanced switch filter for
  931. * destination VSI
  932. */
  933. dest_vsi->num_chnl_fltr++;
  934. /* keeps track of channel filters for PF VSI */
  935. if (vsi->type == ICE_VSI_PF &&
  936. (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
  937. ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
  938. pf->num_dmac_chnl_fltrs++;
  939. }
  940. switch (tc_fltr->action.fltr_act) {
  941. case ICE_FWD_TO_VSI:
  942. dev_dbg(dev, "added switch rule (lkups_cnt %u, flags 0x%x), action is forward to TC %u, rid %u, rule_id %u, vsi_idx %u\n",
  943. lkups_cnt, flags,
  944. tc_fltr->action.fwd.tc.tc_class, rule_added.rid,
  945. rule_added.rule_id, rule_added.vsi_handle);
  946. break;
  947. case ICE_FWD_TO_Q:
  948. dev_dbg(dev, "added switch rule (lkups_cnt %u, flags 0x%x), action is forward to queue: %u (HW queue %u) , rid %u, rule_id %u\n",
  949. lkups_cnt, flags, tc_fltr->action.fwd.q.queue,
  950. tc_fltr->action.fwd.q.hw_queue, rule_added.rid,
  951. rule_added.rule_id);
  952. break;
  953. case ICE_DROP_PACKET:
  954. dev_dbg(dev, "added switch rule (lkups_cnt %u, flags 0x%x), action is drop, rid %u, rule_id %u\n",
  955. lkups_cnt, flags, rule_added.rid, rule_added.rule_id);
  956. break;
  957. default:
  958. break;
  959. }
  960. exit:
  961. kfree(list);
  962. return ret;
  963. }
  964. /**
  965. * ice_tc_set_pppoe - Parse PPPoE fields from TC flower filter
  966. * @match: Pointer to flow match structure
  967. * @fltr: Pointer to filter structure
  968. * @headers: Pointer to outer header fields
  969. * @returns PPP protocol used in filter (ppp_ses or ppp_disc)
  970. */
  971. static u16
  972. ice_tc_set_pppoe(struct flow_match_pppoe *match,
  973. struct ice_tc_flower_fltr *fltr,
  974. struct ice_tc_flower_lyr_2_4_hdrs *headers)
  975. {
  976. if (match->mask->session_id) {
  977. fltr->flags |= ICE_TC_FLWR_FIELD_PPPOE_SESSID;
  978. headers->pppoe_hdr.session_id = match->key->session_id;
  979. }
  980. if (match->mask->ppp_proto) {
  981. fltr->flags |= ICE_TC_FLWR_FIELD_PPP_PROTO;
  982. headers->pppoe_hdr.ppp_proto = match->key->ppp_proto;
  983. }
  984. return be16_to_cpu(match->key->type);
  985. }
  986. /**
  987. * ice_tc_set_ipv4 - Parse IPv4 addresses from TC flower filter
  988. * @match: Pointer to flow match structure
  989. * @fltr: Pointer to filter structure
  990. * @headers: inner or outer header fields
  991. * @is_encap: set true for tunnel IPv4 address
  992. */
  993. static int
  994. ice_tc_set_ipv4(struct flow_match_ipv4_addrs *match,
  995. struct ice_tc_flower_fltr *fltr,
  996. struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
  997. {
  998. if (match->key->dst) {
  999. if (is_encap)
  1000. fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV4;
  1001. else
  1002. fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV4;
  1003. headers->l3_key.dst_ipv4 = match->key->dst;
  1004. headers->l3_mask.dst_ipv4 = match->mask->dst;
  1005. }
  1006. if (match->key->src) {
  1007. if (is_encap)
  1008. fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV4;
  1009. else
  1010. fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV4;
  1011. headers->l3_key.src_ipv4 = match->key->src;
  1012. headers->l3_mask.src_ipv4 = match->mask->src;
  1013. }
  1014. return 0;
  1015. }
  1016. /**
  1017. * ice_tc_set_ipv6 - Parse IPv6 addresses from TC flower filter
  1018. * @match: Pointer to flow match structure
  1019. * @fltr: Pointer to filter structure
  1020. * @headers: inner or outer header fields
  1021. * @is_encap: set true for tunnel IPv6 address
  1022. */
  1023. static int
  1024. ice_tc_set_ipv6(struct flow_match_ipv6_addrs *match,
  1025. struct ice_tc_flower_fltr *fltr,
  1026. struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
  1027. {
  1028. struct ice_tc_l3_hdr *l3_key, *l3_mask;
  1029. /* src and dest IPV6 address should not be LOOPBACK
  1030. * (0:0:0:0:0:0:0:1), which can be represented as ::1
  1031. */
  1032. if (ipv6_addr_loopback(&match->key->dst) ||
  1033. ipv6_addr_loopback(&match->key->src)) {
  1034. NL_SET_ERR_MSG_MOD(fltr->extack, "Bad IPv6, addr is LOOPBACK");
  1035. return -EINVAL;
  1036. }
  1037. /* if src/dest IPv6 address is *,* error */
  1038. if (ipv6_addr_any(&match->mask->dst) &&
  1039. ipv6_addr_any(&match->mask->src)) {
  1040. NL_SET_ERR_MSG_MOD(fltr->extack, "Bad src/dest IPv6, addr is any");
  1041. return -EINVAL;
  1042. }
  1043. if (!ipv6_addr_any(&match->mask->dst)) {
  1044. if (is_encap)
  1045. fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV6;
  1046. else
  1047. fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV6;
  1048. }
  1049. if (!ipv6_addr_any(&match->mask->src)) {
  1050. if (is_encap)
  1051. fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV6;
  1052. else
  1053. fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV6;
  1054. }
  1055. l3_key = &headers->l3_key;
  1056. l3_mask = &headers->l3_mask;
  1057. if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
  1058. ICE_TC_FLWR_FIELD_SRC_IPV6)) {
  1059. memcpy(&l3_key->src_ipv6_addr, &match->key->src.s6_addr,
  1060. sizeof(match->key->src.s6_addr));
  1061. memcpy(&l3_mask->src_ipv6_addr, &match->mask->src.s6_addr,
  1062. sizeof(match->mask->src.s6_addr));
  1063. }
  1064. if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
  1065. ICE_TC_FLWR_FIELD_DEST_IPV6)) {
  1066. memcpy(&l3_key->dst_ipv6_addr, &match->key->dst.s6_addr,
  1067. sizeof(match->key->dst.s6_addr));
  1068. memcpy(&l3_mask->dst_ipv6_addr, &match->mask->dst.s6_addr,
  1069. sizeof(match->mask->dst.s6_addr));
  1070. }
  1071. return 0;
  1072. }
  1073. /**
  1074. * ice_tc_set_tos_ttl - Parse IP ToS/TTL from TC flower filter
  1075. * @match: Pointer to flow match structure
  1076. * @fltr: Pointer to filter structure
  1077. * @headers: inner or outer header fields
  1078. * @is_encap: set true for tunnel
  1079. */
  1080. static void
  1081. ice_tc_set_tos_ttl(struct flow_match_ip *match,
  1082. struct ice_tc_flower_fltr *fltr,
  1083. struct ice_tc_flower_lyr_2_4_hdrs *headers,
  1084. bool is_encap)
  1085. {
  1086. if (match->mask->tos) {
  1087. if (is_encap)
  1088. fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TOS;
  1089. else
  1090. fltr->flags |= ICE_TC_FLWR_FIELD_IP_TOS;
  1091. headers->l3_key.tos = match->key->tos;
  1092. headers->l3_mask.tos = match->mask->tos;
  1093. }
  1094. if (match->mask->ttl) {
  1095. if (is_encap)
  1096. fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TTL;
  1097. else
  1098. fltr->flags |= ICE_TC_FLWR_FIELD_IP_TTL;
  1099. headers->l3_key.ttl = match->key->ttl;
  1100. headers->l3_mask.ttl = match->mask->ttl;
  1101. }
  1102. }
  1103. /**
  1104. * ice_tc_set_port - Parse ports from TC flower filter
  1105. * @match: Flow match structure
  1106. * @fltr: Pointer to filter structure
  1107. * @headers: inner or outer header fields
  1108. * @is_encap: set true for tunnel port
  1109. */
  1110. static int
  1111. ice_tc_set_port(struct flow_match_ports match,
  1112. struct ice_tc_flower_fltr *fltr,
  1113. struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
  1114. {
  1115. if (match.key->dst) {
  1116. if (is_encap)
  1117. fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT;
  1118. else
  1119. fltr->flags |= ICE_TC_FLWR_FIELD_DEST_L4_PORT;
  1120. headers->l4_key.dst_port = match.key->dst;
  1121. headers->l4_mask.dst_port = match.mask->dst;
  1122. }
  1123. if (match.key->src) {
  1124. if (is_encap)
  1125. fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT;
  1126. else
  1127. fltr->flags |= ICE_TC_FLWR_FIELD_SRC_L4_PORT;
  1128. headers->l4_key.src_port = match.key->src;
  1129. headers->l4_mask.src_port = match.mask->src;
  1130. }
  1131. return 0;
  1132. }
  1133. static struct net_device *
  1134. ice_get_tunnel_device(struct net_device *dev, struct flow_rule *rule)
  1135. {
  1136. struct flow_action_entry *act;
  1137. int i;
  1138. if (ice_is_tunnel_supported(dev))
  1139. return dev;
  1140. flow_action_for_each(i, act, &rule->action) {
  1141. if (act->id == FLOW_ACTION_REDIRECT &&
  1142. ice_is_tunnel_supported(act->dev))
  1143. return act->dev;
  1144. }
  1145. return NULL;
  1146. }
  1147. /**
  1148. * ice_parse_gtp_type - Sets GTP tunnel type to GTP-U or GTP-C
  1149. * @match: Flow match structure
  1150. * @fltr: Pointer to filter structure
  1151. *
  1152. * GTP-C/GTP-U is selected based on destination port number (enc_dst_port).
  1153. * Before calling this funtcion, fltr->tunnel_type should be set to TNL_GTPU,
  1154. * therefore making GTP-U the default choice (when destination port number is
  1155. * not specified).
  1156. */
  1157. static int
  1158. ice_parse_gtp_type(struct flow_match_ports match,
  1159. struct ice_tc_flower_fltr *fltr)
  1160. {
  1161. u16 dst_port;
  1162. if (match.key->dst) {
  1163. dst_port = be16_to_cpu(match.key->dst);
  1164. switch (dst_port) {
  1165. case 2152:
  1166. break;
  1167. case 2123:
  1168. fltr->tunnel_type = TNL_GTPC;
  1169. break;
  1170. default:
  1171. NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported GTP port number");
  1172. return -EINVAL;
  1173. }
  1174. }
  1175. return 0;
  1176. }
  1177. static int
  1178. ice_parse_tunnel_attr(struct net_device *dev, struct flow_rule *rule,
  1179. struct ice_tc_flower_fltr *fltr)
  1180. {
  1181. struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
  1182. struct netlink_ext_ack *extack = fltr->extack;
  1183. struct flow_match_control enc_control;
  1184. fltr->tunnel_type = ice_tc_tun_get_type(dev);
  1185. headers->l3_key.ip_proto = IPPROTO_UDP;
  1186. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
  1187. struct flow_match_enc_keyid enc_keyid;
  1188. flow_rule_match_enc_keyid(rule, &enc_keyid);
  1189. if (!enc_keyid.mask->keyid ||
  1190. enc_keyid.mask->keyid != cpu_to_be32(ICE_TC_FLOWER_MASK_32))
  1191. return -EINVAL;
  1192. fltr->flags |= ICE_TC_FLWR_FIELD_TENANT_ID;
  1193. fltr->tenant_id = enc_keyid.key->keyid;
  1194. }
  1195. flow_rule_match_enc_control(rule, &enc_control);
  1196. if (flow_rule_has_enc_control_flags(enc_control.mask->flags, extack))
  1197. return -EOPNOTSUPP;
  1198. if (enc_control.key->addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  1199. struct flow_match_ipv4_addrs match;
  1200. flow_rule_match_enc_ipv4_addrs(rule, &match);
  1201. if (ice_tc_set_ipv4(&match, fltr, headers, true))
  1202. return -EINVAL;
  1203. } else if (enc_control.key->addr_type ==
  1204. FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  1205. struct flow_match_ipv6_addrs match;
  1206. flow_rule_match_enc_ipv6_addrs(rule, &match);
  1207. if (ice_tc_set_ipv6(&match, fltr, headers, true))
  1208. return -EINVAL;
  1209. }
  1210. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_IP)) {
  1211. struct flow_match_ip match;
  1212. flow_rule_match_enc_ip(rule, &match);
  1213. ice_tc_set_tos_ttl(&match, fltr, headers, true);
  1214. }
  1215. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS) &&
  1216. fltr->tunnel_type != TNL_VXLAN && fltr->tunnel_type != TNL_GENEVE) {
  1217. struct flow_match_ports match;
  1218. flow_rule_match_enc_ports(rule, &match);
  1219. if (fltr->tunnel_type != TNL_GTPU) {
  1220. if (ice_tc_set_port(match, fltr, headers, true))
  1221. return -EINVAL;
  1222. } else {
  1223. if (ice_parse_gtp_type(match, fltr))
  1224. return -EINVAL;
  1225. }
  1226. }
  1227. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_OPTS) &&
  1228. (fltr->tunnel_type == TNL_GTPU || fltr->tunnel_type == TNL_GTPC)) {
  1229. struct flow_match_enc_opts match;
  1230. flow_rule_match_enc_opts(rule, &match);
  1231. memcpy(&fltr->gtp_pdu_info_keys, &match.key->data[0],
  1232. sizeof(struct gtp_pdu_session_info));
  1233. memcpy(&fltr->gtp_pdu_info_masks, &match.mask->data[0],
  1234. sizeof(struct gtp_pdu_session_info));
  1235. fltr->flags |= ICE_TC_FLWR_FIELD_GTP_OPTS;
  1236. }
  1237. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_OPTS) &&
  1238. fltr->tunnel_type == TNL_PFCP) {
  1239. struct flow_match_enc_opts match;
  1240. flow_rule_match_enc_opts(rule, &match);
  1241. memcpy(&fltr->pfcp_meta_keys, match.key->data,
  1242. sizeof(struct pfcp_metadata));
  1243. memcpy(&fltr->pfcp_meta_masks, match.mask->data,
  1244. sizeof(struct pfcp_metadata));
  1245. fltr->flags |= ICE_TC_FLWR_FIELD_PFCP_OPTS;
  1246. }
  1247. return 0;
  1248. }
  1249. /**
  1250. * ice_parse_cls_flower - Parse TC flower filters provided by kernel
  1251. * @vsi: Pointer to the VSI
  1252. * @filter_dev: Pointer to device on which filter is being added
  1253. * @f: Pointer to struct flow_cls_offload
  1254. * @fltr: Pointer to filter structure
  1255. */
  1256. static int
  1257. ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi,
  1258. struct flow_cls_offload *f,
  1259. struct ice_tc_flower_fltr *fltr)
  1260. {
  1261. struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
  1262. struct flow_rule *rule = flow_cls_offload_flow_rule(f);
  1263. u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
  1264. struct flow_dissector *dissector;
  1265. struct net_device *tunnel_dev;
  1266. dissector = rule->match.dissector;
  1267. if (dissector->used_keys &
  1268. ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
  1269. BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
  1270. BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
  1271. BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
  1272. BIT_ULL(FLOW_DISSECTOR_KEY_CVLAN) |
  1273. BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
  1274. BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
  1275. BIT_ULL(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
  1276. BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID) |
  1277. BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
  1278. BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
  1279. BIT_ULL(FLOW_DISSECTOR_KEY_ENC_PORTS) |
  1280. BIT_ULL(FLOW_DISSECTOR_KEY_ENC_OPTS) |
  1281. BIT_ULL(FLOW_DISSECTOR_KEY_IP) |
  1282. BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IP) |
  1283. BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
  1284. BIT_ULL(FLOW_DISSECTOR_KEY_PPPOE) |
  1285. BIT_ULL(FLOW_DISSECTOR_KEY_L2TPV3))) {
  1286. NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported key used");
  1287. return -EOPNOTSUPP;
  1288. }
  1289. tunnel_dev = ice_get_tunnel_device(filter_dev, rule);
  1290. if (tunnel_dev) {
  1291. int err;
  1292. filter_dev = tunnel_dev;
  1293. err = ice_parse_tunnel_attr(filter_dev, rule, fltr);
  1294. if (err) {
  1295. NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to parse TC flower tunnel attributes");
  1296. return err;
  1297. }
  1298. /* PFCP is considered non-tunneled - don't swap headers. */
  1299. if (fltr->tunnel_type != TNL_PFCP) {
  1300. /* Header pointers should point to the inner headers,
  1301. * outer header were already set by
  1302. * ice_parse_tunnel_attr().
  1303. */
  1304. headers = &fltr->inner_headers;
  1305. }
  1306. } else if (dissector->used_keys &
  1307. (BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
  1308. BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
  1309. BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID) |
  1310. BIT_ULL(FLOW_DISSECTOR_KEY_ENC_PORTS) |
  1311. BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IP) |
  1312. BIT_ULL(FLOW_DISSECTOR_KEY_ENC_OPTS) |
  1313. BIT_ULL(FLOW_DISSECTOR_KEY_ENC_CONTROL))) {
  1314. NL_SET_ERR_MSG_MOD(fltr->extack, "Tunnel key used, but device isn't a tunnel");
  1315. return -EOPNOTSUPP;
  1316. } else {
  1317. fltr->tunnel_type = TNL_LAST;
  1318. }
  1319. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
  1320. struct flow_match_basic match;
  1321. flow_rule_match_basic(rule, &match);
  1322. n_proto_key = ntohs(match.key->n_proto);
  1323. n_proto_mask = ntohs(match.mask->n_proto);
  1324. if (n_proto_key == ETH_P_ALL || n_proto_key == 0 ||
  1325. fltr->tunnel_type == TNL_GTPU ||
  1326. fltr->tunnel_type == TNL_GTPC) {
  1327. n_proto_key = 0;
  1328. n_proto_mask = 0;
  1329. } else {
  1330. fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
  1331. }
  1332. headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
  1333. headers->l2_mask.n_proto = cpu_to_be16(n_proto_mask);
  1334. headers->l3_key.ip_proto = match.key->ip_proto;
  1335. }
  1336. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  1337. struct flow_match_eth_addrs match;
  1338. flow_rule_match_eth_addrs(rule, &match);
  1339. if (!is_zero_ether_addr(match.key->dst)) {
  1340. ether_addr_copy(headers->l2_key.dst_mac,
  1341. match.key->dst);
  1342. ether_addr_copy(headers->l2_mask.dst_mac,
  1343. match.mask->dst);
  1344. fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
  1345. }
  1346. if (!is_zero_ether_addr(match.key->src)) {
  1347. ether_addr_copy(headers->l2_key.src_mac,
  1348. match.key->src);
  1349. ether_addr_copy(headers->l2_mask.src_mac,
  1350. match.mask->src);
  1351. fltr->flags |= ICE_TC_FLWR_FIELD_SRC_MAC;
  1352. }
  1353. }
  1354. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN) ||
  1355. is_vlan_dev(filter_dev)) {
  1356. struct flow_dissector_key_vlan mask;
  1357. struct flow_dissector_key_vlan key;
  1358. struct flow_match_vlan match;
  1359. if (is_vlan_dev(filter_dev)) {
  1360. match.key = &key;
  1361. match.key->vlan_id = vlan_dev_vlan_id(filter_dev);
  1362. match.key->vlan_priority = 0;
  1363. match.mask = &mask;
  1364. memset(match.mask, 0xff, sizeof(*match.mask));
  1365. match.mask->vlan_priority = 0;
  1366. } else {
  1367. flow_rule_match_vlan(rule, &match);
  1368. }
  1369. if (match.mask->vlan_id) {
  1370. if (match.mask->vlan_id == VLAN_VID_MASK) {
  1371. fltr->flags |= ICE_TC_FLWR_FIELD_VLAN;
  1372. headers->vlan_hdr.vlan_id =
  1373. cpu_to_be16(match.key->vlan_id &
  1374. VLAN_VID_MASK);
  1375. } else {
  1376. NL_SET_ERR_MSG_MOD(fltr->extack, "Bad VLAN mask");
  1377. return -EINVAL;
  1378. }
  1379. }
  1380. if (match.mask->vlan_priority) {
  1381. fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_PRIO;
  1382. headers->vlan_hdr.vlan_prio =
  1383. be16_encode_bits(match.key->vlan_priority,
  1384. VLAN_PRIO_MASK);
  1385. }
  1386. if (match.mask->vlan_tpid) {
  1387. headers->vlan_hdr.vlan_tpid = match.key->vlan_tpid;
  1388. fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_TPID;
  1389. }
  1390. }
  1391. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CVLAN)) {
  1392. struct flow_match_vlan match;
  1393. if (!ice_is_dvm_ena(&vsi->back->hw)) {
  1394. NL_SET_ERR_MSG_MOD(fltr->extack, "Double VLAN mode is not enabled");
  1395. return -EINVAL;
  1396. }
  1397. flow_rule_match_cvlan(rule, &match);
  1398. if (match.mask->vlan_id) {
  1399. if (match.mask->vlan_id == VLAN_VID_MASK) {
  1400. fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN;
  1401. headers->cvlan_hdr.vlan_id =
  1402. cpu_to_be16(match.key->vlan_id &
  1403. VLAN_VID_MASK);
  1404. } else {
  1405. NL_SET_ERR_MSG_MOD(fltr->extack,
  1406. "Bad CVLAN mask");
  1407. return -EINVAL;
  1408. }
  1409. }
  1410. if (match.mask->vlan_priority) {
  1411. fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN_PRIO;
  1412. headers->cvlan_hdr.vlan_prio =
  1413. be16_encode_bits(match.key->vlan_priority,
  1414. VLAN_PRIO_MASK);
  1415. }
  1416. }
  1417. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PPPOE)) {
  1418. struct flow_match_pppoe match;
  1419. flow_rule_match_pppoe(rule, &match);
  1420. n_proto_key = ice_tc_set_pppoe(&match, fltr, headers);
  1421. /* If ethertype equals ETH_P_PPP_SES, n_proto might be
  1422. * overwritten by encapsulated protocol (ppp_proto field) or set
  1423. * to 0. To correct this, flow_match_pppoe provides the type
  1424. * field, which contains the actual ethertype (ETH_P_PPP_SES).
  1425. */
  1426. headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
  1427. headers->l2_mask.n_proto = cpu_to_be16(0xFFFF);
  1428. fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
  1429. }
  1430. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
  1431. struct flow_match_control match;
  1432. flow_rule_match_control(rule, &match);
  1433. addr_type = match.key->addr_type;
  1434. if (flow_rule_has_control_flags(match.mask->flags,
  1435. fltr->extack))
  1436. return -EOPNOTSUPP;
  1437. }
  1438. if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  1439. struct flow_match_ipv4_addrs match;
  1440. flow_rule_match_ipv4_addrs(rule, &match);
  1441. if (ice_tc_set_ipv4(&match, fltr, headers, false))
  1442. return -EINVAL;
  1443. }
  1444. if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  1445. struct flow_match_ipv6_addrs match;
  1446. flow_rule_match_ipv6_addrs(rule, &match);
  1447. if (ice_tc_set_ipv6(&match, fltr, headers, false))
  1448. return -EINVAL;
  1449. }
  1450. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
  1451. struct flow_match_ip match;
  1452. flow_rule_match_ip(rule, &match);
  1453. ice_tc_set_tos_ttl(&match, fltr, headers, false);
  1454. }
  1455. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_L2TPV3)) {
  1456. struct flow_match_l2tpv3 match;
  1457. flow_rule_match_l2tpv3(rule, &match);
  1458. fltr->flags |= ICE_TC_FLWR_FIELD_L2TPV3_SESSID;
  1459. headers->l2tpv3_hdr.session_id = match.key->session_id;
  1460. }
  1461. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
  1462. struct flow_match_ports match;
  1463. flow_rule_match_ports(rule, &match);
  1464. if (ice_tc_set_port(match, fltr, headers, false))
  1465. return -EINVAL;
  1466. switch (headers->l3_key.ip_proto) {
  1467. case IPPROTO_TCP:
  1468. case IPPROTO_UDP:
  1469. break;
  1470. default:
  1471. NL_SET_ERR_MSG_MOD(fltr->extack, "Only UDP and TCP transport are supported");
  1472. return -EINVAL;
  1473. }
  1474. }
  1475. return 0;
  1476. }
  1477. /**
  1478. * ice_add_switch_fltr - Add TC flower filters
  1479. * @vsi: Pointer to VSI
  1480. * @fltr: Pointer to struct ice_tc_flower_fltr
  1481. *
  1482. * Add filter in HW switch block
  1483. */
  1484. static int
  1485. ice_add_switch_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
  1486. {
  1487. if (fltr->action.fltr_act == ICE_FWD_TO_QGRP)
  1488. return -EOPNOTSUPP;
  1489. if (ice_is_eswitch_mode_switchdev(vsi->back))
  1490. return ice_eswitch_add_tc_fltr(vsi, fltr);
  1491. return ice_add_tc_flower_adv_fltr(vsi, fltr);
  1492. }
  1493. /**
  1494. * ice_prep_adq_filter - Prepare ADQ filter with the required additional headers
  1495. * @vsi: Pointer to VSI
  1496. * @fltr: Pointer to TC flower filter structure
  1497. *
  1498. * Prepare ADQ filter with the required additional header fields
  1499. */
  1500. static int
  1501. ice_prep_adq_filter(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
  1502. {
  1503. if ((fltr->flags & ICE_TC_FLWR_FIELD_TENANT_ID) &&
  1504. (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
  1505. ICE_TC_FLWR_FIELD_SRC_MAC))) {
  1506. NL_SET_ERR_MSG_MOD(fltr->extack,
  1507. "Unable to add filter because filter using tunnel key and inner MAC is unsupported combination");
  1508. return -EOPNOTSUPP;
  1509. }
  1510. /* For ADQ, filter must include dest MAC address, otherwise unwanted
  1511. * packets with unrelated MAC address get delivered to ADQ VSIs as long
  1512. * as remaining filter criteria is satisfied such as dest IP address
  1513. * and dest/src L4 port. Below code handles the following cases:
  1514. * 1. For non-tunnel, if user specify MAC addresses, use them.
  1515. * 2. For non-tunnel, if user didn't specify MAC address, add implicit
  1516. * dest MAC to be lower netdev's active unicast MAC address
  1517. * 3. For tunnel, as of now TC-filter through flower classifier doesn't
  1518. * have provision for user to specify outer DMAC, hence driver to
  1519. * implicitly add outer dest MAC to be lower netdev's active unicast
  1520. * MAC address.
  1521. */
  1522. if (fltr->tunnel_type != TNL_LAST &&
  1523. !(fltr->flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC))
  1524. fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DST_MAC;
  1525. if (fltr->tunnel_type == TNL_LAST &&
  1526. !(fltr->flags & ICE_TC_FLWR_FIELD_DST_MAC))
  1527. fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
  1528. if (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
  1529. ICE_TC_FLWR_FIELD_ENC_DST_MAC)) {
  1530. ether_addr_copy(fltr->outer_headers.l2_key.dst_mac,
  1531. vsi->netdev->dev_addr);
  1532. eth_broadcast_addr(fltr->outer_headers.l2_mask.dst_mac);
  1533. }
  1534. /* Make sure VLAN is already added to main VSI, before allowing ADQ to
  1535. * add a VLAN based filter such as MAC + VLAN + L4 port.
  1536. */
  1537. if (fltr->flags & ICE_TC_FLWR_FIELD_VLAN) {
  1538. u16 vlan_id = be16_to_cpu(fltr->outer_headers.vlan_hdr.vlan_id);
  1539. if (!ice_vlan_fltr_exist(&vsi->back->hw, vlan_id, vsi->idx)) {
  1540. NL_SET_ERR_MSG_MOD(fltr->extack,
  1541. "Unable to add filter because legacy VLAN filter for specified destination doesn't exist");
  1542. return -EINVAL;
  1543. }
  1544. }
  1545. return 0;
  1546. }
  1547. /**
  1548. * ice_handle_tclass_action - Support directing to a traffic class
  1549. * @vsi: Pointer to VSI
  1550. * @cls_flower: Pointer to TC flower offload structure
  1551. * @fltr: Pointer to TC flower filter structure
  1552. *
  1553. * Support directing traffic to a traffic class/queue-set
  1554. */
  1555. static int
  1556. ice_handle_tclass_action(struct ice_vsi *vsi,
  1557. struct flow_cls_offload *cls_flower,
  1558. struct ice_tc_flower_fltr *fltr)
  1559. {
  1560. int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
  1561. /* user specified hw_tc (must be non-zero for ADQ TC), action is forward
  1562. * to hw_tc (i.e. ADQ channel number)
  1563. */
  1564. if (tc < ICE_CHNL_START_TC) {
  1565. NL_SET_ERR_MSG_MOD(fltr->extack,
  1566. "Unable to add filter because of unsupported destination");
  1567. return -EOPNOTSUPP;
  1568. }
  1569. if (!(vsi->all_enatc & BIT(tc))) {
  1570. NL_SET_ERR_MSG_MOD(fltr->extack,
  1571. "Unable to add filter because of non-existence destination");
  1572. return -EINVAL;
  1573. }
  1574. fltr->action.fltr_act = ICE_FWD_TO_VSI;
  1575. fltr->action.fwd.tc.tc_class = tc;
  1576. return ice_prep_adq_filter(vsi, fltr);
  1577. }
  1578. static int
  1579. ice_tc_forward_to_queue(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr,
  1580. struct flow_action_entry *act)
  1581. {
  1582. struct ice_vsi *ch_vsi = NULL;
  1583. u16 queue = act->rx_queue;
  1584. if (queue >= vsi->num_rxq) {
  1585. NL_SET_ERR_MSG_MOD(fltr->extack,
  1586. "Unable to add filter because specified queue is invalid");
  1587. return -EINVAL;
  1588. }
  1589. fltr->action.fltr_act = ICE_FWD_TO_Q;
  1590. fltr->action.fwd.q.queue = queue;
  1591. /* determine corresponding HW queue */
  1592. fltr->action.fwd.q.hw_queue = vsi->rxq_map[queue];
  1593. /* If ADQ is configured, and the queue belongs to ADQ VSI, then prepare
  1594. * ADQ switch filter
  1595. */
  1596. ch_vsi = ice_locate_vsi_using_queue(vsi, fltr->action.fwd.q.queue);
  1597. if (!ch_vsi)
  1598. return -EINVAL;
  1599. fltr->dest_vsi = ch_vsi;
  1600. if (!ice_is_chnl_fltr(fltr))
  1601. return 0;
  1602. return ice_prep_adq_filter(vsi, fltr);
  1603. }
  1604. static int
  1605. ice_tc_parse_action(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr,
  1606. struct flow_action_entry *act)
  1607. {
  1608. switch (act->id) {
  1609. case FLOW_ACTION_RX_QUEUE_MAPPING:
  1610. /* forward to queue */
  1611. return ice_tc_forward_to_queue(vsi, fltr, act);
  1612. case FLOW_ACTION_DROP:
  1613. fltr->action.fltr_act = ICE_DROP_PACKET;
  1614. return 0;
  1615. default:
  1616. NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported TC action");
  1617. return -EOPNOTSUPP;
  1618. }
  1619. }
  1620. /**
  1621. * ice_parse_tc_flower_actions - Parse the actions for a TC filter
  1622. * @filter_dev: Pointer to device on which filter is being added
  1623. * @vsi: Pointer to VSI
  1624. * @cls_flower: Pointer to TC flower offload structure
  1625. * @fltr: Pointer to TC flower filter structure
  1626. *
  1627. * Parse the actions for a TC filter
  1628. */
  1629. static int ice_parse_tc_flower_actions(struct net_device *filter_dev,
  1630. struct ice_vsi *vsi,
  1631. struct flow_cls_offload *cls_flower,
  1632. struct ice_tc_flower_fltr *fltr)
  1633. {
  1634. struct flow_rule *rule = flow_cls_offload_flow_rule(cls_flower);
  1635. struct flow_action *flow_action = &rule->action;
  1636. struct flow_action_entry *act;
  1637. int i, err;
  1638. if (cls_flower->classid)
  1639. return ice_handle_tclass_action(vsi, cls_flower, fltr);
  1640. if (!flow_action_has_entries(flow_action))
  1641. return -EINVAL;
  1642. flow_action_for_each(i, act, flow_action) {
  1643. if (ice_is_eswitch_mode_switchdev(vsi->back))
  1644. err = ice_eswitch_tc_parse_action(filter_dev, fltr, act);
  1645. else
  1646. err = ice_tc_parse_action(vsi, fltr, act);
  1647. if (err)
  1648. return err;
  1649. continue;
  1650. }
  1651. return 0;
  1652. }
  1653. /**
  1654. * ice_del_tc_fltr - deletes a filter from HW table
  1655. * @vsi: Pointer to VSI
  1656. * @fltr: Pointer to struct ice_tc_flower_fltr
  1657. *
  1658. * This function deletes a filter from HW table and manages book-keeping
  1659. */
  1660. static int ice_del_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
  1661. {
  1662. struct ice_rule_query_data rule_rem;
  1663. struct ice_pf *pf = vsi->back;
  1664. int err;
  1665. rule_rem.rid = fltr->rid;
  1666. rule_rem.rule_id = fltr->rule_id;
  1667. rule_rem.vsi_handle = fltr->dest_vsi_handle;
  1668. err = ice_rem_adv_rule_by_id(&pf->hw, &rule_rem);
  1669. if (err) {
  1670. if (err == -ENOENT) {
  1671. NL_SET_ERR_MSG_MOD(fltr->extack, "Filter does not exist");
  1672. return -ENOENT;
  1673. }
  1674. NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to delete TC flower filter");
  1675. return -EIO;
  1676. }
  1677. /* update advanced switch filter count for destination
  1678. * VSI if filter destination was VSI
  1679. */
  1680. if (fltr->dest_vsi) {
  1681. if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
  1682. fltr->dest_vsi->num_chnl_fltr--;
  1683. /* keeps track of channel filters for PF VSI */
  1684. if (vsi->type == ICE_VSI_PF &&
  1685. (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
  1686. ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
  1687. pf->num_dmac_chnl_fltrs--;
  1688. }
  1689. }
  1690. return 0;
  1691. }
  1692. /**
  1693. * ice_add_tc_fltr - adds a TC flower filter
  1694. * @netdev: Pointer to netdev
  1695. * @vsi: Pointer to VSI
  1696. * @f: Pointer to flower offload structure
  1697. * @__fltr: Pointer to struct ice_tc_flower_fltr
  1698. *
  1699. * This function parses TC-flower input fields, parses action,
  1700. * and adds a filter.
  1701. */
  1702. static int
  1703. ice_add_tc_fltr(struct net_device *netdev, struct ice_vsi *vsi,
  1704. struct flow_cls_offload *f,
  1705. struct ice_tc_flower_fltr **__fltr)
  1706. {
  1707. struct ice_tc_flower_fltr *fltr;
  1708. int err;
  1709. /* by default, set output to be INVALID */
  1710. *__fltr = NULL;
  1711. fltr = kzalloc(sizeof(*fltr), GFP_KERNEL);
  1712. if (!fltr)
  1713. return -ENOMEM;
  1714. fltr->cookie = f->cookie;
  1715. fltr->extack = f->common.extack;
  1716. fltr->src_vsi = vsi;
  1717. INIT_HLIST_NODE(&fltr->tc_flower_node);
  1718. err = ice_parse_cls_flower(netdev, vsi, f, fltr);
  1719. if (err < 0)
  1720. goto err;
  1721. err = ice_parse_tc_flower_actions(netdev, vsi, f, fltr);
  1722. if (err < 0)
  1723. goto err;
  1724. err = ice_add_switch_fltr(vsi, fltr);
  1725. if (err < 0)
  1726. goto err;
  1727. /* return the newly created filter */
  1728. *__fltr = fltr;
  1729. return 0;
  1730. err:
  1731. kfree(fltr);
  1732. return err;
  1733. }
  1734. /**
  1735. * ice_find_tc_flower_fltr - Find the TC flower filter in the list
  1736. * @pf: Pointer to PF
  1737. * @cookie: filter specific cookie
  1738. */
  1739. static struct ice_tc_flower_fltr *
  1740. ice_find_tc_flower_fltr(struct ice_pf *pf, unsigned long cookie)
  1741. {
  1742. struct ice_tc_flower_fltr *fltr;
  1743. hlist_for_each_entry(fltr, &pf->tc_flower_fltr_list, tc_flower_node)
  1744. if (cookie == fltr->cookie)
  1745. return fltr;
  1746. return NULL;
  1747. }
  1748. /**
  1749. * ice_add_cls_flower - add TC flower filters
  1750. * @netdev: Pointer to filter device
  1751. * @vsi: Pointer to VSI
  1752. * @cls_flower: Pointer to flower offload structure
  1753. */
  1754. int
  1755. ice_add_cls_flower(struct net_device *netdev, struct ice_vsi *vsi,
  1756. struct flow_cls_offload *cls_flower)
  1757. {
  1758. struct netlink_ext_ack *extack = cls_flower->common.extack;
  1759. struct net_device *vsi_netdev = vsi->netdev;
  1760. struct ice_tc_flower_fltr *fltr;
  1761. struct ice_pf *pf = vsi->back;
  1762. int err;
  1763. if (ice_is_reset_in_progress(pf->state))
  1764. return -EBUSY;
  1765. if (test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
  1766. return -EINVAL;
  1767. if (ice_is_port_repr_netdev(netdev))
  1768. vsi_netdev = netdev;
  1769. if (!(vsi_netdev->features & NETIF_F_HW_TC) &&
  1770. !test_bit(ICE_FLAG_CLS_FLOWER, pf->flags)) {
  1771. /* Based on TC indirect notifications from kernel, all ice
  1772. * devices get an instance of rule from higher level device.
  1773. * Avoid triggering explicit error in this case.
  1774. */
  1775. if (netdev == vsi_netdev)
  1776. NL_SET_ERR_MSG_MOD(extack, "can't apply TC flower filters, turn ON hw-tc-offload and try again");
  1777. return -EINVAL;
  1778. }
  1779. /* avoid duplicate entries, if exists - return error */
  1780. fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
  1781. if (fltr) {
  1782. NL_SET_ERR_MSG_MOD(extack, "filter cookie already exists, ignoring");
  1783. return -EEXIST;
  1784. }
  1785. /* prep and add TC-flower filter in HW */
  1786. err = ice_add_tc_fltr(netdev, vsi, cls_flower, &fltr);
  1787. if (err)
  1788. return err;
  1789. /* add filter into an ordered list */
  1790. hlist_add_head(&fltr->tc_flower_node, &pf->tc_flower_fltr_list);
  1791. return 0;
  1792. }
  1793. /**
  1794. * ice_del_cls_flower - delete TC flower filters
  1795. * @vsi: Pointer to VSI
  1796. * @cls_flower: Pointer to struct flow_cls_offload
  1797. */
  1798. int
  1799. ice_del_cls_flower(struct ice_vsi *vsi, struct flow_cls_offload *cls_flower)
  1800. {
  1801. struct ice_tc_flower_fltr *fltr;
  1802. struct ice_pf *pf = vsi->back;
  1803. int err;
  1804. /* find filter */
  1805. fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
  1806. if (!fltr) {
  1807. if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) &&
  1808. hlist_empty(&pf->tc_flower_fltr_list))
  1809. return 0;
  1810. NL_SET_ERR_MSG_MOD(cls_flower->common.extack, "failed to delete TC flower filter because unable to find it");
  1811. return -EINVAL;
  1812. }
  1813. fltr->extack = cls_flower->common.extack;
  1814. /* delete filter from HW */
  1815. err = ice_del_tc_fltr(vsi, fltr);
  1816. if (err)
  1817. return err;
  1818. /* delete filter from an ordered list */
  1819. hlist_del(&fltr->tc_flower_node);
  1820. /* free the filter node */
  1821. kfree(fltr);
  1822. return 0;
  1823. }
  1824. /**
  1825. * ice_replay_tc_fltrs - replay TC filters
  1826. * @pf: pointer to PF struct
  1827. */
  1828. void ice_replay_tc_fltrs(struct ice_pf *pf)
  1829. {
  1830. struct ice_tc_flower_fltr *fltr;
  1831. struct hlist_node *node;
  1832. hlist_for_each_entry_safe(fltr, node,
  1833. &pf->tc_flower_fltr_list,
  1834. tc_flower_node) {
  1835. fltr->extack = NULL;
  1836. ice_add_switch_fltr(fltr->src_vsi, fltr);
  1837. }
  1838. }