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