net-procfs.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/netdevice.h>
  3. #include <linux/proc_fs.h>
  4. #include <linux/seq_file.h>
  5. #include <net/wext.h>
  6. #define BUCKET_SPACE (32 - NETDEV_HASHBITS - 1)
  7. #define get_bucket(x) ((x) >> BUCKET_SPACE)
  8. #define get_offset(x) ((x) & ((1 << BUCKET_SPACE) - 1))
  9. #define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o))
  10. extern struct list_head ptype_all __read_mostly;
  11. extern struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
  12. static inline struct net_device *dev_from_same_bucket(struct seq_file *seq, loff_t *pos)
  13. {
  14. struct net *net = seq_file_net(seq);
  15. struct net_device *dev;
  16. struct hlist_head *h;
  17. unsigned int count = 0, offset = get_offset(*pos);
  18. h = &net->dev_index_head[get_bucket(*pos)];
  19. hlist_for_each_entry_rcu(dev, h, index_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 u32 softnet_backlog_len(struct softnet_data *sd)
  101. {
  102. return skb_queue_len_lockless(&sd->input_pkt_queue) +
  103. skb_queue_len_lockless(&sd->process_queue);
  104. }
  105. static struct softnet_data *softnet_get_online(loff_t *pos)
  106. {
  107. struct softnet_data *sd = NULL;
  108. while (*pos < nr_cpu_ids)
  109. if (cpu_online(*pos)) {
  110. sd = &per_cpu(softnet_data, *pos);
  111. break;
  112. } else
  113. ++*pos;
  114. return sd;
  115. }
  116. static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
  117. {
  118. return softnet_get_online(pos);
  119. }
  120. static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  121. {
  122. ++*pos;
  123. return softnet_get_online(pos);
  124. }
  125. static void softnet_seq_stop(struct seq_file *seq, void *v)
  126. {
  127. }
  128. static int softnet_seq_show(struct seq_file *seq, void *v)
  129. {
  130. struct softnet_data *sd = v;
  131. unsigned int flow_limit_count = 0;
  132. #ifdef CONFIG_NET_FLOW_LIMIT
  133. struct sd_flow_limit *fl;
  134. rcu_read_lock();
  135. fl = rcu_dereference(sd->flow_limit);
  136. if (fl)
  137. flow_limit_count = fl->count;
  138. rcu_read_unlock();
  139. #endif
  140. /* the index is the CPU id owing this sd. Since offline CPUs are not
  141. * displayed, it would be othrwise not trivial for the user-space
  142. * mapping the data a specific CPU
  143. */
  144. seq_printf(seq,
  145. "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
  146. sd->processed, sd->dropped, sd->time_squeeze, 0,
  147. 0, 0, 0, 0, /* was fastroute */
  148. 0, /* was cpu_collision */
  149. sd->received_rps, flow_limit_count,
  150. softnet_backlog_len(sd), (int)seq->index);
  151. return 0;
  152. }
  153. static const struct seq_operations dev_seq_ops = {
  154. .start = dev_seq_start,
  155. .next = dev_seq_next,
  156. .stop = dev_seq_stop,
  157. .show = dev_seq_show,
  158. };
  159. static const struct seq_operations softnet_seq_ops = {
  160. .start = softnet_seq_start,
  161. .next = softnet_seq_next,
  162. .stop = softnet_seq_stop,
  163. .show = softnet_seq_show,
  164. };
  165. static void *ptype_get_idx(struct seq_file *seq, loff_t pos)
  166. {
  167. struct list_head *ptype_list = NULL;
  168. struct packet_type *pt = NULL;
  169. struct net_device *dev;
  170. loff_t i = 0;
  171. int t;
  172. for_each_netdev_rcu(seq_file_net(seq), dev) {
  173. ptype_list = &dev->ptype_all;
  174. list_for_each_entry_rcu(pt, ptype_list, list) {
  175. if (i == pos)
  176. return pt;
  177. ++i;
  178. }
  179. }
  180. list_for_each_entry_rcu(pt, &ptype_all, list) {
  181. if (i == pos)
  182. return pt;
  183. ++i;
  184. }
  185. for (t = 0; t < PTYPE_HASH_SIZE; t++) {
  186. list_for_each_entry_rcu(pt, &ptype_base[t], list) {
  187. if (i == pos)
  188. return pt;
  189. ++i;
  190. }
  191. }
  192. return NULL;
  193. }
  194. static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
  195. __acquires(RCU)
  196. {
  197. rcu_read_lock();
  198. return *pos ? ptype_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  199. }
  200. static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  201. {
  202. struct net_device *dev;
  203. struct packet_type *pt;
  204. struct list_head *nxt;
  205. int hash;
  206. ++*pos;
  207. if (v == SEQ_START_TOKEN)
  208. return ptype_get_idx(seq, 0);
  209. pt = v;
  210. nxt = pt->list.next;
  211. if (pt->dev) {
  212. if (nxt != &pt->dev->ptype_all)
  213. goto found;
  214. dev = pt->dev;
  215. for_each_netdev_continue_rcu(seq_file_net(seq), dev) {
  216. if (!list_empty(&dev->ptype_all)) {
  217. nxt = dev->ptype_all.next;
  218. goto found;
  219. }
  220. }
  221. nxt = ptype_all.next;
  222. goto ptype_all;
  223. }
  224. if (pt->type == htons(ETH_P_ALL)) {
  225. ptype_all:
  226. if (nxt != &ptype_all)
  227. goto found;
  228. hash = 0;
  229. nxt = ptype_base[0].next;
  230. } else
  231. hash = ntohs(pt->type) & PTYPE_HASH_MASK;
  232. while (nxt == &ptype_base[hash]) {
  233. if (++hash >= PTYPE_HASH_SIZE)
  234. return NULL;
  235. nxt = ptype_base[hash].next;
  236. }
  237. found:
  238. return list_entry(nxt, struct packet_type, list);
  239. }
  240. static void ptype_seq_stop(struct seq_file *seq, void *v)
  241. __releases(RCU)
  242. {
  243. rcu_read_unlock();
  244. }
  245. static int ptype_seq_show(struct seq_file *seq, void *v)
  246. {
  247. struct packet_type *pt = v;
  248. if (v == SEQ_START_TOKEN)
  249. seq_puts(seq, "Type Device Function\n");
  250. else if ((!pt->af_packet_net || net_eq(pt->af_packet_net, seq_file_net(seq))) &&
  251. (!pt->dev || net_eq(dev_net(pt->dev), seq_file_net(seq)))) {
  252. if (pt->type == htons(ETH_P_ALL))
  253. seq_puts(seq, "ALL ");
  254. else
  255. seq_printf(seq, "%04x", ntohs(pt->type));
  256. seq_printf(seq, " %-8s %ps\n",
  257. pt->dev ? pt->dev->name : "", pt->func);
  258. }
  259. return 0;
  260. }
  261. static const struct seq_operations ptype_seq_ops = {
  262. .start = ptype_seq_start,
  263. .next = ptype_seq_next,
  264. .stop = ptype_seq_stop,
  265. .show = ptype_seq_show,
  266. };
  267. static int __net_init dev_proc_net_init(struct net *net)
  268. {
  269. int rc = -ENOMEM;
  270. if (!proc_create_net("dev", 0444, net->proc_net, &dev_seq_ops,
  271. sizeof(struct seq_net_private)))
  272. goto out;
  273. if (!proc_create_seq("softnet_stat", 0444, net->proc_net,
  274. &softnet_seq_ops))
  275. goto out_dev;
  276. if (!proc_create_net("ptype", 0444, net->proc_net, &ptype_seq_ops,
  277. sizeof(struct seq_net_private)))
  278. goto out_softnet;
  279. if (wext_proc_init(net))
  280. goto out_ptype;
  281. rc = 0;
  282. out:
  283. return rc;
  284. out_ptype:
  285. remove_proc_entry("ptype", net->proc_net);
  286. out_softnet:
  287. remove_proc_entry("softnet_stat", net->proc_net);
  288. out_dev:
  289. remove_proc_entry("dev", net->proc_net);
  290. goto out;
  291. }
  292. static void __net_exit dev_proc_net_exit(struct net *net)
  293. {
  294. wext_proc_exit(net);
  295. remove_proc_entry("ptype", net->proc_net);
  296. remove_proc_entry("softnet_stat", net->proc_net);
  297. remove_proc_entry("dev", net->proc_net);
  298. }
  299. static struct pernet_operations __net_initdata dev_proc_ops = {
  300. .init = dev_proc_net_init,
  301. .exit = dev_proc_net_exit,
  302. };
  303. static int dev_mc_seq_show(struct seq_file *seq, void *v)
  304. {
  305. struct netdev_hw_addr *ha;
  306. struct net_device *dev = v;
  307. if (v == SEQ_START_TOKEN)
  308. return 0;
  309. netif_addr_lock_bh(dev);
  310. netdev_for_each_mc_addr(ha, dev) {
  311. seq_printf(seq, "%-4d %-15s %-5d %-5d %*phN\n",
  312. dev->ifindex, dev->name,
  313. ha->refcount, ha->global_use,
  314. (int)dev->addr_len, ha->addr);
  315. }
  316. netif_addr_unlock_bh(dev);
  317. return 0;
  318. }
  319. static const struct seq_operations dev_mc_seq_ops = {
  320. .start = dev_seq_start,
  321. .next = dev_seq_next,
  322. .stop = dev_seq_stop,
  323. .show = dev_mc_seq_show,
  324. };
  325. static int __net_init dev_mc_net_init(struct net *net)
  326. {
  327. if (!proc_create_net("dev_mcast", 0, net->proc_net, &dev_mc_seq_ops,
  328. sizeof(struct seq_net_private)))
  329. return -ENOMEM;
  330. return 0;
  331. }
  332. static void __net_exit dev_mc_net_exit(struct net *net)
  333. {
  334. remove_proc_entry("dev_mcast", net->proc_net);
  335. }
  336. static struct pernet_operations __net_initdata dev_mc_net_ops = {
  337. .init = dev_mc_net_init,
  338. .exit = dev_mc_net_exit,
  339. };
  340. int __init dev_proc_init(void)
  341. {
  342. int ret = register_pernet_subsys(&dev_proc_ops);
  343. if (!ret)
  344. return register_pernet_subsys(&dev_mc_net_ops);
  345. return ret;
  346. }