spectrum_dpipe.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * drivers/net/ethernet/mellanox/mlxsw/spectrum_dpipe.c
  3. * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2017 Arkadi Sharshevsky <arakdis@mellanox.com>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the names of the copyright holders nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * Alternatively, this software may be distributed under the terms of the
  19. * GNU General Public License ("GPL") version 2 as published by the Free
  20. * Software Foundation.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <linux/kernel.h>
  35. #include <net/devlink.h>
  36. #include "spectrum.h"
  37. #include "spectrum_dpipe.h"
  38. #include "spectrum_router.h"
  39. enum mlxsw_sp_field_metadata_id {
  40. MLXSW_SP_DPIPE_FIELD_METADATA_ERIF_PORT,
  41. MLXSW_SP_DPIPE_FIELD_METADATA_L3_FORWARD,
  42. MLXSW_SP_DPIPE_FIELD_METADATA_L3_DROP,
  43. };
  44. static struct devlink_dpipe_field mlxsw_sp_dpipe_fields_metadata[] = {
  45. { .name = "erif_port",
  46. .id = MLXSW_SP_DPIPE_FIELD_METADATA_ERIF_PORT,
  47. .bitwidth = 32,
  48. .mapping_type = DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX,
  49. },
  50. { .name = "l3_forward",
  51. .id = MLXSW_SP_DPIPE_FIELD_METADATA_L3_FORWARD,
  52. .bitwidth = 1,
  53. },
  54. { .name = "l3_drop",
  55. .id = MLXSW_SP_DPIPE_FIELD_METADATA_L3_DROP,
  56. .bitwidth = 1,
  57. },
  58. };
  59. enum mlxsw_sp_dpipe_header_id {
  60. MLXSW_SP_DPIPE_HEADER_METADATA,
  61. };
  62. static struct devlink_dpipe_header mlxsw_sp_dpipe_header_metadata = {
  63. .name = "mlxsw_meta",
  64. .id = MLXSW_SP_DPIPE_HEADER_METADATA,
  65. .fields = mlxsw_sp_dpipe_fields_metadata,
  66. .fields_count = ARRAY_SIZE(mlxsw_sp_dpipe_fields_metadata),
  67. };
  68. static struct devlink_dpipe_header *mlxsw_dpipe_headers[] = {
  69. &mlxsw_sp_dpipe_header_metadata,
  70. };
  71. static struct devlink_dpipe_headers mlxsw_sp_dpipe_headers = {
  72. .headers = mlxsw_dpipe_headers,
  73. .headers_count = ARRAY_SIZE(mlxsw_dpipe_headers),
  74. };
  75. static int mlxsw_sp_dpipe_table_erif_actions_dump(void *priv,
  76. struct sk_buff *skb)
  77. {
  78. struct devlink_dpipe_action action = {0};
  79. int err;
  80. action.type = DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY;
  81. action.header = &mlxsw_sp_dpipe_header_metadata;
  82. action.field_id = MLXSW_SP_DPIPE_FIELD_METADATA_L3_FORWARD;
  83. err = devlink_dpipe_action_put(skb, &action);
  84. if (err)
  85. return err;
  86. action.type = DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY;
  87. action.header = &mlxsw_sp_dpipe_header_metadata;
  88. action.field_id = MLXSW_SP_DPIPE_FIELD_METADATA_L3_DROP;
  89. return devlink_dpipe_action_put(skb, &action);
  90. }
  91. static int mlxsw_sp_dpipe_table_erif_matches_dump(void *priv,
  92. struct sk_buff *skb)
  93. {
  94. struct devlink_dpipe_match match = {0};
  95. match.type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
  96. match.header = &mlxsw_sp_dpipe_header_metadata;
  97. match.field_id = MLXSW_SP_DPIPE_FIELD_METADATA_ERIF_PORT;
  98. return devlink_dpipe_match_put(skb, &match);
  99. }
  100. static void mlxsw_sp_erif_entry_clear(struct devlink_dpipe_entry *entry)
  101. {
  102. unsigned int value_count, value_index;
  103. struct devlink_dpipe_value *value;
  104. value = entry->action_values;
  105. value_count = entry->action_values_count;
  106. for (value_index = 0; value_index < value_count; value_index++) {
  107. kfree(value[value_index].value);
  108. kfree(value[value_index].mask);
  109. }
  110. value = entry->match_values;
  111. value_count = entry->match_values_count;
  112. for (value_index = 0; value_index < value_count; value_index++) {
  113. kfree(value[value_index].value);
  114. kfree(value[value_index].mask);
  115. }
  116. }
  117. static void
  118. mlxsw_sp_erif_match_action_prepare(struct devlink_dpipe_match *match,
  119. struct devlink_dpipe_action *action)
  120. {
  121. action->type = DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY;
  122. action->header = &mlxsw_sp_dpipe_header_metadata;
  123. action->field_id = MLXSW_SP_DPIPE_FIELD_METADATA_L3_FORWARD;
  124. match->type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
  125. match->header = &mlxsw_sp_dpipe_header_metadata;
  126. match->field_id = MLXSW_SP_DPIPE_FIELD_METADATA_ERIF_PORT;
  127. }
  128. static int mlxsw_sp_erif_entry_prepare(struct devlink_dpipe_entry *entry,
  129. struct devlink_dpipe_value *match_value,
  130. struct devlink_dpipe_match *match,
  131. struct devlink_dpipe_value *action_value,
  132. struct devlink_dpipe_action *action)
  133. {
  134. entry->match_values = match_value;
  135. entry->match_values_count = 1;
  136. entry->action_values = action_value;
  137. entry->action_values_count = 1;
  138. match_value->match = match;
  139. match_value->value_size = sizeof(u32);
  140. match_value->value = kmalloc(match_value->value_size, GFP_KERNEL);
  141. if (!match_value->value)
  142. return -ENOMEM;
  143. action_value->action = action;
  144. action_value->value_size = sizeof(u32);
  145. action_value->value = kmalloc(action_value->value_size, GFP_KERNEL);
  146. if (!action_value->value)
  147. goto err_action_alloc;
  148. return 0;
  149. err_action_alloc:
  150. kfree(match_value->value);
  151. return -ENOMEM;
  152. }
  153. static int mlxsw_sp_erif_entry_get(struct mlxsw_sp *mlxsw_sp,
  154. struct devlink_dpipe_entry *entry,
  155. struct mlxsw_sp_rif *rif,
  156. bool counters_enabled)
  157. {
  158. u32 *action_value;
  159. u32 *rif_value;
  160. u64 cnt;
  161. int err;
  162. /* Set Match RIF index */
  163. rif_value = entry->match_values->value;
  164. *rif_value = mlxsw_sp_rif_index(rif);
  165. entry->match_values->mapping_value = mlxsw_sp_rif_dev_ifindex(rif);
  166. entry->match_values->mapping_valid = true;
  167. /* Set Action Forwarding */
  168. action_value = entry->action_values->value;
  169. *action_value = 1;
  170. entry->counter_valid = false;
  171. entry->counter = 0;
  172. entry->index = mlxsw_sp_rif_index(rif);
  173. if (!counters_enabled)
  174. return 0;
  175. err = mlxsw_sp_rif_counter_value_get(mlxsw_sp, rif,
  176. MLXSW_SP_RIF_COUNTER_EGRESS,
  177. &cnt);
  178. if (!err) {
  179. entry->counter = cnt;
  180. entry->counter_valid = true;
  181. }
  182. return 0;
  183. }
  184. static int
  185. mlxsw_sp_table_erif_entries_dump(void *priv, bool counters_enabled,
  186. struct devlink_dpipe_dump_ctx *dump_ctx)
  187. {
  188. struct devlink_dpipe_value match_value = {{0}}, action_value = {{0}};
  189. struct devlink_dpipe_action action = {0};
  190. struct devlink_dpipe_match match = {0};
  191. struct devlink_dpipe_entry entry = {0};
  192. struct mlxsw_sp *mlxsw_sp = priv;
  193. unsigned int rif_count;
  194. int i, j;
  195. int err;
  196. mlxsw_sp_erif_match_action_prepare(&match, &action);
  197. err = mlxsw_sp_erif_entry_prepare(&entry, &match_value, &match,
  198. &action_value, &action);
  199. if (err)
  200. return err;
  201. rif_count = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
  202. rtnl_lock();
  203. i = 0;
  204. start_again:
  205. err = devlink_dpipe_entry_ctx_prepare(dump_ctx);
  206. if (err)
  207. return err;
  208. j = 0;
  209. for (; i < rif_count; i++) {
  210. if (!mlxsw_sp->rifs[i])
  211. continue;
  212. err = mlxsw_sp_erif_entry_get(mlxsw_sp, &entry,
  213. mlxsw_sp->rifs[i],
  214. counters_enabled);
  215. if (err)
  216. goto err_entry_get;
  217. err = devlink_dpipe_entry_ctx_append(dump_ctx, &entry);
  218. if (err) {
  219. if (err == -EMSGSIZE) {
  220. if (!j)
  221. goto err_entry_append;
  222. break;
  223. }
  224. goto err_entry_append;
  225. }
  226. j++;
  227. }
  228. devlink_dpipe_entry_ctx_close(dump_ctx);
  229. if (i != rif_count)
  230. goto start_again;
  231. rtnl_unlock();
  232. mlxsw_sp_erif_entry_clear(&entry);
  233. return 0;
  234. err_entry_append:
  235. err_entry_get:
  236. rtnl_unlock();
  237. mlxsw_sp_erif_entry_clear(&entry);
  238. return err;
  239. }
  240. static int mlxsw_sp_table_erif_counters_update(void *priv, bool enable)
  241. {
  242. struct mlxsw_sp *mlxsw_sp = priv;
  243. int i;
  244. rtnl_lock();
  245. for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++) {
  246. if (!mlxsw_sp->rifs[i])
  247. continue;
  248. if (enable)
  249. mlxsw_sp_rif_counter_alloc(mlxsw_sp,
  250. mlxsw_sp->rifs[i],
  251. MLXSW_SP_RIF_COUNTER_EGRESS);
  252. else
  253. mlxsw_sp_rif_counter_free(mlxsw_sp,
  254. mlxsw_sp->rifs[i],
  255. MLXSW_SP_RIF_COUNTER_EGRESS);
  256. }
  257. rtnl_unlock();
  258. return 0;
  259. }
  260. static struct devlink_dpipe_table_ops mlxsw_sp_erif_ops = {
  261. .matches_dump = mlxsw_sp_dpipe_table_erif_matches_dump,
  262. .actions_dump = mlxsw_sp_dpipe_table_erif_actions_dump,
  263. .entries_dump = mlxsw_sp_table_erif_entries_dump,
  264. .counters_set_update = mlxsw_sp_table_erif_counters_update,
  265. };
  266. static int mlxsw_sp_dpipe_erif_table_init(struct mlxsw_sp *mlxsw_sp)
  267. {
  268. struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
  269. u64 table_size;
  270. table_size = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
  271. return devlink_dpipe_table_register(devlink,
  272. MLXSW_SP_DPIPE_TABLE_NAME_ERIF,
  273. &mlxsw_sp_erif_ops,
  274. mlxsw_sp, table_size,
  275. false);
  276. }
  277. static void mlxsw_sp_dpipe_erif_table_fini(struct mlxsw_sp *mlxsw_sp)
  278. {
  279. struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
  280. devlink_dpipe_table_unregister(devlink, MLXSW_SP_DPIPE_TABLE_NAME_ERIF);
  281. }
  282. int mlxsw_sp_dpipe_init(struct mlxsw_sp *mlxsw_sp)
  283. {
  284. struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
  285. int err;
  286. err = devlink_dpipe_headers_register(devlink,
  287. &mlxsw_sp_dpipe_headers);
  288. if (err)
  289. return err;
  290. err = mlxsw_sp_dpipe_erif_table_init(mlxsw_sp);
  291. if (err)
  292. goto err_erif_register;
  293. return 0;
  294. err_erif_register:
  295. devlink_dpipe_headers_unregister(priv_to_devlink(mlxsw_sp->core));
  296. return err;
  297. }
  298. void mlxsw_sp_dpipe_fini(struct mlxsw_sp *mlxsw_sp)
  299. {
  300. struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
  301. mlxsw_sp_dpipe_erif_table_fini(mlxsw_sp);
  302. devlink_dpipe_headers_unregister(devlink);
  303. }