cxgb4_tc_flower.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*
  2. * This file is part of the Chelsio T4/T5/T6 Ethernet driver for Linux.
  3. *
  4. * Copyright (c) 2017 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <net/tc_act/tc_mirred.h>
  35. #include <net/tc_act/tc_pedit.h>
  36. #include <net/tc_act/tc_gact.h>
  37. #include <net/tc_act/tc_vlan.h>
  38. #include "cxgb4.h"
  39. #include "cxgb4_filter.h"
  40. #include "cxgb4_tc_flower.h"
  41. #define STATS_CHECK_PERIOD (HZ / 2)
  42. static struct ch_tc_pedit_fields pedits[] = {
  43. PEDIT_FIELDS(ETH_, DMAC_31_0, 4, dmac, 0),
  44. PEDIT_FIELDS(ETH_, DMAC_47_32, 2, dmac, 4),
  45. PEDIT_FIELDS(ETH_, SMAC_15_0, 2, smac, 0),
  46. PEDIT_FIELDS(ETH_, SMAC_47_16, 4, smac, 2),
  47. PEDIT_FIELDS(IP4_, SRC, 4, nat_fip, 0),
  48. PEDIT_FIELDS(IP4_, DST, 4, nat_lip, 0),
  49. PEDIT_FIELDS(IP6_, SRC_31_0, 4, nat_fip, 0),
  50. PEDIT_FIELDS(IP6_, SRC_63_32, 4, nat_fip, 4),
  51. PEDIT_FIELDS(IP6_, SRC_95_64, 4, nat_fip, 8),
  52. PEDIT_FIELDS(IP6_, SRC_127_96, 4, nat_fip, 12),
  53. PEDIT_FIELDS(IP6_, DST_31_0, 4, nat_lip, 0),
  54. PEDIT_FIELDS(IP6_, DST_63_32, 4, nat_lip, 4),
  55. PEDIT_FIELDS(IP6_, DST_95_64, 4, nat_lip, 8),
  56. PEDIT_FIELDS(IP6_, DST_127_96, 4, nat_lip, 12),
  57. PEDIT_FIELDS(TCP_, SPORT, 2, nat_fport, 0),
  58. PEDIT_FIELDS(TCP_, DPORT, 2, nat_lport, 0),
  59. PEDIT_FIELDS(UDP_, SPORT, 2, nat_fport, 0),
  60. PEDIT_FIELDS(UDP_, DPORT, 2, nat_lport, 0),
  61. };
  62. static struct ch_tc_flower_entry *allocate_flower_entry(void)
  63. {
  64. struct ch_tc_flower_entry *new = kzalloc(sizeof(*new), GFP_KERNEL);
  65. spin_lock_init(&new->lock);
  66. return new;
  67. }
  68. /* Must be called with either RTNL or rcu_read_lock */
  69. static struct ch_tc_flower_entry *ch_flower_lookup(struct adapter *adap,
  70. unsigned long flower_cookie)
  71. {
  72. return rhashtable_lookup_fast(&adap->flower_tbl, &flower_cookie,
  73. adap->flower_ht_params);
  74. }
  75. static void cxgb4_process_flow_match(struct net_device *dev,
  76. struct tc_cls_flower_offload *cls,
  77. struct ch_filter_specification *fs)
  78. {
  79. u16 addr_type = 0;
  80. if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
  81. struct flow_dissector_key_control *key =
  82. skb_flow_dissector_target(cls->dissector,
  83. FLOW_DISSECTOR_KEY_CONTROL,
  84. cls->key);
  85. addr_type = key->addr_type;
  86. }
  87. if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
  88. struct flow_dissector_key_basic *key =
  89. skb_flow_dissector_target(cls->dissector,
  90. FLOW_DISSECTOR_KEY_BASIC,
  91. cls->key);
  92. struct flow_dissector_key_basic *mask =
  93. skb_flow_dissector_target(cls->dissector,
  94. FLOW_DISSECTOR_KEY_BASIC,
  95. cls->mask);
  96. u16 ethtype_key = ntohs(key->n_proto);
  97. u16 ethtype_mask = ntohs(mask->n_proto);
  98. if (ethtype_key == ETH_P_ALL) {
  99. ethtype_key = 0;
  100. ethtype_mask = 0;
  101. }
  102. if (ethtype_key == ETH_P_IPV6)
  103. fs->type = 1;
  104. fs->val.ethtype = ethtype_key;
  105. fs->mask.ethtype = ethtype_mask;
  106. fs->val.proto = key->ip_proto;
  107. fs->mask.proto = mask->ip_proto;
  108. }
  109. if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  110. struct flow_dissector_key_ipv4_addrs *key =
  111. skb_flow_dissector_target(cls->dissector,
  112. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  113. cls->key);
  114. struct flow_dissector_key_ipv4_addrs *mask =
  115. skb_flow_dissector_target(cls->dissector,
  116. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  117. cls->mask);
  118. fs->type = 0;
  119. memcpy(&fs->val.lip[0], &key->dst, sizeof(key->dst));
  120. memcpy(&fs->val.fip[0], &key->src, sizeof(key->src));
  121. memcpy(&fs->mask.lip[0], &mask->dst, sizeof(mask->dst));
  122. memcpy(&fs->mask.fip[0], &mask->src, sizeof(mask->src));
  123. /* also initialize nat_lip/fip to same values */
  124. memcpy(&fs->nat_lip[0], &key->dst, sizeof(key->dst));
  125. memcpy(&fs->nat_fip[0], &key->src, sizeof(key->src));
  126. }
  127. if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  128. struct flow_dissector_key_ipv6_addrs *key =
  129. skb_flow_dissector_target(cls->dissector,
  130. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  131. cls->key);
  132. struct flow_dissector_key_ipv6_addrs *mask =
  133. skb_flow_dissector_target(cls->dissector,
  134. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  135. cls->mask);
  136. fs->type = 1;
  137. memcpy(&fs->val.lip[0], key->dst.s6_addr, sizeof(key->dst));
  138. memcpy(&fs->val.fip[0], key->src.s6_addr, sizeof(key->src));
  139. memcpy(&fs->mask.lip[0], mask->dst.s6_addr, sizeof(mask->dst));
  140. memcpy(&fs->mask.fip[0], mask->src.s6_addr, sizeof(mask->src));
  141. /* also initialize nat_lip/fip to same values */
  142. memcpy(&fs->nat_lip[0], key->dst.s6_addr, sizeof(key->dst));
  143. memcpy(&fs->nat_fip[0], key->src.s6_addr, sizeof(key->src));
  144. }
  145. if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
  146. struct flow_dissector_key_ports *key, *mask;
  147. key = skb_flow_dissector_target(cls->dissector,
  148. FLOW_DISSECTOR_KEY_PORTS,
  149. cls->key);
  150. mask = skb_flow_dissector_target(cls->dissector,
  151. FLOW_DISSECTOR_KEY_PORTS,
  152. cls->mask);
  153. fs->val.lport = cpu_to_be16(key->dst);
  154. fs->mask.lport = cpu_to_be16(mask->dst);
  155. fs->val.fport = cpu_to_be16(key->src);
  156. fs->mask.fport = cpu_to_be16(mask->src);
  157. /* also initialize nat_lport/fport to same values */
  158. fs->nat_lport = cpu_to_be16(key->dst);
  159. fs->nat_fport = cpu_to_be16(key->src);
  160. }
  161. if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_IP)) {
  162. struct flow_dissector_key_ip *key, *mask;
  163. key = skb_flow_dissector_target(cls->dissector,
  164. FLOW_DISSECTOR_KEY_IP,
  165. cls->key);
  166. mask = skb_flow_dissector_target(cls->dissector,
  167. FLOW_DISSECTOR_KEY_IP,
  168. cls->mask);
  169. fs->val.tos = key->tos;
  170. fs->mask.tos = mask->tos;
  171. }
  172. if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
  173. struct flow_dissector_key_keyid *key, *mask;
  174. key = skb_flow_dissector_target(cls->dissector,
  175. FLOW_DISSECTOR_KEY_ENC_KEYID,
  176. cls->key);
  177. mask = skb_flow_dissector_target(cls->dissector,
  178. FLOW_DISSECTOR_KEY_ENC_KEYID,
  179. cls->mask);
  180. fs->val.vni = be32_to_cpu(key->keyid);
  181. fs->mask.vni = be32_to_cpu(mask->keyid);
  182. if (fs->mask.vni) {
  183. fs->val.encap_vld = 1;
  184. fs->mask.encap_vld = 1;
  185. }
  186. }
  187. if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
  188. struct flow_dissector_key_vlan *key, *mask;
  189. u16 vlan_tci, vlan_tci_mask;
  190. key = skb_flow_dissector_target(cls->dissector,
  191. FLOW_DISSECTOR_KEY_VLAN,
  192. cls->key);
  193. mask = skb_flow_dissector_target(cls->dissector,
  194. FLOW_DISSECTOR_KEY_VLAN,
  195. cls->mask);
  196. vlan_tci = key->vlan_id | (key->vlan_priority <<
  197. VLAN_PRIO_SHIFT);
  198. vlan_tci_mask = mask->vlan_id | (mask->vlan_priority <<
  199. VLAN_PRIO_SHIFT);
  200. fs->val.ivlan = vlan_tci;
  201. fs->mask.ivlan = vlan_tci_mask;
  202. /* Chelsio adapters use ivlan_vld bit to match vlan packets
  203. * as 802.1Q. Also, when vlan tag is present in packets,
  204. * ethtype match is used then to match on ethtype of inner
  205. * header ie. the header following the vlan header.
  206. * So, set the ivlan_vld based on ethtype info supplied by
  207. * TC for vlan packets if its 802.1Q. And then reset the
  208. * ethtype value else, hw will try to match the supplied
  209. * ethtype value with ethtype of inner header.
  210. */
  211. if (fs->val.ethtype == ETH_P_8021Q) {
  212. fs->val.ivlan_vld = 1;
  213. fs->mask.ivlan_vld = 1;
  214. fs->val.ethtype = 0;
  215. fs->mask.ethtype = 0;
  216. }
  217. }
  218. /* Match only packets coming from the ingress port where this
  219. * filter will be created.
  220. */
  221. fs->val.iport = netdev2pinfo(dev)->port_id;
  222. fs->mask.iport = ~0;
  223. }
  224. static int cxgb4_validate_flow_match(struct net_device *dev,
  225. struct tc_cls_flower_offload *cls)
  226. {
  227. u16 ethtype_mask = 0;
  228. u16 ethtype_key = 0;
  229. if (cls->dissector->used_keys &
  230. ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
  231. BIT(FLOW_DISSECTOR_KEY_BASIC) |
  232. BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
  233. BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
  234. BIT(FLOW_DISSECTOR_KEY_PORTS) |
  235. BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
  236. BIT(FLOW_DISSECTOR_KEY_VLAN) |
  237. BIT(FLOW_DISSECTOR_KEY_IP))) {
  238. netdev_warn(dev, "Unsupported key used: 0x%x\n",
  239. cls->dissector->used_keys);
  240. return -EOPNOTSUPP;
  241. }
  242. if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
  243. struct flow_dissector_key_basic *key =
  244. skb_flow_dissector_target(cls->dissector,
  245. FLOW_DISSECTOR_KEY_BASIC,
  246. cls->key);
  247. struct flow_dissector_key_basic *mask =
  248. skb_flow_dissector_target(cls->dissector,
  249. FLOW_DISSECTOR_KEY_BASIC,
  250. cls->mask);
  251. ethtype_key = ntohs(key->n_proto);
  252. ethtype_mask = ntohs(mask->n_proto);
  253. }
  254. if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_IP)) {
  255. u16 eth_ip_type = ethtype_key & ethtype_mask;
  256. struct flow_dissector_key_ip *mask;
  257. if (eth_ip_type != ETH_P_IP && eth_ip_type != ETH_P_IPV6) {
  258. netdev_err(dev, "IP Key supported only with IPv4/v6");
  259. return -EINVAL;
  260. }
  261. mask = skb_flow_dissector_target(cls->dissector,
  262. FLOW_DISSECTOR_KEY_IP,
  263. cls->mask);
  264. if (mask->ttl) {
  265. netdev_warn(dev, "ttl match unsupported for offload");
  266. return -EOPNOTSUPP;
  267. }
  268. }
  269. return 0;
  270. }
  271. static void offload_pedit(struct ch_filter_specification *fs, u32 val, u32 mask,
  272. u8 field)
  273. {
  274. u32 set_val = val & ~mask;
  275. u32 offset = 0;
  276. u8 size = 1;
  277. int i;
  278. for (i = 0; i < ARRAY_SIZE(pedits); i++) {
  279. if (pedits[i].field == field) {
  280. offset = pedits[i].offset;
  281. size = pedits[i].size;
  282. break;
  283. }
  284. }
  285. memcpy((u8 *)fs + offset, &set_val, size);
  286. }
  287. static void process_pedit_field(struct ch_filter_specification *fs, u32 val,
  288. u32 mask, u32 offset, u8 htype)
  289. {
  290. switch (htype) {
  291. case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
  292. switch (offset) {
  293. case PEDIT_ETH_DMAC_31_0:
  294. fs->newdmac = 1;
  295. offload_pedit(fs, val, mask, ETH_DMAC_31_0);
  296. break;
  297. case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
  298. if (~mask & PEDIT_ETH_DMAC_MASK)
  299. offload_pedit(fs, val, mask, ETH_DMAC_47_32);
  300. else
  301. offload_pedit(fs, val >> 16, mask >> 16,
  302. ETH_SMAC_15_0);
  303. break;
  304. case PEDIT_ETH_SMAC_47_16:
  305. fs->newsmac = 1;
  306. offload_pedit(fs, val, mask, ETH_SMAC_47_16);
  307. }
  308. break;
  309. case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
  310. switch (offset) {
  311. case PEDIT_IP4_SRC:
  312. offload_pedit(fs, val, mask, IP4_SRC);
  313. break;
  314. case PEDIT_IP4_DST:
  315. offload_pedit(fs, val, mask, IP4_DST);
  316. }
  317. fs->nat_mode = NAT_MODE_ALL;
  318. break;
  319. case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
  320. switch (offset) {
  321. case PEDIT_IP6_SRC_31_0:
  322. offload_pedit(fs, val, mask, IP6_SRC_31_0);
  323. break;
  324. case PEDIT_IP6_SRC_63_32:
  325. offload_pedit(fs, val, mask, IP6_SRC_63_32);
  326. break;
  327. case PEDIT_IP6_SRC_95_64:
  328. offload_pedit(fs, val, mask, IP6_SRC_95_64);
  329. break;
  330. case PEDIT_IP6_SRC_127_96:
  331. offload_pedit(fs, val, mask, IP6_SRC_127_96);
  332. break;
  333. case PEDIT_IP6_DST_31_0:
  334. offload_pedit(fs, val, mask, IP6_DST_31_0);
  335. break;
  336. case PEDIT_IP6_DST_63_32:
  337. offload_pedit(fs, val, mask, IP6_DST_63_32);
  338. break;
  339. case PEDIT_IP6_DST_95_64:
  340. offload_pedit(fs, val, mask, IP6_DST_95_64);
  341. break;
  342. case PEDIT_IP6_DST_127_96:
  343. offload_pedit(fs, val, mask, IP6_DST_127_96);
  344. }
  345. fs->nat_mode = NAT_MODE_ALL;
  346. break;
  347. case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
  348. switch (offset) {
  349. case PEDIT_TCP_SPORT_DPORT:
  350. if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
  351. offload_pedit(fs, cpu_to_be32(val) >> 16,
  352. cpu_to_be32(mask) >> 16,
  353. TCP_SPORT);
  354. else
  355. offload_pedit(fs, cpu_to_be32(val),
  356. cpu_to_be32(mask), TCP_DPORT);
  357. }
  358. fs->nat_mode = NAT_MODE_ALL;
  359. break;
  360. case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
  361. switch (offset) {
  362. case PEDIT_UDP_SPORT_DPORT:
  363. if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
  364. offload_pedit(fs, cpu_to_be32(val) >> 16,
  365. cpu_to_be32(mask) >> 16,
  366. UDP_SPORT);
  367. else
  368. offload_pedit(fs, cpu_to_be32(val),
  369. cpu_to_be32(mask), UDP_DPORT);
  370. }
  371. fs->nat_mode = NAT_MODE_ALL;
  372. }
  373. }
  374. static void cxgb4_process_flow_actions(struct net_device *in,
  375. struct tc_cls_flower_offload *cls,
  376. struct ch_filter_specification *fs)
  377. {
  378. const struct tc_action *a;
  379. LIST_HEAD(actions);
  380. tcf_exts_to_list(cls->exts, &actions);
  381. list_for_each_entry(a, &actions, list) {
  382. if (is_tcf_gact_ok(a)) {
  383. fs->action = FILTER_PASS;
  384. } else if (is_tcf_gact_shot(a)) {
  385. fs->action = FILTER_DROP;
  386. } else if (is_tcf_mirred_egress_redirect(a)) {
  387. struct net_device *out = tcf_mirred_dev(a);
  388. struct port_info *pi = netdev_priv(out);
  389. fs->action = FILTER_SWITCH;
  390. fs->eport = pi->port_id;
  391. } else if (is_tcf_vlan(a)) {
  392. u32 vlan_action = tcf_vlan_action(a);
  393. u8 prio = tcf_vlan_push_prio(a);
  394. u16 vid = tcf_vlan_push_vid(a);
  395. u16 vlan_tci = (prio << VLAN_PRIO_SHIFT) | vid;
  396. switch (vlan_action) {
  397. case TCA_VLAN_ACT_POP:
  398. fs->newvlan |= VLAN_REMOVE;
  399. break;
  400. case TCA_VLAN_ACT_PUSH:
  401. fs->newvlan |= VLAN_INSERT;
  402. fs->vlan = vlan_tci;
  403. break;
  404. case TCA_VLAN_ACT_MODIFY:
  405. fs->newvlan |= VLAN_REWRITE;
  406. fs->vlan = vlan_tci;
  407. break;
  408. default:
  409. break;
  410. }
  411. } else if (is_tcf_pedit(a)) {
  412. u32 mask, val, offset;
  413. int nkeys, i;
  414. u8 htype;
  415. nkeys = tcf_pedit_nkeys(a);
  416. for (i = 0; i < nkeys; i++) {
  417. htype = tcf_pedit_htype(a, i);
  418. mask = tcf_pedit_mask(a, i);
  419. val = tcf_pedit_val(a, i);
  420. offset = tcf_pedit_offset(a, i);
  421. process_pedit_field(fs, val, mask, offset,
  422. htype);
  423. }
  424. }
  425. }
  426. }
  427. static bool valid_l4_mask(u32 mask)
  428. {
  429. u16 hi, lo;
  430. /* Either the upper 16-bits (SPORT) OR the lower
  431. * 16-bits (DPORT) can be set, but NOT BOTH.
  432. */
  433. hi = (mask >> 16) & 0xFFFF;
  434. lo = mask & 0xFFFF;
  435. return hi && lo ? false : true;
  436. }
  437. static bool valid_pedit_action(struct net_device *dev,
  438. const struct tc_action *a)
  439. {
  440. u32 mask, offset;
  441. u8 cmd, htype;
  442. int nkeys, i;
  443. nkeys = tcf_pedit_nkeys(a);
  444. for (i = 0; i < nkeys; i++) {
  445. htype = tcf_pedit_htype(a, i);
  446. cmd = tcf_pedit_cmd(a, i);
  447. mask = tcf_pedit_mask(a, i);
  448. offset = tcf_pedit_offset(a, i);
  449. if (cmd != TCA_PEDIT_KEY_EX_CMD_SET) {
  450. netdev_err(dev, "%s: Unsupported pedit cmd\n",
  451. __func__);
  452. return false;
  453. }
  454. switch (htype) {
  455. case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
  456. switch (offset) {
  457. case PEDIT_ETH_DMAC_31_0:
  458. case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
  459. case PEDIT_ETH_SMAC_47_16:
  460. break;
  461. default:
  462. netdev_err(dev, "%s: Unsupported pedit field\n",
  463. __func__);
  464. return false;
  465. }
  466. break;
  467. case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
  468. switch (offset) {
  469. case PEDIT_IP4_SRC:
  470. case PEDIT_IP4_DST:
  471. break;
  472. default:
  473. netdev_err(dev, "%s: Unsupported pedit field\n",
  474. __func__);
  475. return false;
  476. }
  477. break;
  478. case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
  479. switch (offset) {
  480. case PEDIT_IP6_SRC_31_0:
  481. case PEDIT_IP6_SRC_63_32:
  482. case PEDIT_IP6_SRC_95_64:
  483. case PEDIT_IP6_SRC_127_96:
  484. case PEDIT_IP6_DST_31_0:
  485. case PEDIT_IP6_DST_63_32:
  486. case PEDIT_IP6_DST_95_64:
  487. case PEDIT_IP6_DST_127_96:
  488. break;
  489. default:
  490. netdev_err(dev, "%s: Unsupported pedit field\n",
  491. __func__);
  492. return false;
  493. }
  494. break;
  495. case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
  496. switch (offset) {
  497. case PEDIT_TCP_SPORT_DPORT:
  498. if (!valid_l4_mask(~mask)) {
  499. netdev_err(dev, "%s: Unsupported mask for TCP L4 ports\n",
  500. __func__);
  501. return false;
  502. }
  503. break;
  504. default:
  505. netdev_err(dev, "%s: Unsupported pedit field\n",
  506. __func__);
  507. return false;
  508. }
  509. break;
  510. case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
  511. switch (offset) {
  512. case PEDIT_UDP_SPORT_DPORT:
  513. if (!valid_l4_mask(~mask)) {
  514. netdev_err(dev, "%s: Unsupported mask for UDP L4 ports\n",
  515. __func__);
  516. return false;
  517. }
  518. break;
  519. default:
  520. netdev_err(dev, "%s: Unsupported pedit field\n",
  521. __func__);
  522. return false;
  523. }
  524. break;
  525. default:
  526. netdev_err(dev, "%s: Unsupported pedit type\n",
  527. __func__);
  528. return false;
  529. }
  530. }
  531. return true;
  532. }
  533. static int cxgb4_validate_flow_actions(struct net_device *dev,
  534. struct tc_cls_flower_offload *cls)
  535. {
  536. const struct tc_action *a;
  537. bool act_redir = false;
  538. bool act_pedit = false;
  539. bool act_vlan = false;
  540. LIST_HEAD(actions);
  541. tcf_exts_to_list(cls->exts, &actions);
  542. list_for_each_entry(a, &actions, list) {
  543. if (is_tcf_gact_ok(a)) {
  544. /* Do nothing */
  545. } else if (is_tcf_gact_shot(a)) {
  546. /* Do nothing */
  547. } else if (is_tcf_mirred_egress_redirect(a)) {
  548. struct adapter *adap = netdev2adap(dev);
  549. struct net_device *n_dev, *target_dev;
  550. unsigned int i;
  551. bool found = false;
  552. target_dev = tcf_mirred_dev(a);
  553. for_each_port(adap, i) {
  554. n_dev = adap->port[i];
  555. if (target_dev == n_dev) {
  556. found = true;
  557. break;
  558. }
  559. }
  560. /* If interface doesn't belong to our hw, then
  561. * the provided output port is not valid
  562. */
  563. if (!found) {
  564. netdev_err(dev, "%s: Out port invalid\n",
  565. __func__);
  566. return -EINVAL;
  567. }
  568. act_redir = true;
  569. } else if (is_tcf_vlan(a)) {
  570. u16 proto = be16_to_cpu(tcf_vlan_push_proto(a));
  571. u32 vlan_action = tcf_vlan_action(a);
  572. switch (vlan_action) {
  573. case TCA_VLAN_ACT_POP:
  574. break;
  575. case TCA_VLAN_ACT_PUSH:
  576. case TCA_VLAN_ACT_MODIFY:
  577. if (proto != ETH_P_8021Q) {
  578. netdev_err(dev, "%s: Unsupported vlan proto\n",
  579. __func__);
  580. return -EOPNOTSUPP;
  581. }
  582. break;
  583. default:
  584. netdev_err(dev, "%s: Unsupported vlan action\n",
  585. __func__);
  586. return -EOPNOTSUPP;
  587. }
  588. act_vlan = true;
  589. } else if (is_tcf_pedit(a)) {
  590. bool pedit_valid = valid_pedit_action(dev, a);
  591. if (!pedit_valid)
  592. return -EOPNOTSUPP;
  593. act_pedit = true;
  594. } else {
  595. netdev_err(dev, "%s: Unsupported action\n", __func__);
  596. return -EOPNOTSUPP;
  597. }
  598. }
  599. if ((act_pedit || act_vlan) && !act_redir) {
  600. netdev_err(dev, "%s: pedit/vlan rewrite invalid without egress redirect\n",
  601. __func__);
  602. return -EINVAL;
  603. }
  604. return 0;
  605. }
  606. int cxgb4_tc_flower_replace(struct net_device *dev,
  607. struct tc_cls_flower_offload *cls)
  608. {
  609. struct adapter *adap = netdev2adap(dev);
  610. struct ch_tc_flower_entry *ch_flower;
  611. struct ch_filter_specification *fs;
  612. struct filter_ctx ctx;
  613. int fidx;
  614. int ret;
  615. if (cxgb4_validate_flow_actions(dev, cls))
  616. return -EOPNOTSUPP;
  617. if (cxgb4_validate_flow_match(dev, cls))
  618. return -EOPNOTSUPP;
  619. ch_flower = allocate_flower_entry();
  620. if (!ch_flower) {
  621. netdev_err(dev, "%s: ch_flower alloc failed.\n", __func__);
  622. return -ENOMEM;
  623. }
  624. fs = &ch_flower->fs;
  625. fs->hitcnts = 1;
  626. cxgb4_process_flow_match(dev, cls, fs);
  627. cxgb4_process_flow_actions(dev, cls, fs);
  628. fs->hash = is_filter_exact_match(adap, fs);
  629. if (fs->hash) {
  630. fidx = 0;
  631. } else {
  632. fidx = cxgb4_get_free_ftid(dev, fs->type ? PF_INET6 : PF_INET);
  633. if (fidx < 0) {
  634. netdev_err(dev, "%s: No fidx for offload.\n", __func__);
  635. ret = -ENOMEM;
  636. goto free_entry;
  637. }
  638. }
  639. init_completion(&ctx.completion);
  640. ret = __cxgb4_set_filter(dev, fidx, fs, &ctx);
  641. if (ret) {
  642. netdev_err(dev, "%s: filter creation err %d\n",
  643. __func__, ret);
  644. goto free_entry;
  645. }
  646. /* Wait for reply */
  647. ret = wait_for_completion_timeout(&ctx.completion, 10 * HZ);
  648. if (!ret) {
  649. ret = -ETIMEDOUT;
  650. goto free_entry;
  651. }
  652. ret = ctx.result;
  653. /* Check if hw returned error for filter creation */
  654. if (ret) {
  655. netdev_err(dev, "%s: filter creation err %d\n",
  656. __func__, ret);
  657. goto free_entry;
  658. }
  659. ch_flower->tc_flower_cookie = cls->cookie;
  660. ch_flower->filter_id = ctx.tid;
  661. ret = rhashtable_insert_fast(&adap->flower_tbl, &ch_flower->node,
  662. adap->flower_ht_params);
  663. if (ret)
  664. goto del_filter;
  665. return 0;
  666. del_filter:
  667. cxgb4_del_filter(dev, ch_flower->filter_id, &ch_flower->fs);
  668. free_entry:
  669. kfree(ch_flower);
  670. return ret;
  671. }
  672. int cxgb4_tc_flower_destroy(struct net_device *dev,
  673. struct tc_cls_flower_offload *cls)
  674. {
  675. struct adapter *adap = netdev2adap(dev);
  676. struct ch_tc_flower_entry *ch_flower;
  677. int ret;
  678. ch_flower = ch_flower_lookup(adap, cls->cookie);
  679. if (!ch_flower)
  680. return -ENOENT;
  681. ret = cxgb4_del_filter(dev, ch_flower->filter_id, &ch_flower->fs);
  682. if (ret)
  683. goto err;
  684. ret = rhashtable_remove_fast(&adap->flower_tbl, &ch_flower->node,
  685. adap->flower_ht_params);
  686. if (ret) {
  687. netdev_err(dev, "Flow remove from rhashtable failed");
  688. goto err;
  689. }
  690. kfree_rcu(ch_flower, rcu);
  691. err:
  692. return ret;
  693. }
  694. static void ch_flower_stats_handler(struct work_struct *work)
  695. {
  696. struct adapter *adap = container_of(work, struct adapter,
  697. flower_stats_work);
  698. struct ch_tc_flower_entry *flower_entry;
  699. struct ch_tc_flower_stats *ofld_stats;
  700. struct rhashtable_iter iter;
  701. u64 packets;
  702. u64 bytes;
  703. int ret;
  704. rhashtable_walk_enter(&adap->flower_tbl, &iter);
  705. do {
  706. rhashtable_walk_start(&iter);
  707. while ((flower_entry = rhashtable_walk_next(&iter)) &&
  708. !IS_ERR(flower_entry)) {
  709. ret = cxgb4_get_filter_counters(adap->port[0],
  710. flower_entry->filter_id,
  711. &packets, &bytes,
  712. flower_entry->fs.hash);
  713. if (!ret) {
  714. spin_lock(&flower_entry->lock);
  715. ofld_stats = &flower_entry->stats;
  716. if (ofld_stats->prev_packet_count != packets) {
  717. ofld_stats->prev_packet_count = packets;
  718. ofld_stats->last_used = jiffies;
  719. }
  720. spin_unlock(&flower_entry->lock);
  721. }
  722. }
  723. rhashtable_walk_stop(&iter);
  724. } while (flower_entry == ERR_PTR(-EAGAIN));
  725. rhashtable_walk_exit(&iter);
  726. mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
  727. }
  728. static void ch_flower_stats_cb(struct timer_list *t)
  729. {
  730. struct adapter *adap = from_timer(adap, t, flower_stats_timer);
  731. schedule_work(&adap->flower_stats_work);
  732. }
  733. int cxgb4_tc_flower_stats(struct net_device *dev,
  734. struct tc_cls_flower_offload *cls)
  735. {
  736. struct adapter *adap = netdev2adap(dev);
  737. struct ch_tc_flower_stats *ofld_stats;
  738. struct ch_tc_flower_entry *ch_flower;
  739. u64 packets;
  740. u64 bytes;
  741. int ret;
  742. ch_flower = ch_flower_lookup(adap, cls->cookie);
  743. if (!ch_flower) {
  744. ret = -ENOENT;
  745. goto err;
  746. }
  747. ret = cxgb4_get_filter_counters(dev, ch_flower->filter_id,
  748. &packets, &bytes,
  749. ch_flower->fs.hash);
  750. if (ret < 0)
  751. goto err;
  752. spin_lock_bh(&ch_flower->lock);
  753. ofld_stats = &ch_flower->stats;
  754. if (ofld_stats->packet_count != packets) {
  755. if (ofld_stats->prev_packet_count != packets)
  756. ofld_stats->last_used = jiffies;
  757. tcf_exts_stats_update(cls->exts, bytes - ofld_stats->byte_count,
  758. packets - ofld_stats->packet_count,
  759. ofld_stats->last_used);
  760. ofld_stats->packet_count = packets;
  761. ofld_stats->byte_count = bytes;
  762. ofld_stats->prev_packet_count = packets;
  763. }
  764. spin_unlock_bh(&ch_flower->lock);
  765. return 0;
  766. err:
  767. return ret;
  768. }
  769. static const struct rhashtable_params cxgb4_tc_flower_ht_params = {
  770. .nelem_hint = 384,
  771. .head_offset = offsetof(struct ch_tc_flower_entry, node),
  772. .key_offset = offsetof(struct ch_tc_flower_entry, tc_flower_cookie),
  773. .key_len = sizeof(((struct ch_tc_flower_entry *)0)->tc_flower_cookie),
  774. .max_size = 524288,
  775. .min_size = 512,
  776. .automatic_shrinking = true
  777. };
  778. int cxgb4_init_tc_flower(struct adapter *adap)
  779. {
  780. int ret;
  781. adap->flower_ht_params = cxgb4_tc_flower_ht_params;
  782. ret = rhashtable_init(&adap->flower_tbl, &adap->flower_ht_params);
  783. if (ret)
  784. return ret;
  785. INIT_WORK(&adap->flower_stats_work, ch_flower_stats_handler);
  786. timer_setup(&adap->flower_stats_timer, ch_flower_stats_cb, 0);
  787. mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
  788. return 0;
  789. }
  790. void cxgb4_cleanup_tc_flower(struct adapter *adap)
  791. {
  792. if (adap->flower_stats_timer.function)
  793. del_timer_sync(&adap->flower_stats_timer);
  794. cancel_work_sync(&adap->flower_stats_work);
  795. rhashtable_destroy(&adap->flower_tbl);
  796. }