tc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <net/dsa.h>
  3. #include <linux/delay.h>
  4. #include <linux/etherdevice.h>
  5. #include <linux/netdevice.h>
  6. #include <net/flow_offload.h>
  7. #include <linux/rhashtable.h>
  8. #include <asm/mach-rtl838x/mach-rtl83xx.h>
  9. #include "rtl83xx.h"
  10. #include "rtl838x.h"
  11. /* Parse the flow rule for the matching conditions */
  12. static int rtl83xx_parse_flow_rule(struct rtl838x_switch_priv *priv,
  13. struct flow_rule *rule, struct rtl83xx_flow *flow)
  14. {
  15. struct flow_dissector *dissector = rule->match.dissector;
  16. pr_debug("In %s\n", __func__);
  17. /* KEY_CONTROL and KEY_BASIC are needed for forming a meaningful key */
  18. if ((dissector->used_keys & BIT(FLOW_DISSECTOR_KEY_CONTROL)) == 0 ||
  19. (dissector->used_keys & BIT(FLOW_DISSECTOR_KEY_BASIC)) == 0) {
  20. pr_err("Cannot form TC key: used_keys = 0x%x\n", dissector->used_keys);
  21. return -EOPNOTSUPP;
  22. }
  23. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
  24. struct flow_match_basic match;
  25. pr_debug("%s: BASIC\n", __func__);
  26. flow_rule_match_basic(rule, &match);
  27. if (match.key->n_proto == htons(ETH_P_ARP))
  28. flow->rule.frame_type = 0;
  29. if (match.key->n_proto == htons(ETH_P_IP))
  30. flow->rule.frame_type = 2;
  31. if (match.key->n_proto == htons(ETH_P_IPV6))
  32. flow->rule.frame_type = 3;
  33. if ((match.key->n_proto == htons(ETH_P_ARP)) || flow->rule.frame_type)
  34. flow->rule.frame_type_m = 3;
  35. if (flow->rule.frame_type >= 2) {
  36. if (match.key->ip_proto == IPPROTO_UDP)
  37. flow->rule.frame_type_l4 = 0;
  38. if (match.key->ip_proto == IPPROTO_TCP)
  39. flow->rule.frame_type_l4 = 1;
  40. if (match.key->ip_proto == IPPROTO_ICMP || match.key->ip_proto == IPPROTO_ICMPV6)
  41. flow->rule.frame_type_l4 = 2;
  42. if (match.key->ip_proto == IPPROTO_TCP)
  43. flow->rule.frame_type_l4 = 3;
  44. if ((match.key->ip_proto == IPPROTO_UDP) || flow->rule.frame_type_l4)
  45. flow->rule.frame_type_l4_m = 7;
  46. }
  47. }
  48. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  49. struct flow_match_eth_addrs match;
  50. pr_debug("%s: ETH_ADDR\n", __func__);
  51. flow_rule_match_eth_addrs(rule, &match);
  52. ether_addr_copy(flow->rule.dmac, match.key->dst);
  53. ether_addr_copy(flow->rule.dmac_m, match.mask->dst);
  54. ether_addr_copy(flow->rule.smac, match.key->src);
  55. ether_addr_copy(flow->rule.smac_m, match.mask->src);
  56. }
  57. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
  58. struct flow_match_vlan match;
  59. pr_debug("%s: VLAN\n", __func__);
  60. flow_rule_match_vlan(rule, &match);
  61. flow->rule.itag = match.key->vlan_id;
  62. flow->rule.itag_m = match.mask->vlan_id;
  63. /* TODO: What about match.key->vlan_priority? */
  64. }
  65. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
  66. struct flow_match_ipv4_addrs match;
  67. pr_debug("%s: IPV4\n", __func__);
  68. flow_rule_match_ipv4_addrs(rule, &match);
  69. flow->rule.is_ipv6 = false;
  70. flow->rule.dip = match.key->dst;
  71. flow->rule.dip_m = match.mask->dst;
  72. flow->rule.sip = match.key->src;
  73. flow->rule.sip_m = match.mask->src;
  74. } else if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
  75. struct flow_match_ipv6_addrs match;
  76. pr_debug("%s: IPV6\n", __func__);
  77. flow->rule.is_ipv6 = true;
  78. flow_rule_match_ipv6_addrs(rule, &match);
  79. flow->rule.dip6 = match.key->dst;
  80. flow->rule.dip6_m = match.mask->dst;
  81. flow->rule.sip6 = match.key->src;
  82. flow->rule.sip6_m = match.mask->src;
  83. }
  84. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
  85. struct flow_match_ports match;
  86. pr_debug("%s: PORTS\n", __func__);
  87. flow_rule_match_ports(rule, &match);
  88. flow->rule.dport = match.key->dst;
  89. flow->rule.dport_m = match.mask->dst;
  90. flow->rule.sport = match.key->src;
  91. flow->rule.sport_m = match.mask->src;
  92. }
  93. /* TODO: ICMP */
  94. return 0;
  95. }
  96. static void rtl83xx_flow_bypass_all(struct rtl83xx_flow *flow)
  97. {
  98. flow->rule.bypass_sel = true;
  99. flow->rule.bypass_all = true;
  100. flow->rule.bypass_igr_stp = true;
  101. flow->rule.bypass_ibc_sc = true;
  102. }
  103. static int rtl83xx_parse_fwd(struct rtl838x_switch_priv *priv,
  104. const struct flow_action_entry *act, struct rtl83xx_flow *flow)
  105. {
  106. struct net_device *dev = act->dev;
  107. int port;
  108. port = rtl83xx_port_is_under(dev, priv);
  109. if (port < 0) {
  110. netdev_info(dev, "%s: not a DSA device.\n", __func__);
  111. return -EINVAL;
  112. }
  113. flow->rule.fwd_sel = true;
  114. flow->rule.fwd_data = port;
  115. pr_debug("Using port index: %d\n", port);
  116. rtl83xx_flow_bypass_all(flow);
  117. return 0;
  118. }
  119. static int rtl83xx_add_flow(struct rtl838x_switch_priv *priv, struct flow_cls_offload *f,
  120. struct rtl83xx_flow *flow)
  121. {
  122. struct flow_rule *rule = flow_cls_offload_flow_rule(f);
  123. const struct flow_action_entry *act;
  124. int i, err;
  125. pr_debug("%s\n", __func__);
  126. rtl83xx_parse_flow_rule(priv, rule, flow);
  127. flow_action_for_each(i, act, &rule->action) {
  128. switch (act->id) {
  129. case FLOW_ACTION_DROP:
  130. pr_debug("%s: DROP\n", __func__);
  131. flow->rule.drop = true;
  132. rtl83xx_flow_bypass_all(flow);
  133. return 0;
  134. case FLOW_ACTION_TRAP:
  135. pr_debug("%s: TRAP\n", __func__);
  136. flow->rule.fwd_data = priv->cpu_port;
  137. flow->rule.fwd_act = PIE_ACT_REDIRECT_TO_PORT;
  138. rtl83xx_flow_bypass_all(flow);
  139. break;
  140. case FLOW_ACTION_MANGLE:
  141. pr_err("%s: FLOW_ACTION_MANGLE not supported\n", __func__);
  142. return -EOPNOTSUPP;
  143. case FLOW_ACTION_ADD:
  144. pr_err("%s: FLOW_ACTION_ADD not supported\n", __func__);
  145. return -EOPNOTSUPP;
  146. case FLOW_ACTION_VLAN_PUSH:
  147. pr_debug("%s: VLAN_PUSH\n", __func__);
  148. /* TODO: act->vlan.proto */
  149. flow->rule.ivid_act = PIE_ACT_VID_ASSIGN;
  150. flow->rule.ivid_sel = true;
  151. flow->rule.ivid_data = htons(act->vlan.vid);
  152. flow->rule.ovid_act = PIE_ACT_VID_ASSIGN;
  153. flow->rule.ovid_sel = true;
  154. flow->rule.ovid_data = htons(act->vlan.vid);
  155. flow->rule.fwd_mod_to_cpu = true;
  156. break;
  157. case FLOW_ACTION_VLAN_POP:
  158. pr_debug("%s: VLAN_POP\n", __func__);
  159. flow->rule.ivid_act = PIE_ACT_VID_ASSIGN;
  160. flow->rule.ivid_data = 0;
  161. flow->rule.ivid_sel = true;
  162. flow->rule.ovid_act = PIE_ACT_VID_ASSIGN;
  163. flow->rule.ovid_data = 0;
  164. flow->rule.ovid_sel = true;
  165. flow->rule.fwd_mod_to_cpu = true;
  166. break;
  167. case FLOW_ACTION_CSUM:
  168. pr_err("%s: FLOW_ACTION_CSUM not supported\n", __func__);
  169. return -EOPNOTSUPP;
  170. case FLOW_ACTION_REDIRECT:
  171. pr_debug("%s: REDIRECT\n", __func__);
  172. err = rtl83xx_parse_fwd(priv, act, flow);
  173. if (err)
  174. return err;
  175. flow->rule.fwd_act = PIE_ACT_REDIRECT_TO_PORT;
  176. break;
  177. case FLOW_ACTION_MIRRED:
  178. pr_debug("%s: MIRRED\n", __func__);
  179. err = rtl83xx_parse_fwd(priv, act, flow);
  180. if (err)
  181. return err;
  182. flow->rule.fwd_act = PIE_ACT_COPY_TO_PORT;
  183. break;
  184. default:
  185. pr_err("%s: Flow action not supported: %d\n", __func__, act->id);
  186. return -EOPNOTSUPP;
  187. }
  188. }
  189. return 0;
  190. }
  191. static const struct rhashtable_params tc_ht_params = {
  192. .head_offset = offsetof(struct rtl83xx_flow, node),
  193. .key_offset = offsetof(struct rtl83xx_flow, cookie),
  194. .key_len = sizeof(((struct rtl83xx_flow *)0)->cookie),
  195. .automatic_shrinking = true,
  196. };
  197. static int rtl83xx_configure_flower(struct rtl838x_switch_priv *priv,
  198. struct flow_cls_offload *f)
  199. {
  200. struct rtl83xx_flow *flow;
  201. int err = 0;
  202. pr_debug("In %s\n", __func__);
  203. rcu_read_lock();
  204. pr_debug("Cookie %08lx\n", f->cookie);
  205. flow = rhashtable_lookup(&priv->tc_ht, &f->cookie, tc_ht_params);
  206. if (flow) {
  207. pr_info("%s: Got flow\n", __func__);
  208. err = -EEXIST;
  209. goto rcu_unlock;
  210. }
  211. rcu_unlock:
  212. rcu_read_unlock();
  213. if (flow)
  214. goto out;
  215. pr_debug("%s: New flow\n", __func__);
  216. flow = kzalloc(sizeof(*flow), GFP_KERNEL);
  217. if (!flow) {
  218. err = -ENOMEM;
  219. goto out;
  220. }
  221. flow->cookie = f->cookie;
  222. flow->priv = priv;
  223. err = rhashtable_insert_fast(&priv->tc_ht, &flow->node, tc_ht_params);
  224. if (err) {
  225. pr_err("Could not insert add new rule\n");
  226. goto out_free;
  227. }
  228. rtl83xx_add_flow(priv, f, flow); /* TODO: check error */
  229. /* Add log action to flow */
  230. flow->rule.packet_cntr = rtl83xx_packet_cntr_alloc(priv);
  231. if (flow->rule.packet_cntr >= 0) {
  232. pr_debug("Using packet counter %d\n", flow->rule.packet_cntr);
  233. flow->rule.log_sel = true;
  234. flow->rule.log_data = flow->rule.packet_cntr;
  235. }
  236. err = priv->r->pie_rule_add(priv, &flow->rule);
  237. return err;
  238. out_free:
  239. kfree(flow);
  240. out:
  241. pr_err("%s: error %d\n", __func__, err);
  242. return err;
  243. }
  244. static int rtl83xx_delete_flower(struct rtl838x_switch_priv *priv,
  245. struct flow_cls_offload * cls_flower)
  246. {
  247. struct rtl83xx_flow *flow;
  248. pr_debug("In %s\n", __func__);
  249. rcu_read_lock();
  250. flow = rhashtable_lookup_fast(&priv->tc_ht, &cls_flower->cookie, tc_ht_params);
  251. if (!flow) {
  252. rcu_read_unlock();
  253. return -EINVAL;
  254. }
  255. priv->r->pie_rule_rm(priv, &flow->rule);
  256. rhashtable_remove_fast(&priv->tc_ht, &flow->node, tc_ht_params);
  257. kfree_rcu(flow, rcu_head);
  258. rcu_read_unlock();
  259. return 0;
  260. }
  261. static int rtl83xx_stats_flower(struct rtl838x_switch_priv *priv,
  262. struct flow_cls_offload * cls_flower)
  263. {
  264. struct rtl83xx_flow *flow;
  265. unsigned long lastused = 0;
  266. int total_packets, new_packets;
  267. pr_debug("%s: \n", __func__);
  268. flow = rhashtable_lookup_fast(&priv->tc_ht, &cls_flower->cookie, tc_ht_params);
  269. if (!flow)
  270. return -1;
  271. if (flow->rule.packet_cntr >= 0) {
  272. total_packets = priv->r->packet_cntr_read(flow->rule.packet_cntr);
  273. pr_debug("Total packets: %d\n", total_packets);
  274. new_packets = total_packets - flow->rule.last_packet_cnt;
  275. flow->rule.last_packet_cnt = total_packets;
  276. }
  277. /* TODO: We need a second PIE rule to count the bytes */
  278. flow_stats_update(&cls_flower->stats, 100 * new_packets, new_packets, 0, lastused,
  279. FLOW_ACTION_HW_STATS_IMMEDIATE);
  280. return 0;
  281. }
  282. static int rtl83xx_setup_tc_cls_flower(struct rtl838x_switch_priv *priv,
  283. struct flow_cls_offload *cls_flower)
  284. {
  285. pr_debug("%s: %d\n", __func__, cls_flower->command);
  286. switch (cls_flower->command) {
  287. case FLOW_CLS_REPLACE:
  288. return rtl83xx_configure_flower(priv, cls_flower);
  289. case FLOW_CLS_DESTROY:
  290. return rtl83xx_delete_flower(priv, cls_flower);
  291. case FLOW_CLS_STATS:
  292. return rtl83xx_stats_flower(priv, cls_flower);
  293. default:
  294. return -EOPNOTSUPP;
  295. }
  296. }
  297. static int rtl83xx_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  298. void *cb_priv)
  299. {
  300. struct rtl838x_switch_priv *priv = cb_priv;
  301. switch (type) {
  302. case TC_SETUP_CLSFLOWER:
  303. pr_debug("%s: TC_SETUP_CLSFLOWER\n", __func__);
  304. return rtl83xx_setup_tc_cls_flower(priv, type_data);
  305. default:
  306. return -EOPNOTSUPP;
  307. }
  308. }
  309. static LIST_HEAD(rtl83xx_block_cb_list);
  310. int rtl83xx_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
  311. {
  312. struct rtl838x_switch_priv *priv;
  313. struct flow_block_offload *f = type_data;
  314. static bool first_time = true;
  315. int err;
  316. pr_debug("%s: %d\n", __func__, type);
  317. if(!netdev_uses_dsa(dev)) {
  318. pr_err("%s: no DSA\n", __func__);
  319. return 0;
  320. }
  321. priv = dev->dsa_ptr->ds->priv;
  322. switch (type) {
  323. case TC_SETUP_BLOCK:
  324. if (first_time) {
  325. first_time = false;
  326. err = rhashtable_init(&priv->tc_ht, &tc_ht_params);
  327. if (err)
  328. pr_err("%s: Could not initialize hash table\n", __func__);
  329. }
  330. f->unlocked_driver_cb = true;
  331. return flow_block_cb_setup_simple(type_data,
  332. &rtl83xx_block_cb_list,
  333. rtl83xx_setup_tc_block_cb,
  334. priv, priv, true);
  335. default:
  336. return -EOPNOTSUPP;
  337. }
  338. return 0;
  339. }