net-procfs.c 9.6 KB

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