cxgb4_tc_flower.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. if (new)
  66. spin_lock_init(&new->lock);
  67. return new;
  68. }
  69. /* Must be called with either RTNL or rcu_read_lock */
  70. static struct ch_tc_flower_entry *ch_flower_lookup(struct adapter *adap,
  71. unsigned long flower_cookie)
  72. {
  73. return rhashtable_lookup_fast(&adap->flower_tbl, &flower_cookie,
  74. adap->flower_ht_params);
  75. }
  76. static void cxgb4_process_flow_match(struct net_device *dev,
  77. struct tc_cls_flower_offload *cls,
  78. struct ch_filter_specification *fs)
  79. {
  80. u16 addr_type = 0;
  81. if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
  82. struct flow_dissector_key_control *key =
  83. skb_flow_dissector_target(cls->dissector,
  84. FLOW_DISSECTOR_KEY_CONTROL,
  85. cls->key);
  86. addr_type = key->addr_type;
  87. }
  88. if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
  89. struct flow_dissector_key_basic *key =
  90. skb_flow_dissector_target(cls->dissector,
  91. FLOW_DISSECTOR_KEY_BASIC,
  92. cls->key);
  93. struct flow_dissector_key_basic *mask =
  94. skb_flow_dissector_target(cls->dissector,
  95. FLOW_DISSECTOR_KEY_BASIC,
  96. cls->mask);
  97. u16 ethtype_key = ntohs(key->n_proto);
  98. u16 ethtype_mask = ntohs(mask->n_proto);
  99. if (ethtype_key == ETH_P_ALL) {
  100. ethtype_key = 0;
  101. ethtype_mask = 0;
  102. }
  103. if (ethtype_key == ETH_P_IPV6)
  104. fs->type = 1;
  105. fs->val.ethtype = ethtype_key;
  106. fs->mask.ethtype = ethtype_mask;
  107. fs->val.proto = key->ip_proto;
  108. fs->mask.proto = mask->ip_proto;
  109. }
  110. if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  111. struct flow_dissector_key_ipv4_addrs *key =
  112. skb_flow_dissector_target(cls->dissector,
  113. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  114. cls->key);
  115. struct flow_dissector_key_ipv4_addrs *mask =
  116. skb_flow_dissector_target(cls->dissector,
  117. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  118. cls->mask);
  119. fs->type = 0;
  120. memcpy(&fs->val.lip[0], &key->dst, sizeof(key->dst));
  121. memcpy(&fs->val.fip[0], &key->src, sizeof(key->src));
  122. memcpy(&fs->mask.lip[0], &mask->dst, sizeof(mask->dst));
  123. memcpy(&fs->mask.fip[0], &mask->src, sizeof(mask->src));
  124. /* also initialize nat_lip/fip to same values */
  125. memcpy(&fs->nat_lip[0], &key->dst, sizeof(key->dst));
  126. memcpy(&fs->nat_fip[0], &key->src, sizeof(key->src));
  127. }
  128. if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  129. struct flow_dissector_key_ipv6_addrs *key =
  130. skb_flow_dissector_target(cls->dissector,
  131. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  132. cls->key);
  133. struct flow_dissector_key_ipv6_addrs *mask =
  134. skb_flow_dissector_target(cls->dissector,
  135. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  136. cls->mask);
  137. fs->type = 1;
  138. memcpy(&fs->val.lip[0], key->dst.s6_addr, sizeof(key->dst));
  139. memcpy(&fs->val.fip[0], key->src.s6_addr, sizeof(key->src));
  140. memcpy(&fs->mask.lip[0], mask->dst.s6_addr, sizeof(mask->dst));
  141. memcpy(&fs->mask.fip[0], mask->src.s6_addr, sizeof(mask->src));
  142. /* also initialize nat_lip/fip to same values */
  143. memcpy(&fs->nat_lip[0], key->dst.s6_addr, sizeof(key->dst));
  144. memcpy(&fs->nat_fip[0], key->src.s6_addr, sizeof(key->src));
  145. }
  146. if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
  147. struct flow_dissector_key_ports *key, *mask;
  148. key = skb_flow_dissector_target(cls->dissector,
  149. FLOW_DISSECTOR_KEY_PORTS,
  150. cls->key);
  151. mask = skb_flow_dissector_target(cls->dissector,
  152. FLOW_DISSECTOR_KEY_PORTS,
  153. cls->mask);
  154. fs->val.lport = cpu_to_be16(key->dst);
  155. fs->mask.lport = cpu_to_be16(mask->dst);
  156. fs->val.fport = cpu_to_be16(key->src);
  157. fs->mask.fport = cpu_to_be16(mask->src);
  158. /* also initialize nat_lport/fport to same values */
  159. fs->nat_lport = cpu_to_be16(key->dst);
  160. fs->nat_fport = cpu_to_be16(key->src);
  161. }
  162. if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_IP)) {
  163. struct flow_dissector_key_ip *key, *mask;
  164. key = skb_flow_dissector_target(cls->dissector,
  165. FLOW_DISSECTOR_KEY_IP,
  166. cls->key);
  167. mask = skb_flow_dissector_target(cls->dissector,
  168. FLOW_DISSECTOR_KEY_IP,
  169. cls->mask);
  170. fs->val.tos = key->tos;
  171. fs->mask.tos = mask->tos;
  172. }
  173. if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
  174. struct flow_dissector_key_keyid *key, *mask;
  175. key = skb_flow_dissector_target(cls->dissector,
  176. FLOW_DISSECTOR_KEY_ENC_KEYID,
  177. cls->key);
  178. mask = skb_flow_dissector_target(cls->dissector,
  179. FLOW_DISSECTOR_KEY_ENC_KEYID,
  180. cls->mask);
  181. fs->val.vni = be32_to_cpu(key->keyid);
  182. fs->mask.vni = be32_to_cpu(mask->keyid);
  183. if (fs->mask.vni) {
  184. fs->val.encap_vld = 1;
  185. fs->mask.encap_vld = 1;
  186. }
  187. }
  188. if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
  189. struct flow_dissector_key_vlan *key, *mask;
  190. u16 vlan_tci, vlan_tci_mask;
  191. key = skb_flow_dissector_target(cls->dissector,
  192. FLOW_DISSECTOR_KEY_VLAN,
  193. cls->key);
  194. mask = skb_flow_dissector_target(cls->dissector,
  195. FLOW_DISSECTOR_KEY_VLAN,
  196. cls->mask);
  197. vlan_tci = key->vlan_id | (key->vlan_priority <<
  198. VLAN_PRIO_SHIFT);
  199. vlan_tci_mask = mask->vlan_id | (mask->vlan_priority <<
  200. VLAN_PRIO_SHIFT);
  201. fs->val.ivlan = vlan_tci;
  202. fs->mask.ivlan = vlan_tci_mask;
  203. fs->val.ivlan_vld = 1;
  204. fs->mask.ivlan_vld = 1;
  205. /* Chelsio adapters use ivlan_vld bit to match vlan packets
  206. * as 802.1Q. Also, when vlan tag is present in packets,
  207. * ethtype match is used then to match on ethtype of inner
  208. * header ie. the header following the vlan header.
  209. * So, set the ivlan_vld based on ethtype info supplied by
  210. * TC for vlan packets if its 802.1Q. And then reset the
  211. * ethtype value else, hw will try to match the supplied
  212. * ethtype value with ethtype of inner header.
  213. */
  214. if (fs->val.ethtype == ETH_P_8021Q) {
  215. fs->val.ethtype = 0;
  216. fs->mask.ethtype = 0;
  217. }
  218. }
  219. /* Match only packets coming from the ingress port where this
  220. * filter will be created.
  221. */
  222. fs->val.iport = netdev2pinfo(dev)->port_id;
  223. fs->mask.iport = ~0;
  224. }
  225. static int cxgb4_validate_flow_match(struct net_device *dev,
  226. struct tc_cls_flower_offload *cls)
  227. {
  228. u16 ethtype_mask = 0;
  229. u16 ethtype_key = 0;
  230. if (cls->dissector->used_keys &
  231. ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
  232. BIT(FLOW_DISSECTOR_KEY_BASIC) |
  233. BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
  234. BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
  235. BIT(FLOW_DISSECTOR_KEY_PORTS) |
  236. BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
  237. BIT(FLOW_DISSECTOR_KEY_VLAN) |
  238. BIT(FLOW_DISSECTOR_KEY_IP))) {
  239. netdev_warn(dev, "Unsupported key used: 0x%x\n",
  240. cls->dissector->used_keys);
  241. return -EOPNOTSUPP;
  242. }
  243. if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
  244. struct flow_dissector_key_basic *key =
  245. skb_flow_dissector_target(cls->dissector,
  246. FLOW_DISSECTOR_KEY_BASIC,
  247. cls->key);
  248. struct flow_dissector_key_basic *mask =
  249. skb_flow_dissector_target(cls->dissector,
  250. FLOW_DISSECTOR_KEY_BASIC,
  251. cls->mask);
  252. ethtype_key = ntohs(key->n_proto);
  253. ethtype_mask = ntohs(mask->n_proto);
  254. }
  255. if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_IP)) {
  256. u16 eth_ip_type = ethtype_key & ethtype_mask;
  257. struct flow_dissector_key_ip *mask;
  258. if (eth_ip_type != ETH_P_IP && eth_ip_type != ETH_P_IPV6) {
  259. netdev_err(dev, "IP Key supported only with IPv4/v6");
  260. return -EINVAL;
  261. }
  262. mask = skb_flow_dissector_target(cls->dissector,
  263. FLOW_DISSECTOR_KEY_IP,
  264. cls->mask);
  265. if (mask->ttl) {
  266. netdev_warn(dev, "ttl match unsupported for offload");
  267. return -EOPNOTSUPP;
  268. }
  269. }
  270. return 0;
  271. }
  272. static void offload_pedit(struct ch_filter_specification *fs, u32 val, u32 mask,
  273. u8 field)
  274. {
  275. u32 set_val = val & ~mask;
  276. u32 offset = 0;
  277. u8 size = 1;
  278. int i;
  279. for (i = 0; i < ARRAY_SIZE(pedits); i++) {
  280. if (pedits[i].field == field) {
  281. offset = pedits[i].offset;
  282. size = pedits[i].size;
  283. break;
  284. }
  285. }
  286. memcpy((u8 *)fs + offset, &set_val, size);
  287. }
  288. static void process_pedit_field(struct ch_filter_specification *fs, u32 val,
  289. u32 mask, u32 offset, u8 htype)
  290. {
  291. switch (htype) {
  292. case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
  293. switch (offset) {
  294. case PEDIT_ETH_DMAC_31_0:
  295. fs->newdmac = 1;
  296. offload_pedit(fs, val, mask, ETH_DMAC_31_0);
  297. break;
  298. case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
  299. if (~mask & PEDIT_ETH_DMAC_MASK)
  300. offload_pedit(fs, val, mask, ETH_DMAC_47_32);
  301. else
  302. offload_pedit(fs, val >> 16, mask >> 16,
  303. ETH_SMAC_15_0);
  304. break;
  305. case PEDIT_ETH_SMAC_47_16:
  306. fs->newsmac = 1;
  307. offload_pedit(fs, val, mask, ETH_SMAC_47_16);
  308. }
  309. break;
  310. case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
  311. switch (offset) {
  312. case PEDIT_IP4_SRC:
  313. offload_pedit(fs, val, mask, IP4_SRC);
  314. break;
  315. case PEDIT_IP4_DST:
  316. offload_pedit(fs, val, mask, IP4_DST);
  317. }
  318. fs->nat_mode = NAT_MODE_ALL;
  319. break;
  320. case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
  321. switch (offset) {
  322. case PEDIT_IP6_SRC_31_0:
  323. offload_pedit(fs, val, mask, IP6_SRC_31_0);
  324. break;
  325. case PEDIT_IP6_SRC_63_32:
  326. offload_pedit(fs, val, mask, IP6_SRC_63_32);
  327. break;
  328. case PEDIT_IP6_SRC_95_64:
  329. offload_pedit(fs, val, mask, IP6_SRC_95_64);
  330. break;
  331. case PEDIT_IP6_SRC_127_96:
  332. offload_pedit(fs, val, mask, IP6_SRC_127_96);
  333. break;
  334. case PEDIT_IP6_DST_31_0:
  335. offload_pedit(fs, val, mask, IP6_DST_31_0);
  336. break;
  337. case PEDIT_IP6_DST_63_32:
  338. offload_pedit(fs, val, mask, IP6_DST_63_32);
  339. break;
  340. case PEDIT_IP6_DST_95_64:
  341. offload_pedit(fs, val, mask, IP6_DST_95_64);
  342. break;
  343. case PEDIT_IP6_DST_127_96:
  344. offload_pedit(fs, val, mask, IP6_DST_127_96);
  345. }
  346. fs->nat_mode = NAT_MODE_ALL;
  347. break;
  348. case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
  349. switch (offset) {
  350. case PEDIT_TCP_SPORT_DPORT:
  351. if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
  352. offload_pedit(fs, cpu_to_be32(val) >> 16,
  353. cpu_to_be32(mask) >> 16,
  354. TCP_SPORT);
  355. else
  356. offload_pedit(fs, cpu_to_be32(val),
  357. cpu_to_be32(mask), TCP_DPORT);
  358. }
  359. fs->nat_mode = NAT_MODE_ALL;
  360. break;
  361. case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
  362. switch (offset) {
  363. case PEDIT_UDP_SPORT_DPORT:
  364. if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
  365. offload_pedit(fs, cpu_to_be32(val) >> 16,
  366. cpu_to_be32(mask) >> 16,
  367. UDP_SPORT);
  368. else
  369. offload_pedit(fs, cpu_to_be32(val),
  370. cpu_to_be32(mask), UDP_DPORT);
  371. }
  372. fs->nat_mode = NAT_MODE_ALL;
  373. }
  374. }
  375. static void cxgb4_process_flow_actions(struct net_device *in,
  376. struct tc_cls_flower_offload *cls,
  377. struct ch_filter_specification *fs)
  378. {
  379. const struct tc_action *a;
  380. int i;
  381. tcf_exts_for_each_action(i, a, cls->exts) {
  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. int i;
  541. tcf_exts_for_each_action(i, a, cls->exts) {
  542. if (is_tcf_gact_ok(a)) {
  543. /* Do nothing */
  544. } else if (is_tcf_gact_shot(a)) {
  545. /* Do nothing */
  546. } else if (is_tcf_mirred_egress_redirect(a)) {
  547. struct adapter *adap = netdev2adap(dev);
  548. struct net_device *n_dev, *target_dev;
  549. unsigned int i;
  550. bool found = false;
  551. target_dev = tcf_mirred_dev(a);
  552. for_each_port(adap, i) {
  553. n_dev = adap->port[i];
  554. if (target_dev == n_dev) {
  555. found = true;
  556. break;
  557. }
  558. }
  559. /* If interface doesn't belong to our hw, then
  560. * the provided output port is not valid
  561. */
  562. if (!found) {
  563. netdev_err(dev, "%s: Out port invalid\n",
  564. __func__);
  565. return -EINVAL;
  566. }
  567. act_redir = true;
  568. } else if (is_tcf_vlan(a)) {
  569. u16 proto = be16_to_cpu(tcf_vlan_push_proto(a));
  570. u32 vlan_action = tcf_vlan_action(a);
  571. switch (vlan_action) {
  572. case TCA_VLAN_ACT_POP:
  573. break;
  574. case TCA_VLAN_ACT_PUSH:
  575. case TCA_VLAN_ACT_MODIFY:
  576. if (proto != ETH_P_8021Q) {
  577. netdev_err(dev, "%s: Unsupported vlan proto\n",
  578. __func__);
  579. return -EOPNOTSUPP;
  580. }
  581. break;
  582. default:
  583. netdev_err(dev, "%s: Unsupported vlan action\n",
  584. __func__);
  585. return -EOPNOTSUPP;
  586. }
  587. act_vlan = true;
  588. } else if (is_tcf_pedit(a)) {
  589. bool pedit_valid = valid_pedit_action(dev, a);
  590. if (!pedit_valid)
  591. return -EOPNOTSUPP;
  592. act_pedit = true;
  593. } else {
  594. netdev_err(dev, "%s: Unsupported action\n", __func__);
  595. return -EOPNOTSUPP;
  596. }
  597. }
  598. if ((act_pedit || act_vlan) && !act_redir) {
  599. netdev_err(dev, "%s: pedit/vlan rewrite invalid without egress redirect\n",
  600. __func__);
  601. return -EINVAL;
  602. }
  603. return 0;
  604. }
  605. int cxgb4_tc_flower_replace(struct net_device *dev,
  606. struct tc_cls_flower_offload *cls)
  607. {
  608. struct adapter *adap = netdev2adap(dev);
  609. struct ch_tc_flower_entry *ch_flower;
  610. struct ch_filter_specification *fs;
  611. struct filter_ctx ctx;
  612. int fidx;
  613. int ret;
  614. if (cxgb4_validate_flow_actions(dev, cls))
  615. return -EOPNOTSUPP;
  616. if (cxgb4_validate_flow_match(dev, cls))
  617. return -EOPNOTSUPP;
  618. ch_flower = allocate_flower_entry();
  619. if (!ch_flower) {
  620. netdev_err(dev, "%s: ch_flower alloc failed.\n", __func__);
  621. return -ENOMEM;
  622. }
  623. fs = &ch_flower->fs;
  624. fs->hitcnts = 1;
  625. cxgb4_process_flow_match(dev, cls, fs);
  626. cxgb4_process_flow_actions(dev, cls, fs);
  627. fs->hash = is_filter_exact_match(adap, fs);
  628. if (fs->hash) {
  629. fidx = 0;
  630. } else {
  631. fidx = cxgb4_get_free_ftid(dev, fs->type ? PF_INET6 : PF_INET);
  632. if (fidx < 0) {
  633. netdev_err(dev, "%s: No fidx for offload.\n", __func__);
  634. ret = -ENOMEM;
  635. goto free_entry;
  636. }
  637. }
  638. init_completion(&ctx.completion);
  639. ret = __cxgb4_set_filter(dev, fidx, fs, &ctx);
  640. if (ret) {
  641. netdev_err(dev, "%s: filter creation err %d\n",
  642. __func__, ret);
  643. goto free_entry;
  644. }
  645. /* Wait for reply */
  646. ret = wait_for_completion_timeout(&ctx.completion, 10 * HZ);
  647. if (!ret) {
  648. ret = -ETIMEDOUT;
  649. goto free_entry;
  650. }
  651. ret = ctx.result;
  652. /* Check if hw returned error for filter creation */
  653. if (ret) {
  654. netdev_err(dev, "%s: filter creation err %d\n",
  655. __func__, ret);
  656. goto free_entry;
  657. }
  658. ch_flower->tc_flower_cookie = cls->cookie;
  659. ch_flower->filter_id = ctx.tid;
  660. ret = rhashtable_insert_fast(&adap->flower_tbl, &ch_flower->node,
  661. adap->flower_ht_params);
  662. if (ret)
  663. goto del_filter;
  664. return 0;
  665. del_filter:
  666. cxgb4_del_filter(dev, ch_flower->filter_id, &ch_flower->fs);
  667. free_entry:
  668. kfree(ch_flower);
  669. return ret;
  670. }
  671. int cxgb4_tc_flower_destroy(struct net_device *dev,
  672. struct tc_cls_flower_offload *cls)
  673. {
  674. struct adapter *adap = netdev2adap(dev);
  675. struct ch_tc_flower_entry *ch_flower;
  676. int ret;
  677. ch_flower = ch_flower_lookup(adap, cls->cookie);
  678. if (!ch_flower)
  679. return -ENOENT;
  680. ret = cxgb4_del_filter(dev, ch_flower->filter_id, &ch_flower->fs);
  681. if (ret)
  682. goto err;
  683. ret = rhashtable_remove_fast(&adap->flower_tbl, &ch_flower->node,
  684. adap->flower_ht_params);
  685. if (ret) {
  686. netdev_err(dev, "Flow remove from rhashtable failed");
  687. goto err;
  688. }
  689. kfree_rcu(ch_flower, rcu);
  690. err:
  691. return ret;
  692. }
  693. static void ch_flower_stats_handler(struct work_struct *work)
  694. {
  695. struct adapter *adap = container_of(work, struct adapter,
  696. flower_stats_work);
  697. struct ch_tc_flower_entry *flower_entry;
  698. struct ch_tc_flower_stats *ofld_stats;
  699. struct rhashtable_iter iter;
  700. u64 packets;
  701. u64 bytes;
  702. int ret;
  703. rhashtable_walk_enter(&adap->flower_tbl, &iter);
  704. do {
  705. rhashtable_walk_start(&iter);
  706. while ((flower_entry = rhashtable_walk_next(&iter)) &&
  707. !IS_ERR(flower_entry)) {
  708. ret = cxgb4_get_filter_counters(adap->port[0],
  709. flower_entry->filter_id,
  710. &packets, &bytes,
  711. flower_entry->fs.hash);
  712. if (!ret) {
  713. spin_lock(&flower_entry->lock);
  714. ofld_stats = &flower_entry->stats;
  715. if (ofld_stats->prev_packet_count != packets) {
  716. ofld_stats->prev_packet_count = packets;
  717. ofld_stats->last_used = jiffies;
  718. }
  719. spin_unlock(&flower_entry->lock);
  720. }
  721. }
  722. rhashtable_walk_stop(&iter);
  723. } while (flower_entry == ERR_PTR(-EAGAIN));
  724. rhashtable_walk_exit(&iter);
  725. mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
  726. }
  727. static void ch_flower_stats_cb(struct timer_list *t)
  728. {
  729. struct adapter *adap = from_timer(adap, t, flower_stats_timer);
  730. schedule_work(&adap->flower_stats_work);
  731. }
  732. int cxgb4_tc_flower_stats(struct net_device *dev,
  733. struct tc_cls_flower_offload *cls)
  734. {
  735. struct adapter *adap = netdev2adap(dev);
  736. struct ch_tc_flower_stats *ofld_stats;
  737. struct ch_tc_flower_entry *ch_flower;
  738. u64 packets;
  739. u64 bytes;
  740. int ret;
  741. ch_flower = ch_flower_lookup(adap, cls->cookie);
  742. if (!ch_flower) {
  743. ret = -ENOENT;
  744. goto err;
  745. }
  746. ret = cxgb4_get_filter_counters(dev, ch_flower->filter_id,
  747. &packets, &bytes,
  748. ch_flower->fs.hash);
  749. if (ret < 0)
  750. goto err;
  751. spin_lock_bh(&ch_flower->lock);
  752. ofld_stats = &ch_flower->stats;
  753. if (ofld_stats->packet_count != packets) {
  754. if (ofld_stats->prev_packet_count != packets)
  755. ofld_stats->last_used = jiffies;
  756. tcf_exts_stats_update(cls->exts, bytes - ofld_stats->byte_count,
  757. packets - ofld_stats->packet_count,
  758. ofld_stats->last_used);
  759. ofld_stats->packet_count = packets;
  760. ofld_stats->byte_count = bytes;
  761. ofld_stats->prev_packet_count = packets;
  762. }
  763. spin_unlock_bh(&ch_flower->lock);
  764. return 0;
  765. err:
  766. return ret;
  767. }
  768. static const struct rhashtable_params cxgb4_tc_flower_ht_params = {
  769. .nelem_hint = 384,
  770. .head_offset = offsetof(struct ch_tc_flower_entry, node),
  771. .key_offset = offsetof(struct ch_tc_flower_entry, tc_flower_cookie),
  772. .key_len = sizeof(((struct ch_tc_flower_entry *)0)->tc_flower_cookie),
  773. .max_size = 524288,
  774. .min_size = 512,
  775. .automatic_shrinking = true
  776. };
  777. int cxgb4_init_tc_flower(struct adapter *adap)
  778. {
  779. int ret;
  780. if (adap->tc_flower_initialized)
  781. return -EEXIST;
  782. adap->flower_ht_params = cxgb4_tc_flower_ht_params;
  783. ret = rhashtable_init(&adap->flower_tbl, &adap->flower_ht_params);
  784. if (ret)
  785. return ret;
  786. INIT_WORK(&adap->flower_stats_work, ch_flower_stats_handler);
  787. timer_setup(&adap->flower_stats_timer, ch_flower_stats_cb, 0);
  788. mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
  789. adap->tc_flower_initialized = true;
  790. return 0;
  791. }
  792. void cxgb4_cleanup_tc_flower(struct adapter *adap)
  793. {
  794. if (!adap->tc_flower_initialized)
  795. return;
  796. if (adap->flower_stats_timer.function)
  797. del_timer_sync(&adap->flower_stats_timer);
  798. cancel_work_sync(&adap->flower_stats_work);
  799. rhashtable_destroy(&adap->flower_tbl);
  800. adap->tc_flower_initialized = false;
  801. }