vlanproc.c 9.4 KB

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