net-procfs.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #include <linux/netdevice.h>
  2. #include <linux/proc_fs.h>
  3. #include <linux/seq_file.h>
  4. #include <net/wext.h>
  5. #define BUCKET_SPACE (32 - NETDEV_HASHBITS - 1)
  6. #define get_bucket(x) ((x) >> BUCKET_SPACE)
  7. #define get_offset(x) ((x) & ((1 << BUCKET_SPACE) - 1))
  8. #define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o))
  9. extern struct list_head ptype_all __read_mostly;
  10. extern struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
  11. static inline struct net_device *dev_from_same_bucket(struct seq_file *seq, loff_t *pos)
  12. {
  13. struct net *net = seq_file_net(seq);
  14. struct net_device *dev;
  15. struct hlist_node *p;
  16. struct hlist_head *h;
  17. unsigned int count = 0, offset = get_offset(*pos);
  18. h = &net->dev_name_head[get_bucket(*pos)];
  19. hlist_for_each_entry_rcu(dev, p, h, name_hlist) {
  20. if (++count == offset)
  21. return dev;
  22. }
  23. return NULL;
  24. }
  25. static inline struct net_device *dev_from_bucket(struct seq_file *seq, loff_t *pos)
  26. {
  27. struct net_device *dev;
  28. unsigned int bucket;
  29. do {
  30. dev = dev_from_same_bucket(seq, pos);
  31. if (dev)
  32. return dev;
  33. bucket = get_bucket(*pos) + 1;
  34. *pos = set_bucket_offset(bucket, 1);
  35. } while (bucket < NETDEV_HASHENTRIES);
  36. return NULL;
  37. }
  38. /*
  39. * This is invoked by the /proc filesystem handler to display a device
  40. * in detail.
  41. */
  42. static void *dev_seq_start(struct seq_file *seq, loff_t *pos)
  43. __acquires(RCU)
  44. {
  45. rcu_read_lock();
  46. if (!*pos)
  47. return SEQ_START_TOKEN;
  48. if (get_bucket(*pos) >= NETDEV_HASHENTRIES)
  49. return NULL;
  50. return dev_from_bucket(seq, pos);
  51. }
  52. static void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  53. {
  54. ++*pos;
  55. return dev_from_bucket(seq, pos);
  56. }
  57. static void dev_seq_stop(struct seq_file *seq, void *v)
  58. __releases(RCU)
  59. {
  60. rcu_read_unlock();
  61. }
  62. static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
  63. {
  64. struct rtnl_link_stats64 temp;
  65. const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
  66. seq_printf(seq, "%6s: %7llu %7llu %4llu %4llu %4llu %5llu %10llu %9llu "
  67. "%8llu %7llu %4llu %4llu %4llu %5llu %7llu %10llu\n",
  68. dev->name, stats->rx_bytes, stats->rx_packets,
  69. stats->rx_errors,
  70. stats->rx_dropped + stats->rx_missed_errors,
  71. stats->rx_fifo_errors,
  72. stats->rx_length_errors + stats->rx_over_errors +
  73. stats->rx_crc_errors + stats->rx_frame_errors,
  74. stats->rx_compressed, stats->multicast,
  75. stats->tx_bytes, stats->tx_packets,
  76. stats->tx_errors, stats->tx_dropped,
  77. stats->tx_fifo_errors, stats->collisions,
  78. stats->tx_carrier_errors +
  79. stats->tx_aborted_errors +
  80. stats->tx_window_errors +
  81. stats->tx_heartbeat_errors,
  82. stats->tx_compressed);
  83. }
  84. /*
  85. * Called from the PROCfs module. This now uses the new arbitrary sized
  86. * /proc/net interface to create /proc/net/dev
  87. */
  88. static int dev_seq_show(struct seq_file *seq, void *v)
  89. {
  90. if (v == SEQ_START_TOKEN)
  91. seq_puts(seq, "Inter-| Receive "
  92. " | Transmit\n"
  93. " face |bytes packets errs drop fifo frame "
  94. "compressed multicast|bytes packets errs "
  95. "drop fifo colls carrier compressed\n");
  96. else
  97. dev_seq_printf_stats(seq, v);
  98. return 0;
  99. }
  100. static struct softnet_data *softnet_get_online(loff_t *pos)
  101. {
  102. struct softnet_data *sd = NULL;
  103. while (*pos < nr_cpu_ids)
  104. if (cpu_online(*pos)) {
  105. sd = &per_cpu(softnet_data, *pos);
  106. break;
  107. } else
  108. ++*pos;
  109. return sd;
  110. }
  111. static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
  112. {
  113. return softnet_get_online(pos);
  114. }
  115. static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  116. {
  117. ++*pos;
  118. return softnet_get_online(pos);
  119. }
  120. static void softnet_seq_stop(struct seq_file *seq, void *v)
  121. {
  122. }
  123. static int softnet_seq_show(struct seq_file *seq, void *v)
  124. {
  125. struct softnet_data *sd = v;
  126. seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
  127. sd->processed, sd->dropped, sd->time_squeeze, 0,
  128. 0, 0, 0, 0, /* was fastroute */
  129. sd->cpu_collision, sd->received_rps);
  130. return 0;
  131. }
  132. static const struct seq_operations dev_seq_ops = {
  133. .start = dev_seq_start,
  134. .next = dev_seq_next,
  135. .stop = dev_seq_stop,
  136. .show = dev_seq_show,
  137. };
  138. static int dev_seq_open(struct inode *inode, struct file *file)
  139. {
  140. return seq_open_net(inode, file, &dev_seq_ops,
  141. sizeof(struct seq_net_private));
  142. }
  143. static const struct file_operations dev_seq_fops = {
  144. .owner = THIS_MODULE,
  145. .open = dev_seq_open,
  146. .read = seq_read,
  147. .llseek = seq_lseek,
  148. .release = seq_release_net,
  149. };
  150. static const struct seq_operations softnet_seq_ops = {
  151. .start = softnet_seq_start,
  152. .next = softnet_seq_next,
  153. .stop = softnet_seq_stop,
  154. .show = softnet_seq_show,
  155. };
  156. static int softnet_seq_open(struct inode *inode, struct file *file)
  157. {
  158. return seq_open(file, &softnet_seq_ops);
  159. }
  160. static const struct file_operations softnet_seq_fops = {
  161. .owner = THIS_MODULE,
  162. .open = softnet_seq_open,
  163. .read = seq_read,
  164. .llseek = seq_lseek,
  165. .release = seq_release,
  166. };
  167. static void *ptype_get_idx(loff_t pos)
  168. {
  169. struct packet_type *pt = NULL;
  170. loff_t i = 0;
  171. int t;
  172. list_for_each_entry_rcu(pt, &ptype_all, list) {
  173. if (i == pos)
  174. return pt;
  175. ++i;
  176. }
  177. for (t = 0; t < PTYPE_HASH_SIZE; t++) {
  178. list_for_each_entry_rcu(pt, &ptype_base[t], list) {
  179. if (i == pos)
  180. return pt;
  181. ++i;
  182. }
  183. }
  184. return NULL;
  185. }
  186. static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
  187. __acquires(RCU)
  188. {
  189. rcu_read_lock();
  190. return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
  191. }
  192. static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  193. {
  194. struct packet_type *pt;
  195. struct list_head *nxt;
  196. int hash;
  197. ++*pos;
  198. if (v == SEQ_START_TOKEN)
  199. return ptype_get_idx(0);
  200. pt = v;
  201. nxt = pt->list.next;
  202. if (pt->type == htons(ETH_P_ALL)) {
  203. if (nxt != &ptype_all)
  204. goto found;
  205. hash = 0;
  206. nxt = ptype_base[0].next;
  207. } else
  208. hash = ntohs(pt->type) & PTYPE_HASH_MASK;
  209. while (nxt == &ptype_base[hash]) {
  210. if (++hash >= PTYPE_HASH_SIZE)
  211. return NULL;
  212. nxt = ptype_base[hash].next;
  213. }
  214. found:
  215. return list_entry(nxt, struct packet_type, list);
  216. }
  217. static void ptype_seq_stop(struct seq_file *seq, void *v)
  218. __releases(RCU)
  219. {
  220. rcu_read_unlock();
  221. }
  222. static int ptype_seq_show(struct seq_file *seq, void *v)
  223. {
  224. struct packet_type *pt = v;
  225. if (v == SEQ_START_TOKEN)
  226. seq_puts(seq, "Type Device Function\n");
  227. else if (pt->dev == NULL || dev_net(pt->dev) == seq_file_net(seq)) {
  228. if (pt->type == htons(ETH_P_ALL))
  229. seq_puts(seq, "ALL ");
  230. else
  231. seq_printf(seq, "%04x", ntohs(pt->type));
  232. seq_printf(seq, " %-8s %pF\n",
  233. pt->dev ? pt->dev->name : "", pt->func);
  234. }
  235. return 0;
  236. }
  237. static const struct seq_operations ptype_seq_ops = {
  238. .start = ptype_seq_start,
  239. .next = ptype_seq_next,
  240. .stop = ptype_seq_stop,
  241. .show = ptype_seq_show,
  242. };
  243. static int ptype_seq_open(struct inode *inode, struct file *file)
  244. {
  245. return seq_open_net(inode, file, &ptype_seq_ops,
  246. sizeof(struct seq_net_private));
  247. }
  248. static const struct file_operations ptype_seq_fops = {
  249. .owner = THIS_MODULE,
  250. .open = ptype_seq_open,
  251. .read = seq_read,
  252. .llseek = seq_lseek,
  253. .release = seq_release_net,
  254. };
  255. static int __net_init dev_proc_net_init(struct net *net)
  256. {
  257. int rc = -ENOMEM;
  258. if (!proc_create("dev", S_IRUGO, net->proc_net, &dev_seq_fops))
  259. goto out;
  260. if (!proc_create("softnet_stat", S_IRUGO, net->proc_net,
  261. &softnet_seq_fops))
  262. goto out_dev;
  263. if (!proc_create("ptype", S_IRUGO, net->proc_net, &ptype_seq_fops))
  264. goto out_softnet;
  265. if (wext_proc_init(net))
  266. goto out_ptype;
  267. rc = 0;
  268. out:
  269. return rc;
  270. out_ptype:
  271. remove_proc_entry("ptype", net->proc_net);
  272. out_softnet:
  273. remove_proc_entry("softnet_stat", net->proc_net);
  274. out_dev:
  275. remove_proc_entry("dev", net->proc_net);
  276. goto out;
  277. }
  278. static void __net_exit dev_proc_net_exit(struct net *net)
  279. {
  280. wext_proc_exit(net);
  281. remove_proc_entry("ptype", net->proc_net);
  282. remove_proc_entry("softnet_stat", net->proc_net);
  283. remove_proc_entry("dev", net->proc_net);
  284. }
  285. static struct pernet_operations __net_initdata dev_proc_ops = {
  286. .init = dev_proc_net_init,
  287. .exit = dev_proc_net_exit,
  288. };
  289. static int dev_mc_seq_show(struct seq_file *seq, void *v)
  290. {
  291. struct netdev_hw_addr *ha;
  292. struct net_device *dev = v;
  293. if (v == SEQ_START_TOKEN)
  294. return 0;
  295. netif_addr_lock_bh(dev);
  296. netdev_for_each_mc_addr(ha, dev) {
  297. int i;
  298. seq_printf(seq, "%-4d %-15s %-5d %-5d ", dev->ifindex,
  299. dev->name, ha->refcount, ha->global_use);
  300. for (i = 0; i < dev->addr_len; i++)
  301. seq_printf(seq, "%02x", ha->addr[i]);
  302. seq_putc(seq, '\n');
  303. }
  304. netif_addr_unlock_bh(dev);
  305. return 0;
  306. }
  307. static const struct seq_operations dev_mc_seq_ops = {
  308. .start = dev_seq_start,
  309. .next = dev_seq_next,
  310. .stop = dev_seq_stop,
  311. .show = dev_mc_seq_show,
  312. };
  313. static int dev_mc_seq_open(struct inode *inode, struct file *file)
  314. {
  315. return seq_open_net(inode, file, &dev_mc_seq_ops,
  316. sizeof(struct seq_net_private));
  317. }
  318. static const struct file_operations dev_mc_seq_fops = {
  319. .owner = THIS_MODULE,
  320. .open = dev_mc_seq_open,
  321. .read = seq_read,
  322. .llseek = seq_lseek,
  323. .release = seq_release_net,
  324. };
  325. static int __net_init dev_mc_net_init(struct net *net)
  326. {
  327. if (!proc_create("dev_mcast", 0, net->proc_net, &dev_mc_seq_fops))
  328. return -ENOMEM;
  329. return 0;
  330. }
  331. static void __net_exit dev_mc_net_exit(struct net *net)
  332. {
  333. remove_proc_entry("dev_mcast", net->proc_net);
  334. }
  335. static struct pernet_operations __net_initdata dev_mc_net_ops = {
  336. .init = dev_mc_net_init,
  337. .exit = dev_mc_net_exit,
  338. };
  339. int __init dev_proc_init(void)
  340. {
  341. int ret = register_pernet_subsys(&dev_proc_ops);
  342. if (!ret)
  343. return register_pernet_subsys(&dev_mc_net_ops);
  344. return ret;
  345. }