tc.c 11 KB

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