sysfs_net_ipv4.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * net/ipv4/sysfs_net_ipv4.c
  3. *
  4. * sysfs-based networking knobs (so we can, unlike with sysctl, control perms)
  5. *
  6. * Copyright (C) 2008 Google, Inc.
  7. *
  8. * Robert Love <rlove@google.com>
  9. *
  10. * This software is licensed under the terms of the GNU General Public
  11. * License version 2, as published by the Free Software Foundation, and
  12. * may be copied, distributed, and modified under those terms.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/kobject.h>
  20. #include <linux/string.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/init.h>
  23. #include <net/tcp.h>
  24. #define CREATE_IPV4_FILE(_name, _var) \
  25. static ssize_t _name##_show(struct kobject *kobj, \
  26. struct kobj_attribute *attr, char *buf) \
  27. { \
  28. return sprintf(buf, "%d\n", _var); \
  29. } \
  30. static ssize_t _name##_store(struct kobject *kobj, \
  31. struct kobj_attribute *attr, \
  32. const char *buf, size_t count) \
  33. { \
  34. int val, ret; \
  35. ret = sscanf(buf, "%d", &val); \
  36. if (ret != 1) \
  37. return -EINVAL; \
  38. if (val < 0) \
  39. return -EINVAL; \
  40. _var = val; \
  41. return count; \
  42. } \
  43. static struct kobj_attribute _name##_attr = \
  44. __ATTR(_name, 0644, _name##_show, _name##_store)
  45. CREATE_IPV4_FILE(tcp_wmem_min, sysctl_tcp_wmem[0]);
  46. CREATE_IPV4_FILE(tcp_wmem_def, sysctl_tcp_wmem[1]);
  47. CREATE_IPV4_FILE(tcp_wmem_max, sysctl_tcp_wmem[2]);
  48. CREATE_IPV4_FILE(tcp_rmem_min, sysctl_tcp_rmem[0]);
  49. CREATE_IPV4_FILE(tcp_rmem_def, sysctl_tcp_rmem[1]);
  50. CREATE_IPV4_FILE(tcp_rmem_max, sysctl_tcp_rmem[2]);
  51. static struct attribute *ipv4_attrs[] = {
  52. &tcp_wmem_min_attr.attr,
  53. &tcp_wmem_def_attr.attr,
  54. &tcp_wmem_max_attr.attr,
  55. &tcp_rmem_min_attr.attr,
  56. &tcp_rmem_def_attr.attr,
  57. &tcp_rmem_max_attr.attr,
  58. NULL
  59. };
  60. static struct attribute_group ipv4_attr_group = {
  61. .attrs = ipv4_attrs,
  62. };
  63. static __init int sysfs_ipv4_init(void)
  64. {
  65. struct kobject *ipv4_kobject;
  66. int ret;
  67. ipv4_kobject = kobject_create_and_add("ipv4", kernel_kobj);
  68. if (!ipv4_kobject)
  69. return -ENOMEM;
  70. ret = sysfs_create_group(ipv4_kobject, &ipv4_attr_group);
  71. if (ret) {
  72. kobject_put(ipv4_kobject);
  73. return ret;
  74. }
  75. return 0;
  76. }
  77. subsys_initcall(sysfs_ipv4_init);