bat_sysfs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  2. * Copyright (C) 2010-2011 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "bat_sysfs.h"
  23. #include "translation-table.h"
  24. #include "originator.h"
  25. #include "hard-interface.h"
  26. #include "gateway_common.h"
  27. #include "gateway_client.h"
  28. #include "vis.h"
  29. #define to_dev(obj) container_of(obj, struct device, kobj)
  30. #define kobj_to_netdev(obj) to_net_dev(to_dev(obj->parent))
  31. #define kobj_to_batpriv(obj) netdev_priv(kobj_to_netdev(obj))
  32. /* Use this, if you have customized show and store functions */
  33. #define BAT_ATTR(_name, _mode, _show, _store) \
  34. struct bat_attribute bat_attr_##_name = { \
  35. .attr = {.name = __stringify(_name), \
  36. .mode = _mode }, \
  37. .show = _show, \
  38. .store = _store, \
  39. };
  40. #define BAT_ATTR_STORE_BOOL(_name, _post_func) \
  41. ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \
  42. char *buff, size_t count) \
  43. { \
  44. struct net_device *net_dev = kobj_to_netdev(kobj); \
  45. struct bat_priv *bat_priv = netdev_priv(net_dev); \
  46. return __store_bool_attr(buff, count, _post_func, attr, \
  47. &bat_priv->_name, net_dev); \
  48. }
  49. #define BAT_ATTR_SHOW_BOOL(_name) \
  50. ssize_t show_##_name(struct kobject *kobj, struct attribute *attr, \
  51. char *buff) \
  52. { \
  53. struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \
  54. return sprintf(buff, "%s\n", \
  55. atomic_read(&bat_priv->_name) == 0 ? \
  56. "disabled" : "enabled"); \
  57. } \
  58. /* Use this, if you are going to turn a [name] in bat_priv on or off */
  59. #define BAT_ATTR_BOOL(_name, _mode, _post_func) \
  60. static BAT_ATTR_STORE_BOOL(_name, _post_func) \
  61. static BAT_ATTR_SHOW_BOOL(_name) \
  62. static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
  63. #define BAT_ATTR_STORE_UINT(_name, _min, _max, _post_func) \
  64. ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \
  65. char *buff, size_t count) \
  66. { \
  67. struct net_device *net_dev = kobj_to_netdev(kobj); \
  68. struct bat_priv *bat_priv = netdev_priv(net_dev); \
  69. return __store_uint_attr(buff, count, _min, _max, _post_func, \
  70. attr, &bat_priv->_name, net_dev); \
  71. }
  72. #define BAT_ATTR_SHOW_UINT(_name) \
  73. ssize_t show_##_name(struct kobject *kobj, struct attribute *attr, \
  74. char *buff) \
  75. { \
  76. struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \
  77. return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \
  78. } \
  79. /* Use this, if you are going to set [name] in bat_priv to unsigned integer
  80. * values only */
  81. #define BAT_ATTR_UINT(_name, _mode, _min, _max, _post_func) \
  82. static BAT_ATTR_STORE_UINT(_name, _min, _max, _post_func) \
  83. static BAT_ATTR_SHOW_UINT(_name) \
  84. static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
  85. static int store_bool_attr(char *buff, size_t count,
  86. struct net_device *net_dev,
  87. char *attr_name, atomic_t *attr)
  88. {
  89. int enabled = -1;
  90. if (buff[count - 1] == '\n')
  91. buff[count - 1] = '\0';
  92. if ((strncmp(buff, "1", 2) == 0) ||
  93. (strncmp(buff, "enable", 7) == 0) ||
  94. (strncmp(buff, "enabled", 8) == 0))
  95. enabled = 1;
  96. if ((strncmp(buff, "0", 2) == 0) ||
  97. (strncmp(buff, "disable", 8) == 0) ||
  98. (strncmp(buff, "disabled", 9) == 0))
  99. enabled = 0;
  100. if (enabled < 0) {
  101. bat_info(net_dev,
  102. "%s: Invalid parameter received: %s\n",
  103. attr_name, buff);
  104. return -EINVAL;
  105. }
  106. if (atomic_read(attr) == enabled)
  107. return count;
  108. bat_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
  109. atomic_read(attr) == 1 ? "enabled" : "disabled",
  110. enabled == 1 ? "enabled" : "disabled");
  111. atomic_set(attr, (unsigned)enabled);
  112. return count;
  113. }
  114. static inline ssize_t __store_bool_attr(char *buff, size_t count,
  115. void (*post_func)(struct net_device *),
  116. struct attribute *attr,
  117. atomic_t *attr_store, struct net_device *net_dev)
  118. {
  119. int ret;
  120. ret = store_bool_attr(buff, count, net_dev, (char *)attr->name,
  121. attr_store);
  122. if (post_func && ret)
  123. post_func(net_dev);
  124. return ret;
  125. }
  126. static int store_uint_attr(char *buff, size_t count,
  127. struct net_device *net_dev, char *attr_name,
  128. unsigned int min, unsigned int max, atomic_t *attr)
  129. {
  130. unsigned long uint_val;
  131. int ret;
  132. ret = strict_strtoul(buff, 10, &uint_val);
  133. if (ret) {
  134. bat_info(net_dev,
  135. "%s: Invalid parameter received: %s\n",
  136. attr_name, buff);
  137. return -EINVAL;
  138. }
  139. if (uint_val < min) {
  140. bat_info(net_dev, "%s: Value is too small: %lu min: %u\n",
  141. attr_name, uint_val, min);
  142. return -EINVAL;
  143. }
  144. if (uint_val > max) {
  145. bat_info(net_dev, "%s: Value is too big: %lu max: %u\n",
  146. attr_name, uint_val, max);
  147. return -EINVAL;
  148. }
  149. if (atomic_read(attr) == uint_val)
  150. return count;
  151. bat_info(net_dev, "%s: Changing from: %i to: %lu\n",
  152. attr_name, atomic_read(attr), uint_val);
  153. atomic_set(attr, uint_val);
  154. return count;
  155. }
  156. static inline ssize_t __store_uint_attr(char *buff, size_t count,
  157. int min, int max,
  158. void (*post_func)(struct net_device *),
  159. struct attribute *attr,
  160. atomic_t *attr_store, struct net_device *net_dev)
  161. {
  162. int ret;
  163. ret = store_uint_attr(buff, count, net_dev, (char *)attr->name,
  164. min, max, attr_store);
  165. if (post_func && ret)
  166. post_func(net_dev);
  167. return ret;
  168. }
  169. static ssize_t show_vis_mode(struct kobject *kobj, struct attribute *attr,
  170. char *buff)
  171. {
  172. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  173. int vis_mode = atomic_read(&bat_priv->vis_mode);
  174. return sprintf(buff, "%s\n",
  175. vis_mode == VIS_TYPE_CLIENT_UPDATE ?
  176. "client" : "server");
  177. }
  178. static ssize_t store_vis_mode(struct kobject *kobj, struct attribute *attr,
  179. char *buff, size_t count)
  180. {
  181. struct net_device *net_dev = kobj_to_netdev(kobj);
  182. struct bat_priv *bat_priv = netdev_priv(net_dev);
  183. unsigned long val;
  184. int ret, vis_mode_tmp = -1;
  185. ret = strict_strtoul(buff, 10, &val);
  186. if (((count == 2) && (!ret) && (val == VIS_TYPE_CLIENT_UPDATE)) ||
  187. (strncmp(buff, "client", 6) == 0) ||
  188. (strncmp(buff, "off", 3) == 0))
  189. vis_mode_tmp = VIS_TYPE_CLIENT_UPDATE;
  190. if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) ||
  191. (strncmp(buff, "server", 6) == 0))
  192. vis_mode_tmp = VIS_TYPE_SERVER_SYNC;
  193. if (vis_mode_tmp < 0) {
  194. if (buff[count - 1] == '\n')
  195. buff[count - 1] = '\0';
  196. bat_info(net_dev,
  197. "Invalid parameter for 'vis mode' setting received: "
  198. "%s\n", buff);
  199. return -EINVAL;
  200. }
  201. if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
  202. return count;
  203. bat_info(net_dev, "Changing vis mode from: %s to: %s\n",
  204. atomic_read(&bat_priv->vis_mode) == VIS_TYPE_CLIENT_UPDATE ?
  205. "client" : "server", vis_mode_tmp == VIS_TYPE_CLIENT_UPDATE ?
  206. "client" : "server");
  207. atomic_set(&bat_priv->vis_mode, (unsigned)vis_mode_tmp);
  208. return count;
  209. }
  210. static void post_gw_deselect(struct net_device *net_dev)
  211. {
  212. struct bat_priv *bat_priv = netdev_priv(net_dev);
  213. gw_deselect(bat_priv);
  214. }
  215. static ssize_t show_gw_mode(struct kobject *kobj, struct attribute *attr,
  216. char *buff)
  217. {
  218. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  219. int bytes_written;
  220. switch (atomic_read(&bat_priv->gw_mode)) {
  221. case GW_MODE_CLIENT:
  222. bytes_written = sprintf(buff, "%s\n", GW_MODE_CLIENT_NAME);
  223. break;
  224. case GW_MODE_SERVER:
  225. bytes_written = sprintf(buff, "%s\n", GW_MODE_SERVER_NAME);
  226. break;
  227. default:
  228. bytes_written = sprintf(buff, "%s\n", GW_MODE_OFF_NAME);
  229. break;
  230. }
  231. return bytes_written;
  232. }
  233. static ssize_t store_gw_mode(struct kobject *kobj, struct attribute *attr,
  234. char *buff, size_t count)
  235. {
  236. struct net_device *net_dev = kobj_to_netdev(kobj);
  237. struct bat_priv *bat_priv = netdev_priv(net_dev);
  238. char *curr_gw_mode_str;
  239. int gw_mode_tmp = -1;
  240. if (buff[count - 1] == '\n')
  241. buff[count - 1] = '\0';
  242. if (strncmp(buff, GW_MODE_OFF_NAME, strlen(GW_MODE_OFF_NAME)) == 0)
  243. gw_mode_tmp = GW_MODE_OFF;
  244. if (strncmp(buff, GW_MODE_CLIENT_NAME,
  245. strlen(GW_MODE_CLIENT_NAME)) == 0)
  246. gw_mode_tmp = GW_MODE_CLIENT;
  247. if (strncmp(buff, GW_MODE_SERVER_NAME,
  248. strlen(GW_MODE_SERVER_NAME)) == 0)
  249. gw_mode_tmp = GW_MODE_SERVER;
  250. if (gw_mode_tmp < 0) {
  251. bat_info(net_dev,
  252. "Invalid parameter for 'gw mode' setting received: "
  253. "%s\n", buff);
  254. return -EINVAL;
  255. }
  256. if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
  257. return count;
  258. switch (atomic_read(&bat_priv->gw_mode)) {
  259. case GW_MODE_CLIENT:
  260. curr_gw_mode_str = GW_MODE_CLIENT_NAME;
  261. break;
  262. case GW_MODE_SERVER:
  263. curr_gw_mode_str = GW_MODE_SERVER_NAME;
  264. break;
  265. default:
  266. curr_gw_mode_str = GW_MODE_OFF_NAME;
  267. break;
  268. }
  269. bat_info(net_dev, "Changing gw mode from: %s to: %s\n",
  270. curr_gw_mode_str, buff);
  271. gw_deselect(bat_priv);
  272. atomic_set(&bat_priv->gw_mode, (unsigned)gw_mode_tmp);
  273. return count;
  274. }
  275. static ssize_t show_gw_bwidth(struct kobject *kobj, struct attribute *attr,
  276. char *buff)
  277. {
  278. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  279. int down, up;
  280. int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
  281. gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
  282. return sprintf(buff, "%i%s/%i%s\n",
  283. (down > 2048 ? down / 1024 : down),
  284. (down > 2048 ? "MBit" : "KBit"),
  285. (up > 2048 ? up / 1024 : up),
  286. (up > 2048 ? "MBit" : "KBit"));
  287. }
  288. static ssize_t store_gw_bwidth(struct kobject *kobj, struct attribute *attr,
  289. char *buff, size_t count)
  290. {
  291. struct net_device *net_dev = kobj_to_netdev(kobj);
  292. if (buff[count - 1] == '\n')
  293. buff[count - 1] = '\0';
  294. return gw_bandwidth_set(net_dev, buff, count);
  295. }
  296. BAT_ATTR_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
  297. BAT_ATTR_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
  298. BAT_ATTR_BOOL(fragmentation, S_IRUGO | S_IWUSR, update_min_mtu);
  299. static BAT_ATTR(vis_mode, S_IRUGO | S_IWUSR, show_vis_mode, store_vis_mode);
  300. static BAT_ATTR(gw_mode, S_IRUGO | S_IWUSR, show_gw_mode, store_gw_mode);
  301. BAT_ATTR_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * JITTER, INT_MAX, NULL);
  302. BAT_ATTR_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL);
  303. BAT_ATTR_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, TQ_MAX_VALUE,
  304. post_gw_deselect);
  305. static BAT_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, show_gw_bwidth,
  306. store_gw_bwidth);
  307. #ifdef CONFIG_BATMAN_ADV_DEBUG
  308. BAT_ATTR_UINT(log_level, S_IRUGO | S_IWUSR, 0, 3, NULL);
  309. #endif
  310. static struct bat_attribute *mesh_attrs[] = {
  311. &bat_attr_aggregated_ogms,
  312. &bat_attr_bonding,
  313. &bat_attr_fragmentation,
  314. &bat_attr_vis_mode,
  315. &bat_attr_gw_mode,
  316. &bat_attr_orig_interval,
  317. &bat_attr_hop_penalty,
  318. &bat_attr_gw_sel_class,
  319. &bat_attr_gw_bandwidth,
  320. #ifdef CONFIG_BATMAN_ADV_DEBUG
  321. &bat_attr_log_level,
  322. #endif
  323. NULL,
  324. };
  325. int sysfs_add_meshif(struct net_device *dev)
  326. {
  327. struct kobject *batif_kobject = &dev->dev.kobj;
  328. struct bat_priv *bat_priv = netdev_priv(dev);
  329. struct bat_attribute **bat_attr;
  330. int err;
  331. bat_priv->mesh_obj = kobject_create_and_add(SYSFS_IF_MESH_SUBDIR,
  332. batif_kobject);
  333. if (!bat_priv->mesh_obj) {
  334. bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  335. SYSFS_IF_MESH_SUBDIR);
  336. goto out;
  337. }
  338. for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr) {
  339. err = sysfs_create_file(bat_priv->mesh_obj,
  340. &((*bat_attr)->attr));
  341. if (err) {
  342. bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  343. dev->name, SYSFS_IF_MESH_SUBDIR,
  344. ((*bat_attr)->attr).name);
  345. goto rem_attr;
  346. }
  347. }
  348. return 0;
  349. rem_attr:
  350. for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
  351. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  352. kobject_put(bat_priv->mesh_obj);
  353. bat_priv->mesh_obj = NULL;
  354. out:
  355. return -ENOMEM;
  356. }
  357. void sysfs_del_meshif(struct net_device *dev)
  358. {
  359. struct bat_priv *bat_priv = netdev_priv(dev);
  360. struct bat_attribute **bat_attr;
  361. for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
  362. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  363. kobject_put(bat_priv->mesh_obj);
  364. bat_priv->mesh_obj = NULL;
  365. }
  366. static ssize_t show_mesh_iface(struct kobject *kobj, struct attribute *attr,
  367. char *buff)
  368. {
  369. struct net_device *net_dev = kobj_to_netdev(kobj);
  370. struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
  371. ssize_t length;
  372. if (!hard_iface)
  373. return 0;
  374. length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ?
  375. "none" : hard_iface->soft_iface->name);
  376. hardif_free_ref(hard_iface);
  377. return length;
  378. }
  379. static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr,
  380. char *buff, size_t count)
  381. {
  382. struct net_device *net_dev = kobj_to_netdev(kobj);
  383. struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
  384. int status_tmp = -1;
  385. int ret = count;
  386. if (!hard_iface)
  387. return count;
  388. if (buff[count - 1] == '\n')
  389. buff[count - 1] = '\0';
  390. if (strlen(buff) >= IFNAMSIZ) {
  391. pr_err("Invalid parameter for 'mesh_iface' setting received: "
  392. "interface name too long '%s'\n", buff);
  393. hardif_free_ref(hard_iface);
  394. return -EINVAL;
  395. }
  396. if (strncmp(buff, "none", 4) == 0)
  397. status_tmp = IF_NOT_IN_USE;
  398. else
  399. status_tmp = IF_I_WANT_YOU;
  400. if (hard_iface->if_status == status_tmp)
  401. goto out;
  402. if ((hard_iface->soft_iface) &&
  403. (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
  404. goto out;
  405. if (status_tmp == IF_NOT_IN_USE) {
  406. rtnl_lock();
  407. hardif_disable_interface(hard_iface);
  408. rtnl_unlock();
  409. goto out;
  410. }
  411. /* if the interface already is in use */
  412. if (hard_iface->if_status != IF_NOT_IN_USE) {
  413. rtnl_lock();
  414. hardif_disable_interface(hard_iface);
  415. rtnl_unlock();
  416. }
  417. ret = hardif_enable_interface(hard_iface, buff);
  418. out:
  419. hardif_free_ref(hard_iface);
  420. return ret;
  421. }
  422. static ssize_t show_iface_status(struct kobject *kobj, struct attribute *attr,
  423. char *buff)
  424. {
  425. struct net_device *net_dev = kobj_to_netdev(kobj);
  426. struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
  427. ssize_t length;
  428. if (!hard_iface)
  429. return 0;
  430. switch (hard_iface->if_status) {
  431. case IF_TO_BE_REMOVED:
  432. length = sprintf(buff, "disabling\n");
  433. break;
  434. case IF_INACTIVE:
  435. length = sprintf(buff, "inactive\n");
  436. break;
  437. case IF_ACTIVE:
  438. length = sprintf(buff, "active\n");
  439. break;
  440. case IF_TO_BE_ACTIVATED:
  441. length = sprintf(buff, "enabling\n");
  442. break;
  443. case IF_NOT_IN_USE:
  444. default:
  445. length = sprintf(buff, "not in use\n");
  446. break;
  447. }
  448. hardif_free_ref(hard_iface);
  449. return length;
  450. }
  451. static BAT_ATTR(mesh_iface, S_IRUGO | S_IWUSR,
  452. show_mesh_iface, store_mesh_iface);
  453. static BAT_ATTR(iface_status, S_IRUGO, show_iface_status, NULL);
  454. static struct bat_attribute *batman_attrs[] = {
  455. &bat_attr_mesh_iface,
  456. &bat_attr_iface_status,
  457. NULL,
  458. };
  459. int sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
  460. {
  461. struct kobject *hardif_kobject = &dev->dev.kobj;
  462. struct bat_attribute **bat_attr;
  463. int err;
  464. *hardif_obj = kobject_create_and_add(SYSFS_IF_BAT_SUBDIR,
  465. hardif_kobject);
  466. if (!*hardif_obj) {
  467. bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  468. SYSFS_IF_BAT_SUBDIR);
  469. goto out;
  470. }
  471. for (bat_attr = batman_attrs; *bat_attr; ++bat_attr) {
  472. err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
  473. if (err) {
  474. bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  475. dev->name, SYSFS_IF_BAT_SUBDIR,
  476. ((*bat_attr)->attr).name);
  477. goto rem_attr;
  478. }
  479. }
  480. return 0;
  481. rem_attr:
  482. for (bat_attr = batman_attrs; *bat_attr; ++bat_attr)
  483. sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
  484. out:
  485. return -ENOMEM;
  486. }
  487. void sysfs_del_hardif(struct kobject **hardif_obj)
  488. {
  489. kobject_put(*hardif_obj);
  490. *hardif_obj = NULL;
  491. }