vlanproc.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #include <linux/module.h>
  20. #include <linux/stddef.h> /* offsetof(), etc. */
  21. #include <linux/errno.h> /* return codes */
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h> /* kmalloc(), kfree() */
  24. #include <linux/mm.h>
  25. #include <linux/string.h> /* inline mem*, str* functions */
  26. #include <linux/init.h> /* __initfunc et al. */
  27. #include <asm/byteorder.h> /* htons(), etc. */
  28. #include <asm/uaccess.h> /* copy_to_user */
  29. #include <asm/io.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/fs.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/if_vlan.h>
  35. #include "vlanproc.h"
  36. #include "vlan.h"
  37. /****** Function Prototypes *************************************************/
  38. /* Methods for preparing data for reading proc entries */
  39. static int vlan_seq_show(struct seq_file *seq, void *v);
  40. static void *vlan_seq_start(struct seq_file *seq, loff_t *pos);
  41. static void *vlan_seq_next(struct seq_file *seq, void *v, loff_t *pos);
  42. static void vlan_seq_stop(struct seq_file *seq, void *);
  43. static int vlandev_seq_show(struct seq_file *seq, void *v);
  44. /*
  45. * Global Data
  46. */
  47. /*
  48. * Names of the proc directory entries
  49. */
  50. static const char name_root[] = "vlan";
  51. static const char name_conf[] = "config";
  52. /*
  53. * Structures for interfacing with the /proc filesystem.
  54. * VLAN creates its own directory /proc/net/vlan with the folowing
  55. * entries:
  56. * config device status/configuration
  57. * <device> entry for each device
  58. */
  59. /*
  60. * Generic /proc/net/vlan/<file> file and inode operations
  61. */
  62. static struct seq_operations vlan_seq_ops = {
  63. .start = vlan_seq_start,
  64. .next = vlan_seq_next,
  65. .stop = vlan_seq_stop,
  66. .show = vlan_seq_show,
  67. };
  68. static int vlan_seq_open(struct inode *inode, struct file *file)
  69. {
  70. return seq_open(file, &vlan_seq_ops);
  71. }
  72. static const struct file_operations vlan_fops = {
  73. .owner = THIS_MODULE,
  74. .open = vlan_seq_open,
  75. .read = seq_read,
  76. .llseek = seq_lseek,
  77. .release = seq_release,
  78. };
  79. /*
  80. * /proc/net/vlan/<device> file and inode operations
  81. */
  82. static int vlandev_seq_open(struct inode *inode, struct file *file)
  83. {
  84. return single_open(file, vlandev_seq_show, PDE(inode)->data);
  85. }
  86. static const struct file_operations vlandev_fops = {
  87. .owner = THIS_MODULE,
  88. .open = vlandev_seq_open,
  89. .read = seq_read,
  90. .llseek = seq_lseek,
  91. .release = single_release,
  92. };
  93. /*
  94. * Proc filesystem derectory entries.
  95. */
  96. /*
  97. * /proc/net/vlan
  98. */
  99. static struct proc_dir_entry *proc_vlan_dir;
  100. /*
  101. * /proc/net/vlan/config
  102. */
  103. static struct proc_dir_entry *proc_vlan_conf;
  104. /* Strings */
  105. static const char *vlan_name_type_str[VLAN_NAME_TYPE_HIGHEST] = {
  106. [VLAN_NAME_TYPE_RAW_PLUS_VID] = "VLAN_NAME_TYPE_RAW_PLUS_VID",
  107. [VLAN_NAME_TYPE_PLUS_VID_NO_PAD] = "VLAN_NAME_TYPE_PLUS_VID_NO_PAD",
  108. [VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD]= "VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD",
  109. [VLAN_NAME_TYPE_PLUS_VID] = "VLAN_NAME_TYPE_PLUS_VID",
  110. };
  111. /*
  112. * Interface functions
  113. */
  114. /*
  115. * Clean up /proc/net/vlan entries
  116. */
  117. void vlan_proc_cleanup(void)
  118. {
  119. if (proc_vlan_conf)
  120. remove_proc_entry(name_conf, proc_vlan_dir);
  121. if (proc_vlan_dir)
  122. proc_net_remove(name_root);
  123. /* Dynamically added entries should be cleaned up as their vlan_device
  124. * is removed, so we should not have to take care of it here...
  125. */
  126. }
  127. /*
  128. * Create /proc/net/vlan entries
  129. */
  130. int __init vlan_proc_init(void)
  131. {
  132. proc_vlan_dir = proc_mkdir(name_root, proc_net);
  133. if (proc_vlan_dir) {
  134. proc_vlan_conf = create_proc_entry(name_conf,
  135. S_IFREG|S_IRUSR|S_IWUSR,
  136. proc_vlan_dir);
  137. if (proc_vlan_conf) {
  138. proc_vlan_conf->proc_fops = &vlan_fops;
  139. return 0;
  140. }
  141. }
  142. vlan_proc_cleanup();
  143. return -ENOBUFS;
  144. }
  145. /*
  146. * Add directory entry for VLAN device.
  147. */
  148. int vlan_proc_add_dev (struct net_device *vlandev)
  149. {
  150. struct vlan_dev_info *dev_info = VLAN_DEV_INFO(vlandev);
  151. if (!(vlandev->priv_flags & IFF_802_1Q_VLAN)) {
  152. printk(KERN_ERR
  153. "ERROR: vlan_proc_add, device -:%s:- is NOT a VLAN\n",
  154. vlandev->name);
  155. return -EINVAL;
  156. }
  157. dev_info->dent = create_proc_entry(vlandev->name,
  158. S_IFREG|S_IRUSR|S_IWUSR,
  159. proc_vlan_dir);
  160. if (!dev_info->dent)
  161. return -ENOBUFS;
  162. dev_info->dent->proc_fops = &vlandev_fops;
  163. dev_info->dent->data = vlandev;
  164. #ifdef VLAN_DEBUG
  165. printk(KERN_ERR "vlan_proc_add, device -:%s:- being added.\n",
  166. vlandev->name);
  167. #endif
  168. return 0;
  169. }
  170. /*
  171. * Delete directory entry for VLAN device.
  172. */
  173. int vlan_proc_rem_dev(struct net_device *vlandev)
  174. {
  175. if (!vlandev) {
  176. printk(VLAN_ERR "%s: invalid argument: %p\n",
  177. __FUNCTION__, vlandev);
  178. return -EINVAL;
  179. }
  180. if (!(vlandev->priv_flags & IFF_802_1Q_VLAN)) {
  181. printk(VLAN_DBG "%s: invalid argument, device: %s is not a VLAN device, priv_flags: 0x%4hX.\n",
  182. __FUNCTION__, vlandev->name, vlandev->priv_flags);
  183. return -EINVAL;
  184. }
  185. #ifdef VLAN_DEBUG
  186. printk(VLAN_DBG "%s: dev: %p\n", __FUNCTION__, vlandev);
  187. #endif
  188. /** NOTE: This will consume the memory pointed to by dent, it seems. */
  189. if (VLAN_DEV_INFO(vlandev)->dent) {
  190. remove_proc_entry(VLAN_DEV_INFO(vlandev)->dent->name, proc_vlan_dir);
  191. VLAN_DEV_INFO(vlandev)->dent = NULL;
  192. }
  193. return 0;
  194. }
  195. /****** Proc filesystem entry points ****************************************/
  196. /*
  197. * The following few functions build the content of /proc/net/vlan/config
  198. */
  199. /* starting at dev, find a VLAN device */
  200. static struct net_device *vlan_skip(struct net_device *dev)
  201. {
  202. while (dev && !(dev->priv_flags & IFF_802_1Q_VLAN))
  203. dev = dev->next;
  204. return dev;
  205. }
  206. /* start read of /proc/net/vlan/config */
  207. static void *vlan_seq_start(struct seq_file *seq, loff_t *pos)
  208. {
  209. struct net_device *dev;
  210. loff_t i = 1;
  211. read_lock(&dev_base_lock);
  212. if (*pos == 0)
  213. return SEQ_START_TOKEN;
  214. for (dev = vlan_skip(dev_base); dev && i < *pos;
  215. dev = vlan_skip(dev->next), ++i);
  216. return (i == *pos) ? dev : NULL;
  217. }
  218. static void *vlan_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  219. {
  220. ++*pos;
  221. return vlan_skip((v == SEQ_START_TOKEN)
  222. ? dev_base
  223. : ((struct net_device *)v)->next);
  224. }
  225. static void vlan_seq_stop(struct seq_file *seq, void *v)
  226. {
  227. read_unlock(&dev_base_lock);
  228. }
  229. static int vlan_seq_show(struct seq_file *seq, void *v)
  230. {
  231. if (v == SEQ_START_TOKEN) {
  232. const char *nmtype = NULL;
  233. seq_puts(seq, "VLAN Dev name | VLAN ID\n");
  234. if (vlan_name_type < ARRAY_SIZE(vlan_name_type_str))
  235. nmtype = vlan_name_type_str[vlan_name_type];
  236. seq_printf(seq, "Name-Type: %s\n",
  237. nmtype ? nmtype : "UNKNOWN" );
  238. } else {
  239. const struct net_device *vlandev = v;
  240. const struct vlan_dev_info *dev_info = VLAN_DEV_INFO(vlandev);
  241. seq_printf(seq, "%-15s| %d | %s\n", vlandev->name,
  242. dev_info->vlan_id, dev_info->real_dev->name);
  243. }
  244. return 0;
  245. }
  246. static int vlandev_seq_show(struct seq_file *seq, void *offset)
  247. {
  248. struct net_device *vlandev = (struct net_device *) seq->private;
  249. const struct vlan_dev_info *dev_info = VLAN_DEV_INFO(vlandev);
  250. struct net_device_stats *stats;
  251. static const char fmt[] = "%30s %12lu\n";
  252. int i;
  253. if ((vlandev == NULL) || (!(vlandev->priv_flags & IFF_802_1Q_VLAN)))
  254. return 0;
  255. seq_printf(seq, "%s VID: %d REORDER_HDR: %i dev->priv_flags: %hx\n",
  256. vlandev->name, dev_info->vlan_id,
  257. (int)(dev_info->flags & 1), vlandev->priv_flags);
  258. stats = vlan_dev_get_stats(vlandev);
  259. seq_printf(seq, fmt, "total frames received", stats->rx_packets);
  260. seq_printf(seq, fmt, "total bytes received", stats->rx_bytes);
  261. seq_printf(seq, fmt, "Broadcast/Multicast Rcvd", stats->multicast);
  262. seq_puts(seq, "\n");
  263. seq_printf(seq, fmt, "total frames transmitted", stats->tx_packets);
  264. seq_printf(seq, fmt, "total bytes transmitted", stats->tx_bytes);
  265. seq_printf(seq, fmt, "total headroom inc",
  266. dev_info->cnt_inc_headroom_on_tx);
  267. seq_printf(seq, fmt, "total encap on xmit",
  268. dev_info->cnt_encap_on_xmit);
  269. seq_printf(seq, "Device: %s", dev_info->real_dev->name);
  270. /* now show all PRIORITY mappings relating to this VLAN */
  271. seq_printf(seq,
  272. "\nINGRESS priority mappings: 0:%lu 1:%lu 2:%lu 3:%lu 4:%lu 5:%lu 6:%lu 7:%lu\n",
  273. dev_info->ingress_priority_map[0],
  274. dev_info->ingress_priority_map[1],
  275. dev_info->ingress_priority_map[2],
  276. dev_info->ingress_priority_map[3],
  277. dev_info->ingress_priority_map[4],
  278. dev_info->ingress_priority_map[5],
  279. dev_info->ingress_priority_map[6],
  280. dev_info->ingress_priority_map[7]);
  281. seq_printf(seq, "EGRESSS priority Mappings: ");
  282. for (i = 0; i < 16; i++) {
  283. const struct vlan_priority_tci_mapping *mp
  284. = dev_info->egress_priority_map[i];
  285. while (mp) {
  286. seq_printf(seq, "%lu:%hu ",
  287. mp->priority, ((mp->vlan_qos >> 13) & 0x7));
  288. mp = mp->next;
  289. }
  290. }
  291. seq_puts(seq, "\n");
  292. return 0;
  293. }