vlanproc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /******************************************************************************
  2. * vlanproc.c VLAN Module. /proc filesystem interface.
  3. *
  4. * This module is completely hardware-independent and provides
  5. * access to the router using Linux /proc filesystem.
  6. *
  7. * Author: Ben Greear, <greearb@candelatech.com> coppied from wanproc.c
  8. * by: Gene Kozin <genek@compuserve.com>
  9. *
  10. * Copyright: (c) 1998 Ben Greear
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. * ============================================================================
  17. * Jan 20, 1998 Ben Greear Initial Version
  18. *****************************************************************************/
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h>
  21. #include <linux/errno.h>
  22. #include <linux/kernel.h>
  23. #include <linux/string.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/fs.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/if_vlan.h>
  29. #include <net/net_namespace.h>
  30. #include <net/netns/generic.h>
  31. #include "vlanproc.h"
  32. #include "vlan.h"
  33. /****** Function Prototypes *************************************************/
  34. /* Methods for preparing data for reading proc entries */
  35. static int vlan_seq_show(struct seq_file *seq, void *v);
  36. static void *vlan_seq_start(struct seq_file *seq, loff_t *pos);
  37. static void *vlan_seq_next(struct seq_file *seq, void *v, loff_t *pos);
  38. static void vlan_seq_stop(struct seq_file *seq, void *);
  39. static int vlandev_seq_show(struct seq_file *seq, void *v);
  40. /*
  41. * Global Data
  42. */
  43. /*
  44. * Names of the proc directory entries
  45. */
  46. static const char name_root[] = "vlan";
  47. static const char name_conf[] = "config";
  48. /*
  49. * Structures for interfacing with the /proc filesystem.
  50. * VLAN creates its own directory /proc/net/vlan with the following
  51. * entries:
  52. * config device status/configuration
  53. * <device> entry for each device
  54. */
  55. /*
  56. * Generic /proc/net/vlan/<file> file and inode operations
  57. */
  58. static const struct seq_operations vlan_seq_ops = {
  59. .start = vlan_seq_start,
  60. .next = vlan_seq_next,
  61. .stop = vlan_seq_stop,
  62. .show = vlan_seq_show,
  63. };
  64. /*
  65. * Proc filesystem directory entries.
  66. */
  67. /* Strings */
  68. static const char *const vlan_name_type_str[VLAN_NAME_TYPE_HIGHEST] = {
  69. [VLAN_NAME_TYPE_RAW_PLUS_VID] = "VLAN_NAME_TYPE_RAW_PLUS_VID",
  70. [VLAN_NAME_TYPE_PLUS_VID_NO_PAD] = "VLAN_NAME_TYPE_PLUS_VID_NO_PAD",
  71. [VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD] = "VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD",
  72. [VLAN_NAME_TYPE_PLUS_VID] = "VLAN_NAME_TYPE_PLUS_VID",
  73. };
  74. /*
  75. * Interface functions
  76. */
  77. /*
  78. * Clean up /proc/net/vlan entries
  79. */
  80. void vlan_proc_cleanup(struct net *net)
  81. {
  82. struct vlan_net *vn = net_generic(net, vlan_net_id);
  83. if (vn->proc_vlan_conf)
  84. remove_proc_entry(name_conf, vn->proc_vlan_dir);
  85. if (vn->proc_vlan_dir)
  86. remove_proc_entry(name_root, net->proc_net);
  87. /* Dynamically added entries should be cleaned up as their vlan_device
  88. * is removed, so we should not have to take care of it here...
  89. */
  90. }
  91. /*
  92. * Create /proc/net/vlan entries
  93. */
  94. int __net_init vlan_proc_init(struct net *net)
  95. {
  96. struct vlan_net *vn = net_generic(net, vlan_net_id);
  97. vn->proc_vlan_dir = proc_net_mkdir(net, name_root, net->proc_net);
  98. if (!vn->proc_vlan_dir)
  99. goto err;
  100. vn->proc_vlan_conf = proc_create_net(name_conf, S_IFREG | 0600,
  101. vn->proc_vlan_dir, &vlan_seq_ops,
  102. sizeof(struct seq_net_private));
  103. if (!vn->proc_vlan_conf)
  104. goto err;
  105. return 0;
  106. err:
  107. pr_err("can't create entry in proc filesystem!\n");
  108. vlan_proc_cleanup(net);
  109. return -ENOBUFS;
  110. }
  111. /*
  112. * Add directory entry for VLAN device.
  113. */
  114. int vlan_proc_add_dev(struct net_device *vlandev)
  115. {
  116. struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev);
  117. struct vlan_net *vn = net_generic(dev_net(vlandev), vlan_net_id);
  118. if (!strcmp(vlandev->name, name_conf))
  119. return -EINVAL;
  120. vlan->dent = proc_create_single_data(vlandev->name, S_IFREG | 0600,
  121. vn->proc_vlan_dir, vlandev_seq_show, vlandev);
  122. if (!vlan->dent)
  123. return -ENOBUFS;
  124. return 0;
  125. }
  126. /*
  127. * Delete directory entry for VLAN device.
  128. */
  129. void vlan_proc_rem_dev(struct net_device *vlandev)
  130. {
  131. /** NOTE: This will consume the memory pointed to by dent, it seems. */
  132. proc_remove(vlan_dev_priv(vlandev)->dent);
  133. vlan_dev_priv(vlandev)->dent = NULL;
  134. }
  135. /****** Proc filesystem entry points ****************************************/
  136. /*
  137. * The following few functions build the content of /proc/net/vlan/config
  138. */
  139. /* start read of /proc/net/vlan/config */
  140. static void *vlan_seq_start(struct seq_file *seq, loff_t *pos)
  141. __acquires(rcu)
  142. {
  143. struct net_device *dev;
  144. struct net *net = seq_file_net(seq);
  145. loff_t i = 1;
  146. rcu_read_lock();
  147. if (*pos == 0)
  148. return SEQ_START_TOKEN;
  149. for_each_netdev_rcu(net, dev) {
  150. if (!is_vlan_dev(dev))
  151. continue;
  152. if (i++ == *pos)
  153. return dev;
  154. }
  155. return NULL;
  156. }
  157. static void *vlan_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  158. {
  159. struct net_device *dev;
  160. struct net *net = seq_file_net(seq);
  161. ++*pos;
  162. dev = v;
  163. if (v == SEQ_START_TOKEN)
  164. dev = net_device_entry(&net->dev_base_head);
  165. for_each_netdev_continue_rcu(net, dev) {
  166. if (!is_vlan_dev(dev))
  167. continue;
  168. return dev;
  169. }
  170. return NULL;
  171. }
  172. static void vlan_seq_stop(struct seq_file *seq, void *v)
  173. __releases(rcu)
  174. {
  175. rcu_read_unlock();
  176. }
  177. static int vlan_seq_show(struct seq_file *seq, void *v)
  178. {
  179. struct net *net = seq_file_net(seq);
  180. struct vlan_net *vn = net_generic(net, vlan_net_id);
  181. if (v == SEQ_START_TOKEN) {
  182. const char *nmtype = NULL;
  183. seq_puts(seq, "VLAN Dev name | VLAN ID\n");
  184. if (vn->name_type < ARRAY_SIZE(vlan_name_type_str))
  185. nmtype = vlan_name_type_str[vn->name_type];
  186. seq_printf(seq, "Name-Type: %s\n",
  187. nmtype ? nmtype : "UNKNOWN");
  188. } else {
  189. const struct net_device *vlandev = v;
  190. const struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev);
  191. seq_printf(seq, "%-15s| %d | %s\n", vlandev->name,
  192. vlan->vlan_id, vlan->real_dev->name);
  193. }
  194. return 0;
  195. }
  196. static int vlandev_seq_show(struct seq_file *seq, void *offset)
  197. {
  198. struct net_device *vlandev = (struct net_device *) seq->private;
  199. const struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev);
  200. struct rtnl_link_stats64 temp;
  201. const struct rtnl_link_stats64 *stats;
  202. static const char fmt64[] = "%30s %12llu\n";
  203. int i;
  204. if (!is_vlan_dev(vlandev))
  205. return 0;
  206. stats = dev_get_stats(vlandev, &temp);
  207. seq_printf(seq,
  208. "%s VID: %d REORDER_HDR: %i dev->priv_flags: %hx\n",
  209. vlandev->name, vlan->vlan_id,
  210. (int)(vlan->flags & 1), vlandev->priv_flags);
  211. seq_printf(seq, fmt64, "total frames received", stats->rx_packets);
  212. seq_printf(seq, fmt64, "total bytes received", stats->rx_bytes);
  213. seq_printf(seq, fmt64, "Broadcast/Multicast Rcvd", stats->multicast);
  214. seq_puts(seq, "\n");
  215. seq_printf(seq, fmt64, "total frames transmitted", stats->tx_packets);
  216. seq_printf(seq, fmt64, "total bytes transmitted", stats->tx_bytes);
  217. seq_printf(seq, "Device: %s", vlan->real_dev->name);
  218. /* now show all PRIORITY mappings relating to this VLAN */
  219. seq_printf(seq, "\nINGRESS priority mappings: "
  220. "0:%u 1:%u 2:%u 3:%u 4:%u 5:%u 6:%u 7:%u\n",
  221. vlan->ingress_priority_map[0],
  222. vlan->ingress_priority_map[1],
  223. vlan->ingress_priority_map[2],
  224. vlan->ingress_priority_map[3],
  225. vlan->ingress_priority_map[4],
  226. vlan->ingress_priority_map[5],
  227. vlan->ingress_priority_map[6],
  228. vlan->ingress_priority_map[7]);
  229. seq_printf(seq, " EGRESS priority mappings: ");
  230. for (i = 0; i < 16; i++) {
  231. const struct vlan_priority_tci_mapping *mp
  232. = vlan->egress_priority_map[i];
  233. while (mp) {
  234. seq_printf(seq, "%u:%hu ",
  235. mp->priority, ((mp->vlan_qos >> 13) & 0x7));
  236. mp = mp->next;
  237. }
  238. }
  239. seq_puts(seq, "\n");
  240. return 0;
  241. }